Find your content:

Search form

You are here

Writing a test classes

 
Share
global class DealExclusivityDialogController1 {
    public List<Exclusive_Categories_for_Deal__c> existcatedealList = null;

    public void closeWindow() {

        List<Exclusive_Categories_for_Deal__c> ecdList = new List<Exclusive_Categories_for_Deal__c>();
        for(rowwrapper rw: rows) {
            for(RowInnerWrapper riw: rw.riwList) {
                System.debug('-------riw:'+riw);
                if(riw.isSelected) {
                    Exclusive_Categories_for_Deal__c ecd = new Exclusive_Categories_for_Deal__c(Deal_Name__c=oppId, Exclusive_Category__c=riw.deal.Category_Id__c);
                            ecdList.add(ecd);

                }
            }
        }
        if(existcatedealList != null && !existcatedealList.isEmpty()) {
            delete existcatedealList;
        } 
        insert ecdList;
    }
   public void parentSelection(){
       for(rowwrapper rw: rows) {
           rw.isSelect= false;
           for(RowInnerWrapper riw: rw.riwList){
               if(riw.isSelected)
                 rw.isSelect= true;
                  // break;   
                }  
           }    
   } 

  public String oppId {get; set;}
  public Opportunity opp  {get; set;} 
  public List<rowWrapper> rows {get; set;}
  public List<Opportunity> deals {get; set;}  
  public List<Country__c> countryCodes{get; set;}


  public DealExclusivityDialogController1() {
    oppId = ApexPages.currentPage().getParameters().get('oppId');
    Set<String> existCateSet = new Set<string>();
    if(oppId == null) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Not a valid ID')); 
    }
    else {
      try {       
          opp = [Select Id, Name, Categories__c from Opportunity where ID =: oppId];
          existcatedealList = [select Id, Name,Exclusive_Category__c,Exclusive_Category__r.Name,Deal_Name__c from Exclusive_Categories_for_Deal__c where Deal_Name__c =: opp.Id];
          for(Exclusive_Categories_for_Deal__c ecd: existcatedealList) {
              existCateSet.add(Ecd.Exclusive_Category__r.Name);
          }

      } catch(Exception e) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Not a valid ID'));          
      }     
    }

    // get all categories
    List<Category__c> categories = [SELECT Id, Name, Parent_Category__r.Id 
                                    FROM Category__c
                                    order by Name];


    List<Category__c> mainCategories = new List<Category__c>();
    Map<Id, List<Category__c>> subCategories = new Map<Id, List<Category__c>>();

    // build up main categories and sub categories
    for(Category__c category : categories) {
      if(category.Parent_Category__r != null) { // sub category
        if(subCategories.containsKey(category.Parent_Category__r.Id)) {
        List<Category__c> sCats = subCategories.get(category.Parent_Category__r.Id);
        sCats.add(category);

        subCategories.put( category.Parent_Category__r.Id, sCats);
        } else {
          subCategories.put( category.Parent_Category__r.Id, new List<Category__c>{category});
        }
      } else { // main category
        mainCategories.add(category);
      }
    }

    // get deals fields to be displayed
    Set<FieldSetMember> fields = new Set<FieldSetMember>();
    fields.addAll(Schema.getGlobalDescribe().get('Opportunity').getDescribe().fieldSets.getMap().get('Grid_Fields').fields);

    // compile all the fieldnames needed
    Set<String> field_names = new Set<String>();
    for(FieldSetMember f : fields) {
      field_names.add(f.FieldPath);    
    }

    // also add essential field names
    field_names.addAll(new Set<String>{'Id', 'Name', 'Categories__c','ContractType__c', 'Category_Name__c', 'Category_Id__c', 'Buyer__r.Name', 'Account.Name'});    

    // prepare query
    String qry = 'SELECT ' + String.join(new List<String>(field_names), ', ') + ' FROM Opportunity ';

    // get deals
    deals = Database.query(qry );

    // deals by category
    Map<Id, List<Opportunity>> deals_by_category = make_deals_by_category(deals);

    // get prefs
    List<Category_Pref__c> prefs = new List<Category_Pref__c>();
    // prefs_by_category
    Map<Id, Category_Pref__c> prefs_by_category = new Map<Id, Category_Pref__c>();

    for(Category_Pref__c pref : prefs) {
      prefs_by_category.put(pref.Category__r.Id, pref);
    }

    // build up rows
    rows = new List<rowWrapper>();

    for(Category__c mCat : mainCategories) {
        System.debug('XXXX - in the loop mainCategories:  mCat.Id = '+mCat.Id +' : mCat.Name = '+mCat.Name);
      rowWrapper row = new rowWrapper();

      row.Id = mCat.Id;
      row.Name = mCat.Name;
      row.labels = new Set<String>();
      row.riwList = new List<RowInnerWrapper>();

      if(subCategories.containsKey(mCat.Id)) {
        for(Category__c sCat : subCategories.get(mCat.Id)) {
        System.debug('XXXX - in the loop for(Category__c sCat : subCategories.get(mCat.Id))  : sCat.Name '+sCat.Name);
          // add deals
          if(deals_by_category.containsKey(sCat.Id)) {
            for(Opportunity opp: deals_by_category.get(sCat.Id))
                row.riwList.add(new RowInnerWrapper(opp, false));

            for(Opportunity deal : deals_by_category.get(sCat.Id)) {
              row.labels.add(deal.ContractType__c);
               System.debug('XXXX - in the loop for(Opportunity deal : deals_by_category.get(sCat.Id)) -- added deal to label deal.Name '+deal.Name);
            }

          } else { // blank
            Opportunity blank_deal = new Opportunity();            
            blank_deal.Category_Name__c = sCat.Name;              
            blank_deal.Category_Id__c = sCat.Id;      
            if(existCateSet.contains(sCat.Name)) {
                row.riwList.add(new RowInnerWrapper(blank_deal, true));
                row.isSelect =true;
            } else {
                row.riwList.add(new RowInnerWrapper(blank_deal, false)); 
            }
            System.debug('XXXX - ELSE blank deal : blank_deal.Category_Name__c'+blank_deal.Category_Name__c + '  : blank_deal.Category_Id__c = ' + blank_deal.Category_Id__c);  
          }         
        }            
      }

      rows.add(row);
      System.debug(' XXX -  rows.add(row)= '+ row);
    }       
  }

  public class rowWrapper {
    public Id                Id     {get; set;}
    public String            Name   {get; set;}
    public Set<String>       labels {get; set;}
    public List<RowInnerWrapper> riwList {get; set;}
    public Transient Boolean isSelect{get; set;} 

}

  public class RowInnerWrapper {
        public Boolean isSelected {get; set;}
        public Opportunity deal  {get; set;}
        public RowInnerWrapper(Opportunity opp, Boolean isSel) {
            deal = opp;
            isSelected = isSel;
        }
    }

  public Map<Id, List<Opportunity>> make_deals_by_category(List<Opportunity> deals) {

    //Map of category names and their Ids.
    Map<String, Id> mapCatName_Id = new Map<String, Id>();
    for(Category__c cat : [SELECT Id, Name FROM Category__c] ) {
        mapCatName_Id.put(cat.Name, cat.Id);
    }

    Map<Id, List<Opportunity>> deals_by_category = new Map<Id, List<Opportunity>>();

    for(Opportunity deal : deals) {

        //Split the categories to a list
        List<String> catList = new List<String>();      
        if(deal.Categories__c != null && deal.Categories__c != '' ){
            catList = deal.Categories__c.split(';');
        }
        System.debug('XXX - deal.Categories__c = '+deal.Categories__c);
        //Loop over all categories of the deal
        for(String categ : catList) {
            Id catId = mapCatName_Id.get(categ);

            //Clone the deal only for display purpose.
            Opportunity clonedDeal = deal.clone(true, true);
        clonedDeal.Category_Name__c = categ;              
        clonedDeal.Category_Id__c = catId;   

            if(deals_by_category.containsKey(catId)) {

            deals_by_category.get(catId).add(clonedDeal);
            System.debug('XXX - deals_by_category.get(catId).add(deal) = '+deal.Categories__c);

          } else {
            deals_by_category.put(catId, new List<Opportunity>{clonedDeal});
            System.debug('XXX - deals_by_category.put(catId, new List<Opportunity>{deal}) - catId = '+catId +' deal.Name = '+ deal.Name);
          }
        }
    }

    return deals_by_category;
  }

  @RemoteAction 
  global static Category_Pref__c setPreference(String categoryId, String pref) {
    Category_Pref__c p;    
    return p;
  } 
}

