+ Start a Discussion
Guru 91Guru 91 

Test help for utility class?

Help me with test class,

Class Name:

public without sharing class UtilityClassWithoutSharing {
static final String ERROR_MSG = 'The Opportunity you have chosen does not meet all Opportunity Validation rules.  Please update the opportunity and then re-assocaite it with the Deal.';
  
  //Method used in Deal Trigger.
  public static void updateOpp(map<id,id> oppIdDealIdMap, List<Deal__c> newDeals){
    
    list<Opportunity> opps = [select id,Deal__c from Opportunity where id in:oppIdDealIdMap.keySet()];
    list<Opportunity> oppsToUpdate = new list<Opportunity>();
    
    if(opps!=null && opps.size()>0){
      for(Opportunity opp:opps){
        if(opp.Deal__c != oppIdDealIdMap.get(opp.id)){
          opp.Deal__c = oppIdDealIdMap.get(opp.id);
          oppsToUpdate.add(opp);
        }
      }
    }
    
  
    if(oppsToUpdate!=null && oppsToUpdate.size()>0){
      Database.SaveResult[] results = Database.update(oppsToUpdate,false);
      System.debug('MPK: '+results);
      Map<Id,String> mapErrors = new Map<Id,String>();
      for(Integer index = 0 ; index < results.size(); index++){
          Database.SaveResult result = results.get(index);
          Opportunity oppty = oppsToUpdate.get(index);
          if(!result.isSuccess() && String.valueOf(result.getErrors()[0].getStatusCode()).contains('FIELD_CUSTOM_VALIDATION_EXCEPTION')){
              mapErrors.put(oppty.Deal__c,ERROR_MSG);
          }
      }
      for(Deal__c deal  : newDeals){
          if(mapErrors.containsKey(deal.id)){
              deal.addError(mapErrors.get(deal.id));
          }
      }
    }
  }
  
  //Method used in Deal Trigger.
  public static void updateOppToRemoveDealId(list<id> oppIdDealIdMap){
    
    list<Opportunity> opps = [select id,Deal__c from Opportunity where Deal__c in:oppIdDealIdMap];
    list<Opportunity> oppsToUpdate = new list<Opportunity>();
    
    if(opps!=null && opps.size()>0){
      for(Opportunity opp:opps){
          opp.Deal__c = null;
          oppsToUpdate.add(opp);
        
      }
    }
    
    if(oppsToUpdate!=null && oppsToUpdate.size()>0){
      update oppsToUpdate;
    }
  }
}