You need to sign in to do that
Don't have an account?
@GM
why inner clases were not covering?
The below code is not covering both RequestClass & ResponseClass. Any help?
Test Class:
@isTest
public class OpportunityDataResourceTest {
private static testMethod void doGetTest_Positive(){
Opportunity o = BacklogGeneratorTestFactory.InsertOpportunity(Date.newInstance(2015,01,01), 2, true, 100);
o = [SELECT Id,Services_Provided__c,StageName,
Signed_SOW__c,Job_Setup__c,
Expected_SOW_Signed_Date__c,
Billing_Type__c,
Business_Developer__c,
Ultimate_Client__c,
Engagement_Name1__c,AccountId
FROM Opportunity WHERE Id = :o.Id];
Test.startTest();
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/fetchOpportunities'; //Request URL
req.httpMethod = 'POST';//HTTP Request Type
OpportunityDataResource.RequestClass reqParam = new OpportunityDataResource.RequestClass();
reqParam.opportunityId = String.valueOf(o.Id);
String JsonMsg=JSON.serialize(reqParam);
req.requestBody = Blob.valueof(JsonMsg);
RestContext.request = req;
RestContext.response= res;
OpportunityDataResource.ResponseClass response = OpportunityDataResource.doGet(reqParam);
Test.stopTest();
system.assertEquals(true, response.isValid);
system.debug('obj status '+response);
}
}
Class :
@RestResource(urlMapping='/fetchOpportunities/')
global with sharing class OpportunityDataResource {
//request parameters from external system
global class RequestClass{
webservice String opportunityId;
}
//response class to send it to external system
global class ResponseClass{
webservice String serviceCode;
webservice String description;
webservice Boolean isValid;
//webservice List<Opportunity> lstOpptyToBeReturn;
}
@HttpPost
global static ResponseClass doGet(RequestClass req){
ResponseClass response = new ResponseClass();
List<Opportunity> lstOpp = new List<Opportunity>();
try{
//get the opptyId from request class
String opptyId = String.escapeSingleQuotes(req.opportunityId);
if(opptyId!=null && String.isNotBlank(opptyId.trim())){
lstOpp = [Select Id,Name from Opportunity where Oppty_ID_For_Elite__c =: opptyId];
}
//form the response if the list of opportunites are found in the system
if(!lstOpp.isEmpty() && lstOpp.Size() == 1){
response.isValid = true;
response.serviceCode = 'Success-1';
response.description = lstOpp.size()+' '+'opportunities found with Id - '+opptyId;
return response;
}
//form the response if the list of opportunites are found in the system
else if(lstOpp.isEmpty()){
response.isValid = false;
response.serviceCode = 'Error-1';
response.description = 'No opportunity found with Id - '+opptyId;
return response;
}
//form the response if the list of opportunites are not found with list error
else{
response.isValid = false;
response.serviceCode = 'Error-2';
response.description = 'No opportunity found.Please Contact salesforce system admin';
return response;
}
}
//exception handling for SOQL query
Catch(System.QueryException e){
response.serviceCode = 'Error-1';
response.description = 'Query exception : '+e+' found.Please Contact system admin';
return response;
}
//exception handling other than SOQL
Catch(Exception e){
response.serviceCode = 'Error-2';
response.description = 'Exception : '+e+' occured.Please Contact system admin';
return response;
}
}
}
Test Class:
@isTest
public class OpportunityDataResourceTest {
private static testMethod void doGetTest_Positive(){
Opportunity o = BacklogGeneratorTestFactory.InsertOpportunity(Date.newInstance(2015,01,01), 2, true, 100);
o = [SELECT Id,Services_Provided__c,StageName,
Signed_SOW__c,Job_Setup__c,
Expected_SOW_Signed_Date__c,
Billing_Type__c,
Business_Developer__c,
Ultimate_Client__c,
Engagement_Name1__c,AccountId
FROM Opportunity WHERE Id = :o.Id];
Test.startTest();
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/fetchOpportunities'; //Request URL
req.httpMethod = 'POST';//HTTP Request Type
OpportunityDataResource.RequestClass reqParam = new OpportunityDataResource.RequestClass();
reqParam.opportunityId = String.valueOf(o.Id);
String JsonMsg=JSON.serialize(reqParam);
req.requestBody = Blob.valueof(JsonMsg);
RestContext.request = req;
RestContext.response= res;
OpportunityDataResource.ResponseClass response = OpportunityDataResource.doGet(reqParam);
Test.stopTest();
system.assertEquals(true, response.isValid);
system.debug('obj status '+response);
}
}
Class :
@RestResource(urlMapping='/fetchOpportunities/')
global with sharing class OpportunityDataResource {
//request parameters from external system
global class RequestClass{
webservice String opportunityId;
}
//response class to send it to external system
global class ResponseClass{
webservice String serviceCode;
webservice String description;
webservice Boolean isValid;
//webservice List<Opportunity> lstOpptyToBeReturn;
}
@HttpPost
global static ResponseClass doGet(RequestClass req){
ResponseClass response = new ResponseClass();
List<Opportunity> lstOpp = new List<Opportunity>();
try{
//get the opptyId from request class
String opptyId = String.escapeSingleQuotes(req.opportunityId);
if(opptyId!=null && String.isNotBlank(opptyId.trim())){
lstOpp = [Select Id,Name from Opportunity where Oppty_ID_For_Elite__c =: opptyId];
}
//form the response if the list of opportunites are found in the system
if(!lstOpp.isEmpty() && lstOpp.Size() == 1){
response.isValid = true;
response.serviceCode = 'Success-1';
response.description = lstOpp.size()+' '+'opportunities found with Id - '+opptyId;
return response;
}
//form the response if the list of opportunites are found in the system
else if(lstOpp.isEmpty()){
response.isValid = false;
response.serviceCode = 'Error-1';
response.description = 'No opportunity found with Id - '+opptyId;
return response;
}
//form the response if the list of opportunites are not found with list error
else{
response.isValid = false;
response.serviceCode = 'Error-2';
response.description = 'No opportunity found.Please Contact salesforce system admin';
return response;
}
}
//exception handling for SOQL query
Catch(System.QueryException e){
response.serviceCode = 'Error-1';
response.description = 'Query exception : '+e+' found.Please Contact system admin';
return response;
}
//exception handling other than SOQL
Catch(Exception e){
response.serviceCode = 'Error-2';
response.description = 'Exception : '+e+' occured.Please Contact system admin';
return response;
}
}
}
public String getFormattedFechaDeEntrega(){
Date fechaFirmaCompraEntrega1 = [SELECT fecha_entrega_2__c FROM Parcialidad__c WHERE Oportunidad__c = :opp.Id and Tipo__c ='Saldo'].fecha_entrega_2__c;
FormattedDate fd = new FormattedDate( fechaFirmaCompraEntrega1 , 'es-MX', '#dd# de #mm# del #yyyy#');
return fd.getCompleteFormattedDate();
}