+ Start a Discussion
bharath kuamarbharath kuamar 

Please help me out with the test class for the below class

public class ManualAssignController
{
    public Preference__c prefer{get;set;}
    List<Commitment__c> list_commitment = new List<Commitment__c>();
    private final Preference__c obj;
    public List<ChildDetails> detailsList {get; set;}
    
 
   
    public ManualAssignController(ApexPages.StandardController stdController)
    {
        this.obj = (Preference__c)stdController.getRecord();
        
        
    }



  
    public class ChildDetails
    {
        public String Name {get; set;}
        public String id {get;set;}
        public String regDate {get;set;}
        public String sex {get; set;}
        public String dob {get;set;}
        public String fieldOfficier{get;set;}
        public String religion {get;set;}
        public String status {get;set;}
        public Boolean isAssign {get;set;}
        
    }
    
    public void generateQuery()
    {
       
      
        detailsList = new List<childDetails>(); 
        Preference__c prefObj = obj;//[Select Id, Age_From__c, Age_To__c, Sex__c, Continent__c, Country__c, Language__c, Religion__c
                                     //         From Preference__c Where Id=:obj.Id];
        List<String> list_status = new List<String>();
        list_status.add('Pooled');
        list_status.add('UnAssigned');
        list_status.add('Cancelled');
        
        String sOQL = 'Select Age__c, Name, Child_Registration_Date__c, Comments__c, Continent__c, Country__c, '
                                + ' Date_of_Birth__c, Field_Officer__r.Name, Language__c, Religion__c, Sex__c, Status__c '
                                + ' From Child__c  Where Status__c IN :list_status';
        String whereSQOL = '';
        if (prefObj.Age_From__c != null && prefObj.Age_To__c != null)
        {
            whereSQOL = whereSQOL + ' And Age__c >='+prefObj.Age_From__c +' And Age__c <= '+ prefObj.Age_To__c;
        }
        else if (prefObj.Age_From__c == null && prefObj.Age_To__c != null)
        {
            whereSQOL = whereSQOL + ' And Age__c >='+prefObj.Age_To__c + ' And Age__c <= ' + prefObj.Age_To__c;
        }
        else if (prefObj.Age_From__c != null && prefObj.Age_To__c == null)
        {
            whereSQOL = whereSQOL + ' And Age__c >= '+ prefObj.Age_From__c + 'And Age__c <= '+prefObj.Age_From__c;
        }
        if (prefObj.Continent__c != null)
        {
            whereSQOL = whereSQOL + ' And Continent__c =\''+prefObj.Continent__c+'\'';
        }
        if (prefObj.Country__c != null)
        {
            whereSQOL = whereSQOL + ' And Country__c =\''+prefObj.Country__c+'\'';
        }
        /*else if (prefObj.Language__c != null)
        {
            whereSQOL = whereSQOL + ' And Language__c =:prefObj.Country__c';
        }*/
        if (prefObj.Religion__c != null)
        {
            whereSQOL = whereSQOL + ' And Religion__c =\''+prefObj.Religion__c+'\'';
        }
        if (prefObj.Sex__c != null)
        {
            whereSQOL = whereSQOL + ' And Sex__c =\''+prefObj.Sex__c+'\'';
        }
        
        sOQL = sOQL +  whereSQOL;
        system.debug('<<<<<<<<<<<<<>>>>>>>>>>>>>>');
        System.debug(sOQL + ' == sOQL');
        for (SObject oSobject : Database.Query(sOQL))
        {
            Child__c obj = (Child__c)oSobject;
            ChildDetails oChildDetails = new ChildDetails();
            oChildDetails.Name = obj.Name;
            oChildDetails.id = obj.Id;
            oChildDetails.regDate = obj.Child_Registration_Date__c != null ? obj.Child_Registration_Date__c.format() : '';
            oChildDetails.sex = obj.Sex__c;
            oChildDetails.dob = obj.Date_of_Birth__c != null ? obj.Date_of_Birth__c .format() : '';
            oChildDetails.fieldOfficier = obj.Field_Officer__r.Name;
            oChildDetails.religion = obj.Religion__c;
            oChildDetails.isAssign = false;
            detailsList.add( oChildDetails );
        }
    }
    
