Apex Integration Services - astromechanic/cheat_sheets GitHub Wiki
REST Callouts
To test REST callouts, implement HttpCalloutMock:
@IsTest
global class MyTestMock implements HttpCalloutMock {
// We can set different status codes in constructor
private Integer statusCode;
public MyTestMock(Integer statusCode) {
this.statusCode = statusCode;
}
global HTTPResponse respond(HTTPRequest request) {
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setStatusCode(statusCode);
if (statusCode == 200) {
response.setBody('{"animal":{"id":2,"name":"bear","eats":"berries, campers, adam seligman","says":"yum yum"}}');
} else if (statusCode == 404) {
response.setBody('{"error": "Animal not found"}');
}
return response;
}
}
Test class:
@IsTest
private class MyClassTest {
@IsTest
static void testSuccess() {
Test.setMock(HttpCalloutMock.class, new MyTestMock(200));
String nameResult = MyClass.getNameById(2);
System.assertEquals('bear', nameResult, 'Must be bear');
}
}
SOAP Callouts
To test SOAP callouts, implement WebServiceMock.
@IsTest
global class MyServiceMock implements WebServiceMock {
global void doInvoke(
Object stub,
Object request,
Map<String, Object> response,
String endpoint,
String soapAction,
String requestName,
String responseNS,
String responseName,
String responseType
) {
// start - specify the response you want to send
MyService.doResponse response_x = new MyService.doResponse();
response_x.return_x = new String[]{'Park 1', 'Park 2', 'Park 3'};
// end
response.put('response_x', response_x);
}
}
Unit test:
@IsTest
private class MyUnitTest {
@IsTest static void testCountry() {
Test.setMock(WebServiceMock.class, new MyServiceMock());
String countryName = 'Germany';
String[] result = ParkLocator.country(countryName);
System.assertEquals(result[0], 'Park 1', 'Should be park 1');
}
}
Apex Web Services
The base URL endpoint is: https://yourInstance.my.salesforce.com/services/apexrest/ If the web service is a part of a package: https://instance.my.salesforce.com/services/apexrest/packageNamespace/MyMethod/
Example from Trailhead:
@RestResource(urlMapping='/Cases/v1/*')
global with sharing class MyRestResource {
@HttpGet
global static Case getCaseById() {
RestRequest request = RestContext.request;
System.debug('request: ' + request);
// grab the caseId from the end of the URL
String caseId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
Case result = [SELECT CaseNumber,Subject,Status,Origin,Priority
FROM Case
WHERE Id = :caseId];
return result;
}
@HttpPost
global static ID createCase(
String subject,
String status,
String origin,
String priority,
String customerType
) {
Case thisCase = new Case(
Subject=subject,
Status=status,
Origin=origin,
Priority=priority,
Customer_Type__c = customerType);
insert thisCase;
return thisCase.Id;
}
@HttpDelete
global static void deleteCase() {
RestRequest request = RestContext.request;
String caseId = request.requestURI.substring(
request.requestURI.lastIndexOf('/')+1);
Case thisCase = [SELECT Id FROM Case WHERE Id = :caseId];
delete thisCase;
}
@HttpPut
global static ID upsertCase(
String subject,
String status,
String origin,
String priority,
String id
) {
Case thisCase = new Case(
Id=id,
Subject=subject,
Status=status,
Origin=origin,
Priority=priority);
// Match case by Id, if present.
// Otherwise, create new case.
upsert thisCase;
// Return the case ID.
return thisCase.Id;
}
@HttpPatch
global static ID updateCaseFields() {
RestRequest request = RestContext.request;
System.debug('request: ' + request);
String caseId = request.requestURI.substring(
request.requestURI.lastIndexOf('/')+1);
Case thisCase = [SELECT Id FROM Case WHERE Id = :caseId];
// Deserialize the JSON string into name-value pairs
Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring());
// Iterate through each parameter field and value
for(String fieldName : params.keySet()) {
// Set the field and value on the Case sObject
thisCase.put(fieldName, params.get(fieldName));
}
update thisCase;
return thisCase.Id;
}
}