Attribution to: user8332

Possible Suggestion/Solution #1

Agree completely with the comments. This isnt a forum to get developers to write code for you. I will try to give you a head start.

Test classes can be very tricky at first, but in general, for tests for basic classes, you can follow just three steps. These are very basic examples, but should give you some context to look at some of the available documentation and have a base of knowledge

Create your data

Let's say your class is a controller for a VF page that lists myObject in a pageBlockTable, and allows you to do 'something' with these records. In order to test the class, you need to have some myObject__c records to work with.

myObject__c o1 = new myObject__c(name = 'test1', .......);
insert o1;
myObject__c o2 = new myObject__c(name = 'test2', .......);
insert o2;

Instantiate the class

In order to use this class and test the methods, you need to instantiate the class, that is fairly simple

myClass controller = new myClass();

Test the logic within that class

Lets assume you allow a user to remove one of the myObject__c records on the VF page from the list with a myCustomDelete method. Lets also assume you hold the list of myObjects in a controller property myObjectList.

integer listSize = controller.myObjectList.size(); 
controller.myCustomDelete();
system.assertEquals(controller.myObjectList.size(), listSize - 1);

These are very basic and simple examples but it will show you how a test class should work. I would suggest looking at the documentation and taking a stab at it yourself. When you reach a point were you are confused or have a specific question (ie 'How do I test that an object was actually created with assertions?') then people will be glad to help. As it stands people will likely ignore questions that seem to be looking for code to be written on demand for them


Attribution to: Chris Duncombe
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34939

My Block Status

My Block Content