I wrote mock and test for callout, but I don't think it's right. I think I check wrong things with asserts and maybe call the same methods a few times, while have another ways to get 100%. Have I?
I also still don't know how to make a bulk test for my batch. Thanks for any help.
Callout:
public class CallThePepperoni {
public List<Pepperoni__c> pepperoniList;
public HttpResponse responseService;
public static HttpRequest createRequestForToken(){
HttpRequest ourRequest = new HttpRequest();
ourRequest.setBody(myTokenString);
ourRequest.setMethod('GET');
ourRequest.setEndpoint('https://myorg.salesforce.com/services/oauth2/token');
return ourRequest;
}
public static HttpRequest createRequestForService(String token){
HttpRequest finalRequest = new HttpRequest();
finalRequest.setHeader('Authorization','Bearer ' + token);
finalRequest.setHeader('Content-Type','application/json');
finalRequest.setHeader('accept','application/json');
finalRequest.setMethod('GET');
finalRequest.setEndpoint('https://myorg.salesforce.com/services/apexrest/endpoint');
return finalRequest;
}
public class OAuth2{
public String TOKEN{get;set;}
}
public List<Pepperoni> getCalloutResponseContents() {
Http ourHttp = new Http();
HttpRequest requestForToken = RequestClass.createRequestForToken('GET');
HttpResponse responseToken = ourHttp.send(requestForToken);
OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(responseToken.getbody(), OAuth2.class);
if(objAuthenticationInfo.TOKEN != null) {
HttpRequest requestForService = RequestClass.createRequestForService(objAuthenticationInfo.TOKEN);
responseService = ourHttp.send(requestForService);
pepperoniList = (List<Pepperoni>)JSON.deserialize(responseService.getBody(), List<Pepperoni>.class);
for(Pepperoni pep : pepperoniList) {
pep.Id = null;
}
return pepperoniList;
}
}
public List<ContentVersion> getContentVersionList(Id pepId){
List<ContentVersion> contentVersionList = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE FirstPublishLocationId =: pepId];
return contentVersionList;
}
public void insertCurrentContentVersion(){
for(Pepperoni temporaryP : pepperoniList) {
if(temporaryP.VersionData__c != null) {
deletePreviousFiles(temporaryP.Id);
ContentVersion currentContentVersion = new ContentVersion(
Title = temporaryP.Title__c,
PathOnClient = temporaryP.PathOnClient__c,
VersionData = EncodingUtil.base64Decode(temporaryP.VersionData__c),
FirstPublishLocationId = temporaryP.Id
);
insert currentContentVersion;
temporaryCandidate.VersionData__c = null;
update temporaryP;
}
}
}
public void deletePreviousFiles(Id pepId) {
List<ContentVersion> contentVersionList = getContentVersionList(pepId);
List<ContentDocument> contentDocumentList = new List <ContentDocument>();
for(ContentVersion temporaryContentVersion : contentVersionList){
ContentDocument thisContentDocument = [SELECT Id FROM ContentDocument WHERE Id =: temporaryContentVersion.ContentDocumentId];
contentDocumentList.add(thisContentDocument);
}
delete contentDocumentList;
}
}
Batch:
global class BatchPep implements Database.Batchable<Integer>, Database.AllowsCallouts {
CallThePepperoni callThePepperoni = new CallThePepperoni();
global Integer operationsQuantity = 1;
global Iterable<Integer> start(Database.BatchableContext BC){
List<Integer> scope = new List<Integer>();
for(integer i = 1; i <= operationsQuantity; i++){
scope.add(i);
}
return scope;
}
global void execute(Database.BatchableContext BC, List<Integer> scope){
List<Pepperoni__c> mainList = new List<Pepperoni__c>();
for(Integer i : scope){
List<Pepperoni__c> pepList = callThePepperoni.getCalloutResponseContents();
mainList.addAll(pepList);
}
upsert mainList Email__c;
callThePepperoni.insertCurrentContentVersion();
}
global void finish(Database.BatchableContext BC){
}
}
Mock:
@isTest
global class PepMock implements HttpCalloutMock {
String token;
String externalId;
public CalloutJobAdvertisementMock(String token, String externalId) {
this.token = token;
this.externalId = externalId;
}
global HTTPResponse respond(HTTPRequest request) {
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
String serialized;
if(request.getEndpoint().contains('services/oauth2/token')) {
serialized = '{"'+ token +'":"FAKE-TOKEN"}';
} else {
//Here I serialized an object from another side:
serialized = '[{"attributes":{"type":"AnotherPep__c","url":"/services/data/v44.0/sobjects/AnotherPepFromOtherSide__c/a01f600000Mji6yAAB"},"Name__c":"My","VersionData__c":"veryLoooongRealCode"}]';
}
response.setBody(serialized);
return response;
}
}
Test:
@isTest
private class CalloutPepTest {
static CallThePepperoni callThePepperoni = new CallThePepperoni();
@isTest
static void testGetCallout() {
Pepperoni__c pep = new Pepperoni__c();
insert pep;
ContentVersion cv = new Contentversion(Title = 'TestTitle', PathOnClient = 'TestPath', VersionData = EncodingUtil.base64Decode('veryLoooongRealCode'), FirstPublishLocationId = pep.Id);
List<ContentVersion> cvListTest = new List<ContentVersion>{ cv };
insert cvListTest;
String request = someFakeTokenIsHere;
Test.setMock(HttpCalloutMock.class, new CalloutJobAdvertisementMock(200, 'ACCESS_TOKEN', ''));
Test.startTest();
List<Pepperoni__c> pepList = callThePepperoni.getCalloutResponseContents();
HttpResponse response = callThePepperoni.responseService;
List<ContentVersion> cvList = callThePepperoni.getContentVersionList(pep.Id);
callThePepperoni.deletePreviousFiles(pep.Id);
//Is bulk-test for batch work like this? Or not?
Id batchJobId = Database.executeBatch(new BatchPep(), 100);
Test.stopTest();
System.assertEquals(response.getBody(), '[{"attributes":{"type":"AnotherPep__c","url":"/services/data/v44.0/sobjects/AnotherPepFromOtherSide__c/a01f600000Mji6yAAB"},"Name__c":"My","VersionData__c":"veryLoooongRealCode"}]';);
System.assertEquals(pepList.size(), 1);
System.assertEquals(cvListTest.get(0).Id, cvList.get(0).Id);
}
}
BatchPep
but in test class you are executing a diffrent bacth,BatchResume