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
Jagadish LutimathJagadish Lutimath 

Can anyone help me to write a test class for this REST API call-out?This is new for me to write a test class for REST call-out.

public class AP117ACTQualtrics {
   
   @future(callout=true)
   public static void basicAuthCallout(List<Id> IDs){
     
     HttpRequest req = new HttpRequest();
     list<contact> objCon=[select firstname,lastname,email from contact limit 2];
     string endPointUrl='https://eu.qualtrics.com/API/v3/mailinglists/ML_3l39rKNHuEOT0G1/contacts';
     endPointUrl=endPointUrl;
     req.setEndpoint(endPointUrl);
     req.setMethod('GET');
     String username = 'abc@xyz.com';
     String password = '********';
     Blob headerValue = Blob.valueOf(username + ':' + password);
     String authorizationHeader = 'BASIC ' +
     EncodingUtil.base64Encode(headerValue);
     req.setHeader('Authorization', authorizationHeader);
     req.setHeader('content-type','application/json');
     req.setHeader('accept-language','en-US');
     req.setHeader('X-API-TOKEN','w6I0GZXTo12345vyudn7cA8SFBGIXvUvQhXW');
     
     
     string strFname;
     String strLname;
     String strEmail;
     string extdataref;
     string Lang;
     string strEmbededData;
     Id contactId;
     CustomerComplaint__c objContact=[Select id,OwnerId,CustomerContactName__r.FirstName,CustomerContactName__r.LastName,CustomerContactName__r.Email,CustomerContactName__r.Language__c from CustomerComplaint__c where Id=:IDs Limit 1];
     if(objContact!=null){
     contactId=objContact.id;
     strFname='{"firstName": "'+objContact.CustomerContactName__r.FirstName+'",';
     strLname='"lastName": "'+objContact.CustomerContactName__r.LastName+'",';
     extdataref='"externalDataRef":"'+objContact.Id+'",';
     Lang='"language":"'+objContact.CustomerContactName__r.Language__c+'",';
     strEmbededData='"embeddedData": {"sfId": "'+objContact.id+'","sfOwnerId": "'+objContact.OwnerId+'","sfIsDeleted": "false"},';
     strEmail='"email": "'+objContact.CustomerContactName__r.Email+'"}';
     
     }
     string body=strFname+strLname+extdataref+Lang+strEmbededData+strEmail;
     system.debug('body........'+body);
     req.setBody(body);
     Http http = new Http();
     string strConId;
     HTTPResponse res = http.send(req);
     system.debug('res ..........'+res.getBody());
    FetchContactOrDistrId objContact1= new FetchContactOrDistrId();
     objContact1=parse(res.getBody());
     system.debug('objContact.............'+objContact1);
     if(objContact1!=null ){
     strConId=objContact1.Result.id;}
     system.debug('strConId.............'+strConId);
     
     HttpRequest reqDistribution = new HttpRequest();
     string endPointDisUrl='https://eu.qualtrics.com/API/v3/distributions';
     reqDistribution.setEndpoint(endPointDisUrl);
     reqDistribution.setMethod('POST');
     String bodyDis;
     String username1 = 'abc@xyz.com';
     String password1 = '*******';
     Blob headerValue1 = Blob.valueOf(username1 + ':' + password1);
     String authorizationHeader1 = 'BASIC ' +
     EncodingUtil.base64Encode(headerValue1);
     reqDistribution.setHeader('Authorization', authorizationHeader1);
     reqDistribution.setHeader('content-type','application/json');
     reqDistribution.setHeader('accept-language','en-US');
     reqDistribution.setHeader('X-API-TOKEN','w6I0GZXTo12345vyudn7cA8SFBGIXvUvQhXW');
     
     
     bodyDis='{"action": "CreateDistribution","surveyId": "SV_bgEnDhQebQgmtmZ","mailingListId": "ML_3l39rKNHuEOT0G1","description": "ACT Closing Survey List","expirationDate": "2017-12-03 11:22:33","linkType": "Individual"}';
    reqDistribution.setBody(bodyDis);
     Http httpRes = new Http();
     HTTPResponse result = httpRes.send(reqDistribution);
     
    FetchContactOrDistrId DisResult=parse(result.getBody());
    system.debug('DisResultD.............'+DisResult);
    String strDistId;
     if(DisResult!=null){
     strDistId=DisResult.Result.id;}
     
     system.debug('End Method'+strDistId);
     
     
     HttpRequest reqSurvey = new HttpRequest();
     string endPointSurveyUrl='https://eu.qualtrics.com/API/v3/distributions/'+strDistId+'/links?surveyId=SV_bgEnDhQebQgmtmZ';
     reqSurvey.setEndpoint(endPointSurveyUrl);
     reqSurvey.setMethod('GET');
     String bodyDis1;
     String username12 = 'abc@xyz.com';
     String password12 = '*******';
     Blob headerValue12 = Blob.valueOf(username12 + ':' + password12);
     String authorizationHeader12 = 'BASIC ' +
     EncodingUtil.base64Encode(headerValue12);
     reqSurvey.setHeader('Authorization', authorizationHeader12);
     reqSurvey.setHeader('content-type','application/json');
     reqSurvey.setHeader('accept-language','en-US');
     reqSurvey.setHeader('X-API-TOKEN','w6I0GZXTo12345vyudn7cA8SFBGIXvUvQhXW');
     Http httpResp = new Http();
     HTTPResponse resultRes = httpResp.send(reqSurvey);
     system.debug('lstElements.........'+resultRes.getBody());
     
     //string resSurveyLink=objSurvey.parse(resultRes.getBody(),strConId,contactId);
   
   QualtricsJSONparser.parse(resultRes.getBody(),strConId,contactId);
     //system.debug('resSurveyLink...........'+resSurveyLink);
  
     }
     
     
     /*Distrubtion list wrapper*/
  public class Elements {
    public String contactId;
    
    public String link;
       }
    public class Meta {
    public String httpStatus;
    public String requestId;
    public String notice;
    }

    public class FetchContactOrDistrId{
     public Meta meta;
    public Result result;
   
    }
    
    public class Result {
    public String id;
  
    }
    
   
    public static FetchContactOrDistrId parse(String json) {
        return (FetchContactOrDistrId) System.JSON.deserialize(json, FetchContactOrDistrId.class);
        }
}

 
SFDC_DeveloperSFDC_Developer
Please go through this trailheadhttps://trailhead.salesforce.com/en/modules/apex_integration_services/units/apex_integration_rest_callouts, it covers everything related with REST API.