function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sumit dsumit d 

test class for batch fetching zendesk Contact

hi all,
i have a batch in which i am fetching zendesk contact. how to write test class for it?
My batch class is given below:-
 
public class BatchZendeskToSalesforceCon implements Database.Batchable<String>,Database.AllowsCallouts, Database.Stateful{
    
    // start method to return the list of end points of Zendesk Users
    public Iterable<String> start(Database.BatchableContext BC){
        
        List<Zendesk_Contact__c> conList = new List<Zendesk_Contact__c>();
        List<String> endPoints = new List<String>();
        // instance of class ZendeskApis to call Contacts Api
        ZendeskApis.ContactJSON2Apex firstRecords =  ZendeskApis.ZendeskContactApi();
        
        Integer totalRecords = firstRecords.count;
        if(totalRecords > 100 &&
           totalRecords > 0 &&
           firstRecords.next_page != null && 
           firstRecords.next_page != ''){
               totalRecords = totalRecords / 100;
               for(Integer i= 1; i<= totalRecords+1; i++){
                   String currentEndPoints = firstRecords.next_page;
                   currentEndPoints = currentEndPoints.substringBefore('?');
                   currentEndPoints = currentEndPoints + '?page='+i;
                   endPoints.add(currentEndPoints);
               }
           }else if(totalRecords < 100 && totalRecords > 0){
               String currentEndPoints = 'https://timerack.zendesk.com/api/v2/users.json';
               endPoints.add(currentEndPoints);
           }
        return endPoints;
        
    }
    //Batch size should be 1 only.  
    //execute method to upsert Users
    public void execute( Database.BatchableContext BC, List<String> endPoints){
        List<Zendesk_Contact__c> conList = new List<Zendesk_Contact__c>();
        for(String endPo : endPoints){
           ZendeskApis.ContactJSON2Apex ajNextRecords =  ZendeskApis.ZendeskContactNextApi(endPo);
           conList.addAll(insertContactData(ajNextRecords));
        }
        if(conList.size() > 0){
            insert conList;
        }
        
    }
    // method to insert Users Records into Contact
    public static List<Zendesk_Contact__c> insertContactData(ZendeskApis.ContactJSON2Apex conData){
        List<Zendesk_Contact__c> conList = new List<Zendesk_Contact__c>();
        //ZendeskApis.ZendeskContactApi();
        for(ZendeskApis.ContactUsers conWrapData : conData.users){
            Zendesk_Contact__c con = new Zendesk_Contact__c();
            
            if(conWrapData.id != null){
                con.Zendesk_id__c = String.valueOf(conWrapData.id);
            }
            con.Url__c = conWrapData.url;
            con.Name__c = conWrapData.name;
            con.Email__c = conWrapData.email;
            if(conWrapData.created_at != null){
                con.created_at__c = Date.valueOf(conWrapData.created_at);
            }
            if(conWrapData.updated_at != null){
                con.Updated_at__c = Date.valueOf(conWrapData.updated_at);
            }
            con.Time_zone__c     = conWrapData.time_zone;
            con.iana_time_zone__c = conWrapData.iana_time_zone;
            if(conWrapData.phone != null){
                con.Phone__c = String.valueOf(conWrapData.phone);
            }
            if(conWrapData.shared_phone_number != null){
                con.Shared_phone_number__c = Boolean.valueOf(conWrapData.shared_phone_number);
            }
            if(conWrapData.photo != null){
                con.Photo__c = String.valueOf(conWrapData.photo);
            }
            con.Locale_id__c = conWrapData.locale_id;
            con.Locale__c = conWrapData.locale;
            if(conWrapData.organization_id != null){
                con.Organization_id__c = String.valueOf(conWrapData.organization_id);
            }
            con.Role__c = conWrapData.role;
            con.verified__c = conWrapData.verified;
            con.External_id__c = conWrapData.external_id;
            if(conWrapData.tags != null){
                con.Tags__c = String.valueOf(conWrapData.tags);
            }
            con.Alias__c = conWrapData.alias;
            con.Active__c = conWrapData.active;
            con.Shared__c = conWrapData.shared;
            con.Shared_agent__c = conWrapData.shared_agent;
            if(conWrapData.last_login_at != null){
                con.last_login_at__c     = Date.valueOf(conWrapData.last_login_at);
            }
            if(String.valueOf(conWrapData.two_factor_auth_enabled) != null && 
               String.valueOf(conWrapData.two_factor_auth_enabled) != ''){
                   con.Two_factor_auth_enabled__c = conWrapData.two_factor_auth_enabled;
               }
            con.Signature__c = conWrapData.signature;
            con.Details__c   = conWrapData.details;
            con.Notes__c = conWrapData.notes;
            con.Role_type__c = conWrapData.role_type;
            con.Custom_Role_id__c = conWrapData.custom_role_id;
            con.Moderator__c = conWrapData.moderator;
            con.Ticket_restriction__c = conWrapData.ticket_restriction;
            con.Only_private_comments__c = conWrapData.only_private_comments;
            con.Restricted_agent__c = conWrapData.restricted_agent;
            con.Suspended__c = conWrapData.suspended;
            con.chat_only__c = conWrapData.chat_only;
            con.Default_group_id__c = conWrapData.default_group_id;
            con.Report_csv__c = conWrapData.report_csv;
            if(conWrapData.user_fields != null){
                con.Users_Fields__c = String.valueOf(conWrapData.user_fields);
                ZendeskApis.ContactUser_fields contactUserFields  = conWrapData.user_fields;
                con.Training_Time__c = contactUserFields.training_time;
            }
            if(!conList.Contains(con)){
                conList.add(con);
            }
            
        }
        return conList;
    }
    public void finish(Database.BatchableContext BC){
       Map<String , Zendesk_Contact__c> conEmails = new Map<String, Zendesk_Contact__c>(); 
       for(Zendesk_Contact__c zdConObj : [Select id, email__c , 
                                         Existing_Contact__c 
                                         from Zendesk_Contact__c]){
            if(zdConObj.email__c != Null){
               conEmails.put(zdConObj.email__c , zdConObj);                                     
            }                                      
       }
        List<Zendesk_Contact__c> zdContactToUpdate = new List<Zendesk_Contact__c>();
        if(conEmails.size() > 0 ){
           for(Contact conObj : [Select id, email
                                 from Contact where email IN: conEmails.keySet()]){
                if(conObj.email != Null && conEmails.containsKey(conObj.email)){
                       Zendesk_Contact__c zdCon = conEmails.get(conObj.email);
                       zdCon.Existing_Contact__c = conObj.Id;
                    if(!zdContactToUpdate.contains(zdCon)){
                      zdContactToUpdate.add(zdCon);    
                    }
                                                            
                   }                                      
               } 
        } 
        update zdContactToUpdate;
    }
    
}
Any suggestions?
 how to write its test class?
Raj VakatiRaj Vakati
Try this 
 
@isTest
private class BatchZendeskToSalesforceConTest {
 
    
    static testMethod void testEx1() {
    	 
    		Test.startTest();
           
    		Account account  = new Account(Name = 'testOrg' , Technical_Account_Manager__c = user.id,PortalId__c='55555');
    		insert account;
            Account_Zendesk__c az = new Account_Zendesk__c(Account__c=account.id);
            insert az;
            
            Contact con = new Contact(firstname = 'Test',lastname = 'Test',description='');
            insert con;
            List<Contact> partnerContacts = new List<Contact>(); 
            partnerContacts.add(con);
			List<Zendesk_Contact__c> scCList = new List<Zendesk_Contact__c>();
			for(Integer i =o ; i <10 ; i++){
            Zendesk_Contact__c zc = new Zendesk_Contact__c(Account_Zendesk__c=az.id,Contact_Id__c=con.id);
            //insert zc;
			scCList.add(zc);
			}
    		 
			 
			Database.executeBatch(new BatchZendeskToSalesforceCon(), 200);

    		
    		Test.stopTest();
            
 
        
    }
     
}

 
sumit dsumit d
HI Raj,
          i tried your code but its not giving me the coverage?
       Can you help me out with this?