You need to sign in to do that
Don't have an account?
Need Test class code for the apex class urgently
Hi,
We need test class code for the below controller class satisfying all the criterias urgently.
public class TestController {
public PageReference save() {
Integer CountofWs;
Integer CountofAs;
Map<ID,Integer> W = new Map<ID,Integer>();
Map<ID,Integer> A = new Map<ID,Integer>();
List<Opportunity> FinOpp = new List<Opportunity>();
List<Opportunity> FlagOpp = new List<Opportunity>();
List<Opportunity> UnFlagOpp = new List<Opportunity>();
List<OpportunityLineItem> AllOps = new List<OpportunityLineItem>();
Datetime endd = Datetime.now();
Datetime start = Datetime.now().addHours(-1);
//Get all the Opportunities which are modified an hour ago
List<Opportunity> opptys = [SELECT o.Id,o.Name, o.Type, o.AccountId,o.StageName, o.Cloud_Attached__c, o.Parent_Opportunity__c FROM Opportunity o WHERE o.LastModifiedDate>= : start and o.LastModifiedDate<= : endd];
if(!opptys.isEmpty()) {
System.debug('Opportunity List'+opptys);
for(Opportunity oppty :opptys){
//get all the Parent Opportunities first
if(oppty.Parent_Opportunity__c!=null){
if(!w.containsKey(oppty.parent_Opportunity__c)){
W.put(oppty.parent_Opportunity__c, 0);
A.put(oppty.parent_Opportunity__c, 0);
}
}
if(!w.containsKey(oppty.Id)){
W.put(oppty.Id, 0);
A.put(oppty.Id, 0);
}
}//for
System.debug(' A Keyset - :'+ A.keyset()+' W Keyset - :' + W.keyset()+' :W Keyset is Empty ?'+W.isEmpty()+' W Size : '+ W.size());
if(W.size()>0){
//Get all Children of All Unique Parent Id's collected
AllOps = [Select Opportunity.id, Opportunity.StageName, Opportunity.Type,Opportunity.Cloud_Attached__c, Opportunity.parent_Opportunity__c,OpportunityLineItem.PricebookEntry.Product2.Supplier__c from Opportunitylineitem where Opportunity.parent_Opportunity__c in :W.keyset()];
System.debug('Step 1.3 AllOps - :'+AllOps);
// Calculate how many W's and A's each parent has
for(Integer i=0; i<AllOps.size(); i++){
CountofWs = 0;
CountofAs = 0;
if (AllOps.size()>0){
if (AllOps.get(i).Opportunity.parent_Opportunity__c!=NULL){
if (AllOps[i].PricebookEntry.Product2.Supplier__c==NULL || AllOps[i].PricebookEntry.Product2.Supplier__c=='Cloud - Long Term'){
if (AllOps[i].Opportunity.Type.contains('Cloud') && AllOps[i].Opportunity.StageName=='Won'){
system.debug('Step 1.4 AllOps - :'+AllOps[i]);
CountofWs = W.get(AllOps[i].Opportunity.parent_Opportunity__c);
CountofWs = CountofWs+1;
//system.debug('Step 1.5 - ' + CountofWs);
W.put(AllOps[i].Opportunity.parent_Opportunity__c, CountofWs);
}
system.debug('Step 1.7 - Ws'+W.values());
if (AllOps[i].Opportunity.Type.contains('Cloud') && AllOps[i].Opportunity.StageName<>'Won'){
System.debug('Step 1.4 AllOps - :'+AllOps[i]);
CountofAs = A.get(AllOps[i].Opportunity.parent_Opportunity__c);
CountofAs = CountofAs+1;
//system.debug('Step 2 - '+CountofAs);
A.put(AllOps[i].Opportunity.parent_Opportunity__c, CountofAs);
//W.put (AllOps[i].parent_Opportunity__c, 0);
//CountofAs = 0;
}
system.debug('Step 2.1 - As'+A.values());
}//if
}//if
}//if
}//for
Set<ID> Oppidset = W.keyset();
System.debug('Step 3 - Keyset' + Oppidset);
List<ID> Oppidlist = new List<ID>();
Oppidlist.addAll(Oppidset);
System.debug('Step 4 - Keylist' + Oppidlist);
for(Integer i=0; i<Oppidlist.size(); i++){
Opportunity Opp = new Opportunity(id=Oppidlist.get(i), Cloud_Attach_TriggerRun__c=True);
FlagOpp.add(Opp);
}
for(Integer i=0; i<Oppidlist.size(); i++){
System.debug('Step 4.5 - Oppidlist : ' + Oppidlist.get(i));
System.debug('Step 4.5 - W-Value : ' + W.get(Oppidlist.get(i)));
System.debug('Step 4.5 - A-Value : ' + A.get(Oppidlist.get(i)));
if (W.get(Oppidlist.get(i))>=A.get(Oppidlist.get(i)) && W.get(Oppidlist.get(i))>0){
Opportunity Opp = new Opportunity(id=Oppidlist.get(i), Cloud_Attached__c='W');
FinOpp.add(Opp);
System.debug('Step 5 - FinOppWs' + FinOpp);
}
if (A.get(Oppidlist.get(i))>W.get(Oppidlist.get(i))){
Opportunity Opp = new Opportunity(id=Oppidlist.get(i), Cloud_Attached__c='A');
FinOpp.add(Opp);
System.debug('Step 6 - FinOppAs' + FinOpp);
}
if (A.get(Oppidlist.get(i))==0 && W.get(Oppidlist.get(i))==0){
Opportunity Opp = new Opportunity(id=Oppidlist.get(i), Cloud_Attached__c='');
FinOpp.add(Opp);
System.debug('Step 7 - FinOppWithNulls' + FinOpp);
}
}//for
for(Integer i=0; i<Oppidlist.size(); i++){
Opportunity Opp = new Opportunity(id=Oppidlist.get(i), Cloud_Attach_TriggerRun__c=False);
UnFlagOpp.add(Opp);
}
System.debug('Step 7 - FinOpp' + FinOpp);
Update FlagOpp;
Update FinOpp;
Update UnFlagOpp;
}//if (w.size()>0)
}//if
return null;
}//save
}//class
Any help is appreciated.
Regards
Surender