You need to sign in to do that
Don't have an account?
Ashutosh Tripathi 66
Help on the test class
Need to write a test class for the below REST class.
public class callApi { public String Response; public String apiKey; List<String> prospectValues = new List<String>(); public PardotProspectWrapper wrapper {get; set;} public List<PardotProspectWrapper> wrapperList {get;set;} public List<Pardot_Prospect__c> ppobjlst {get;set;} //getting values from customSetting SFtoPardot__c pdcred = SFtoPardot__c.getInstance('PardotCredentials'); String email = pdcred.PardotUserName__c; String password = pdcred.PardotPassword__c; String userKey = pdcred.API_User_Key__c; String CreatedAfter = pdcred.Created_After__c; String CreatedBefore = pdcred.Created_Before__c; //Declaring wrapper for custom object Pardot_Prospect__c Pardot_Prospect__c PP = new Pardot_Prospect__c(); //Authenticating Pardot with Salesforce public void Auth() { HttpRequest req = new HttpRequest(); req.setEndpoint( 'https://pi.pardot.com/api/login/version/4' ); req.setMethod( 'POST' ); req.setBody( 'email=' + email + '&password=' + password + '&user_key=' + userKey ); HttpResponse res = new Http().send( req ); String response = res.getBody(); Integer startIdx = response.indexOf( '<api_key>' ) + 9; Integer endIdx = response.indexOf( '</api_key>' ); String apiKey = response.substring( startIdx, endIdx ); System.debug('******'+apiKey ); getAndParse(apiKey,userKey); } //Getting and Parsing the response received from Pardot public void getAndParse(String apiKey, String userKey){ HttpRequest req = new HttpRequest(); HttpResponse res = new HttpResponse(); req.setEndpoint( 'https://pi.pardot.com/api/prospect/version/4/do/query?user_key='+userKey+'&'+'api_key='+apiKey+'&'+'output=bulk&format=json&created_after='+CreatedAfter+'&'+'created_before='+CreatedBefore+'&'+'sort_by=created_at&sort_order=ascending'); req.setMethod( 'GET' ); req.setBody( 'user_key='+userKey+'&'+'api_key='+apiKey); req.setHeader('Accept', 'application/json '); req.setHeader('Content-Type','application/json'); res = new Http().send( req ); System.debug('***Response****'+res.getBody()); wrapper = (PardotProspectWrapper)JSON.deserialize(res.getBody(), PardotProspectWrapper.class); system.debug('******Wrapper*****'+wrapper); //this I tried to add to store the data in Pardot_Prospect__c ppobjlst = new List<Pardot_Prospect__c>(); Pardot_Prospect__c ppobj = new Pardot_Prospect__c(); for (PardotProspectWrapper.prospect p : wrapper.result.prospect) { ppobj = new Pardot_Prospect__c(); ppobj.Prospect_Id__c = string.valueOf(p.id); ppobj.First_name__c = p.first_name; ppobj.Last_Name__c = p.last_name; ppobj.Company__c = p.company; ppobj.Grade__c = p.grade; ppobj.Score__c = p.score; ppobjlst.add(ppobj); } insert ppobjlst; } }
First Create HTTPMock test class and create your required resopnse like below.
@isTest
global class HttpCalloutMockTest implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
System.assertEquals('POST', req.getMethod());
// Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"success":"true"}');
res.setStatusCode(200);
return res;
}
}
Use below line in your actual Test class
Test.setMock(HttpCalloutMock.class, new <<yourClassName>>());
Hope this will helps you.
You need to insert the SFtoPardot__c values also then only it wlll work