    public void getQueryPrefID(){
     prefer =[select id,name,Duration__c,Opportunity__c from Preference__c where Id=:obj.Id limit 1];
    }
    
    public pagereference assignAction()
    
    {
     getQueryPrefID();
     Opportunity oppid =[select id,contact__c from opportunity where id=:prefer.Opportunity__c];
     PageReference pageRef=New PageReference('/'+oppid.contact__c);
     pageRef.setRedirect(true);
      for (ChildDetails obj : detailsList)
        {
            if (obj.isAssign)
            {
               Commitment__c com=new Commitment__c();
               com.Preference__c=prefer.id;
               com.Commitment_Type__c = 'Sponsor Child';
               com.Start_Date__c = Date.today();
               com.End_Date__c =  Date.today().addMonths(Integer.valueOf(prefer.Duration__c));
               com.Child__c =obj.id;
               com.Commitment_Details__c = 'Manual Assignment';
               com.Status__c = 'Assigned';
                
               //system.debug('<<<<<arun>>>>>'+prefer.name+'>>>>>>>>>>>>'+prefer.ID);
               list_commitment.add(com);
               
             
            }
        }
    
       if ( list_commitment != null &&  list_commitment.size() > 0 ) insert list_commitment;
          generatePaymentSchedules(prefer.id);
       return pageRef;  
    }
    
    
              public void generatePaymentSchedules(String PreferenceID)
              {
                 getQueryPrefID();
                 Opportunity oppamt =[select OppAmount__c from opportunity where id=:prefer.Opportunity__c];
                 system.debug('<<<<<<<<<<<venki>>>>>>>>>>');
                 Preference__c payPref = [select Payment_Frequency__c,Payment_Mode__c,Duration__c from preference__c where id=:PreferenceID];
                  Integer divisionRatio = 1;
                    Integer PayDurationInt = Integer.valueOf(payPref.Duration__c);
                  if (payPref.Payment_Frequency__c == 'Monthly')
                  {
                      divisionRatio = 1;
                  }
                  if (payPref.Payment_Frequency__c == 'Quarterly')
                  {
                      divisionRatio = 3;
                    
                
                      if (PayDurationInt  < 6)
                      {
                            divisionRatio = 1;
                      } 
                  }
                  if (payPref.Payment_Frequency__c == 'Half-Yearly')
                  {
                      divisionRatio = 6;
                     if (PayDurationInt < 12)
                      {
                            divisionRatio = 1;
                      }
                  }
                  if (payPref.Payment_Frequency__c == 'Annually')
                  {
                      divisionRatio = 12;
                      if (PayDurationInt  < 12)
                      {
                            divisionRatio = 1;
                      }
                  }
                  List<Payment_Schedule__c> list_schedule = new List<Payment_Schedule__c>();
                  System.debug(divisionRatio + ' == paymentMode == ' + payPref.Payment_Mode__c);
                  Double scheduleAmt = oppamt.OppAmount__c/divisionRatio;
                  Integer scheduleDueDate = 0;
                  for (Integer j = 0; j < (PayDurationInt /divisionRatio); j++)
                  {
                        Payment_Schedule__c obj = new Payment_Schedule__c();
                        obj.opportunity__c = prefer.Opportunity__c;
                        obj.Amount__c = scheduleAmt;
                        scheduleDueDate = scheduleDueDate + divisionRatio;
                        obj.Payment_Start_Date__c = Date.today().addMonths(scheduleDueDate);
                        list_schedule.add(obj);
                  } 
                  if (list_schedule.size() > 0) insert list_schedule;
                  closedOpportunity();
            }
 
    public void closedOpportunity(){
          getQueryPrefID();
          Opportunity opp = [select id,StageName from opportunity where id=:prefer.Opportunity__c];
          opp.StageName ='Closed Won';
           update opp;
        
        }
    
    
    
    public pagereference cancelAction()
    
    {
    getQueryPrefID();
    PageReference pageRef=New PageReference('/'+prefer.Opportunity__c);
      pageRef.setRedirect(true);
        return pageRef;
     
    }
    
}

Please helpme