0

I need to create a Test class where a single method (doPost()) calls two Webservices in a row. The first is a REST API and the second is a SOAP API. The first API retrieve a token that I need to use in the second API call. This is my code:

public class UNITAHttpPost {
   
    public static Boolean callFromVF(String campaignId){
        System.debug('### callFromVF');
        Boolean status = false;
        List<CampaignMember> cm = [SELECT ID,ContactId, Campaign.UNITA_Workflow__c, UNITA_Id_Database___c, UNITA_Node_API_Name__c FROM CampaignMember WHERE CampaignId =:campaignId AND Campaign.UNITA_Workflow__c != null];
        if(!cm.isEmpty()){
            UNITAHttpPost.doPost(cm);
            status = true;
        }
        return status;
    }
   
    public static void doPost(List<CampaignMember> campMemb) {
        System.debug('### SIZE LIST ' + campMemb.size());       
       
        String clientId = Label.UNITA_MNGclient_id;
        String clientSecret = Label.UNITA_MNGclient_secret;
        String refreshToken = Label.UNITA_MNGrefresh_token;
        Object accessTokenRes = null;
        Object refreshTokenRes = null;
       
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://TEST?'+'client_id='+clientId+'&client_secret='+clientSecret+'&grant_type=refresh_token'+'&refresh_token='+refreshToken);
        request.setHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
        request.setMethod('POST');
       
        HttpResponse response = http.send(request);
        // Parse the JSON response
        System.debug('### STATUS ' + response.getStatusCode());
        if (response.getStatusCode() == 200) {
            // Deserialize the JSON
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            accessTokenRes = results.get('access_token');
            refreshTokenRes = results.get('refresh_token');
            System.debug('### accessToken ' + String.valueOf(accessTokenRes));
            System.debug('### refreshToken ' + String.valueOf(refreshTokenRes));
           
            webservicesTEST.APIPort mg = new  webservicesTEST.APIPort();
            List<webservicesTEST.enterWorkflowData> cAdd = new List <webservicesTEST.enterWorkflowData>();
            List<webservicesTEST.option> addOption = new List<webservicesTEST.option>();
           
            webservicesTEST.enterWorkflow enterWorkFlowService = new webservicesTEST.enterWorkflow();
            webservicesTEST.credentials credentials = new webservicesTEST.credentials();
            enterWorkFlowService.contact = cAdd;
            credentials.password = String.valueOf(accessTokenRes);
            enterWorkFlowService.credentials = credentials;
           
            CampaignMember cm;
            for(Integer i= 0; i<campMemb.size(); i++){
                cm = campMemb[i];
                String key = cm.UNITA_Node_API_Name__c;              
                if(!key.equals(cm.UNITA_Node_API_Name__c)){
                    UNITAHttpPost.doCall(cAdd, key, addOption, credentials);
                    cAdd.clear();
                }
               
                webservicesTEST.enterWorkflowData c = new webservicesTEST.enterWorkflowData();
                c.primaryKey = cm.ContactId;
                c.iddatabase = cm.UNITA_Id_Database___c;
                cAdd.add(c);
               
                if(i == (campMemb.size()-1)){
                    UNITAHttpPost.doCall(cAdd, key, addOption, credentials);
                    cAdd.clear();
                }
            }           
        } else {
            System.debug('### Could not retrieve the access token' );
            System.debug('### RESPONSE STATUS:' + response.getStatus());
            System.debug('### RESPONSE MESSAGE:' + response.getBody());     
        }
       
    }  
    public static void doCall(List<webservicesTEST.enterWorkflowData> cAdd, String key, List<webservicesTEST.option> addOption, webservicesTEST.credentials credentials){
        webservicesTEST.APIPort mg = new  webservicesTEST.APIPort();
        webservicesTEST.enterWorkflowResponse responseTest = new webservicesTEST.enterWorkflowResponse();
        responseTest.return_x = mg.enterWorkflow(cAdd, key , addOption, credentials);
        System.debug('### RESPONSETEST ' + responseTest.return_x);
       
    }
}

How can I test two different types of API in the same method?

3
  • @identigral, my question is related to how can I test a single class method where more than one API call is present; furthermore one is REST and one is Soap.
    – sdfcwolf
    Commented Nov 13, 2020 at 7:31
  • The referenced answer applies regardless of the number of callouts.
    – identigral
    Commented Nov 13, 2020 at 16:24
  • @identigral thanks for your suggestion.
    – sdfcwolf
    Commented Nov 23, 2020 at 11:09

0

Browse other questions tagged .