You need to sign in to do that
Don't have an account?
Coolday
Help me write the test class for this class.
public class RestResource{
public class SimpleRequest {
public string email;
public string password;
public string storeKey;
}
public class SimpleResponse {
public string jwtToken;
}
public static string getRequestSimple() {
Http h = new Http();
HttpRequest req = new HttpRequest();
SimpleRequest simpleReq = new SimpleRequest();
my_Settings__c objset = my_Settings__c.getValues('custom');
string endPoint = objset .End_Point__c;
if(Test.isRunningTest()){
simpleReq .email = 'XXXX@XXXX.com';
simpleReq .password = 'XXXXXXX';
simpleReq .storekey='msme';
} else{
simpleReq .email = objset.Username__c;
simpleReq .password = objset.Password__c;
simpleReq .storeKey = objset.Token__c;
}
req.setBody(JSON.serialize(simpleReq ));
req.setMethod('POST');
req.setEndpoint(endPoint);
req.setHeader('Content-Type', 'application/json');
HttpResponse hresp = h.send(req);
SimpleResponse wResp = (SimpleResponse) JSON.deserialize(hresp.getBody(), SimpleResponse.class);
system.debug('JWT ' + wResp.jwtToken);
return wResp.jwtToken;
}
@future(callout=true)
public static void sendRequestAsync(String payload,Id objectId, string customSettingName) {
my_Log__c ilog = my_Log__c();
ilog.Subject__c= customSettingName;
ilog.Request__c=payload;
try{
Http http = new Http();
HttpRequest request2 = new HttpRequest();
Map<String, Object> deserializedPayload = (Map<String, Object>)JSON.deserializeUntyped(payload);
String type = (String)deserializedPayload.get('type');
List<SObject> endpoint;
my_Settings__c setting = my_Settings__c .getValues(customSettingName);
if(setting != null && setting.end_point__c != null){
String username = (String)setting.username__c;
String password = (String)setting.password__c;
String endp = (String)setting.end_point__c;
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
request2.setHeader ('Authorization', authorizationHeader);
request2.setHeader ('APPKEY',(String)setting.app_key__c);
request2.setEndpoint (endp);
request2.setMethod ('POST');
request2.setHeader ('Content-Type', 'application/json; charset=utf-8');
request2.setBody(payload);
HttpResponse response = http.send(request2);
system.debug('The Response Body: '+response.getBody());
ilog.Response__c='response= '+response;
ilog.Response_Status_Code__c='status Code= '+response.getStatusCode();
ilog.Response_Body__c=response.getBody();
insert ilog;
if(response.getStatusCode() != 200){
Myexception e = new Myxception();
e.setMessage(response.toString());
HandleCustomException.LogException(e,payload,null);
}
}}catch(exception ex){
ilog.Exception__c='Exception:: '+ex.getMessage()+' Line Number::'+ ex.getLineNumber();
ilog.Response_Status_Code__c='ERROR';
insert ilog;
}
}
private class Myexception extends Exception{}
}
public class SimpleRequest {
public string email;
public string password;
public string storeKey;
}
public class SimpleResponse {
public string jwtToken;
}
public static string getRequestSimple() {
Http h = new Http();
HttpRequest req = new HttpRequest();
SimpleRequest simpleReq = new SimpleRequest();
my_Settings__c objset = my_Settings__c.getValues('custom');
string endPoint = objset .End_Point__c;
if(Test.isRunningTest()){
simpleReq .email = 'XXXX@XXXX.com';
simpleReq .password = 'XXXXXXX';
simpleReq .storekey='msme';
} else{
simpleReq .email = objset.Username__c;
simpleReq .password = objset.Password__c;
simpleReq .storeKey = objset.Token__c;
}
req.setBody(JSON.serialize(simpleReq ));
req.setMethod('POST');
req.setEndpoint(endPoint);
req.setHeader('Content-Type', 'application/json');
HttpResponse hresp = h.send(req);
SimpleResponse wResp = (SimpleResponse) JSON.deserialize(hresp.getBody(), SimpleResponse.class);
system.debug('JWT ' + wResp.jwtToken);
return wResp.jwtToken;
}
@future(callout=true)
public static void sendRequestAsync(String payload,Id objectId, string customSettingName) {
my_Log__c ilog = my_Log__c();
ilog.Subject__c= customSettingName;
ilog.Request__c=payload;
try{
Http http = new Http();
HttpRequest request2 = new HttpRequest();
Map<String, Object> deserializedPayload = (Map<String, Object>)JSON.deserializeUntyped(payload);
String type = (String)deserializedPayload.get('type');
List<SObject> endpoint;
my_Settings__c setting = my_Settings__c .getValues(customSettingName);
if(setting != null && setting.end_point__c != null){
String username = (String)setting.username__c;
String password = (String)setting.password__c;
String endp = (String)setting.end_point__c;
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
request2.setHeader ('Authorization', authorizationHeader);
request2.setHeader ('APPKEY',(String)setting.app_key__c);
request2.setEndpoint (endp);
request2.setMethod ('POST');
request2.setHeader ('Content-Type', 'application/json; charset=utf-8');
request2.setBody(payload);
HttpResponse response = http.send(request2);
system.debug('The Response Body: '+response.getBody());
ilog.Response__c='response= '+response;
ilog.Response_Status_Code__c='status Code= '+response.getStatusCode();
ilog.Response_Body__c=response.getBody();
insert ilog;
if(response.getStatusCode() != 200){
Myexception e = new Myxception();
e.setMessage(response.toString());
HandleCustomException.LogException(e,payload,null);
}
}}catch(exception ex){
ilog.Exception__c='Exception:: '+ex.getMessage()+' Line Number::'+ ex.getLineNumber();
ilog.Response_Status_Code__c='ERROR';
insert ilog;
}
}
private class Myexception extends Exception{}
}
Please include which lines of code are uncovered. The below information should help you get started.
https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
Also, kindly do not include username and password in the code on public forums. Please consider masking sensitive content.
Thank you