So I've been through quite a few posts/questions already looking for enough information to have this "click" for me, but I'm still missing something. This is my first time working with RestResource outside of Trailhead. Essentially I'm getting a good-sized JSON payload delivered via a webhook to my REST service. After reading a ton of posts I figured out how to use a ResponseWrapper and classes to deserialize my JSON. Using SF Workbench's REST explorer, I can successfully send my payload and see a response returned.
Eventually, I'll need to manipulate the data and upsert(PUT) it into my DB, but I wanted to get some basic testing set up as I know it can be a pain and is essential to migrating to production.
Below is my Class and attempt at a Test Class. I guess what I'm not understanding is how I can pass my sample JSON via the test and then get code coverage. I thought I should at least be able to get a System.AssertEquals(200, res.statusCode) to work but doesn't seem to do anything... right now except run indefinitely until I abort the test. Do I need to use a mock class? Any help is greatly appreciated.
@RestResource(urlMapping='/Leads/v1/*')
global with sharing class LeadManager {
global class ResponseWrapper {
public Integer contactId;
public Integer campaignId;
}
@HttpPost
global static ResponseWrapper doPost(JSONLead lead) {
ResponseWrapper resp = new ResponseWrapper();
resp.contactId = lead.id;
resp.campaignId = lead.campaign_id;
//map a bunch more variables
return resp;
}
global class JSONLead {
global Integer id;
global Integer contact_id;
global Integer campaign_id;
global JSONContact contact;
global JSONCall call;
// define more vars
}
global class JSONContact {
global Integer id;
global String first_name;
global String last_name;
// define more vars
}
global class JSONCall {
public String occured_at;
public Integer duration;
public String recording_url;
}
}
And then my test class attempt
@IsTest
private class LeadManagerTest {
@isTest static void testDoPost() {
String testJson = '{'+
' \"lead\":{'+
' \"id\":912873,'+
' \"contact_id\":8389,'+
' \"campaign_id\":1,'+
' \"contact\":{'+
' \"id\":8389,'+
' \"first_name\":\"Homer\",'+
' \"last_name\":\"Simpson\",'+
' },'+
' \"call\":{'+
' \"occured_at\":\"2017-05-30T08:18:49.000-07:00\",'+
' \"duration\":52,'+
' \"recording_url\":\"http://linktofile.com/somefile.mp3\"'+
' }'+
' }'+
'}';
//Send the test JSON via POST and then check for a valid response. I thought I could do something like
LeadManager.ResponseWrapper respWrap = new LeadManager.doPost(testJson);
System.assert(respWrap != null);
// but I get Invalid type: LeadManager.doPost error
//also tried fooling around a bit with with RestRequest/Response but with no luck...
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/Leads/v1/';
req.httpMethod = 'POST';
RestContext.request = req;
req.requestBody = Blob.valueOf(testJson);
RestContext.response = res;
System.assertEquals(200, res.statusCode);
}
}