You need to sign in to do that
Don't have an account?
Code+1
Help required in test class code coverage - Rest
Hi Team,
I need help in building test class for the below Apex Class
@RestResource(urlMapping='/Cortx/CancelOrderService/*')
global class CortxCancelOrderService {
@HttpPost
global static List<WebResponse> cancelOrders(List<OrderDetail> lstOrderDetail) {
Integration_Log__c LogObj = new Integration_Log__c(Request__c = JSON.serialize(lstOrderDetail));
insert LogObj;
Set<String> set_OrderIds = new Set<String>();
LogObj = [Select Id, Name, Response__c from Integration_Log__c WHERE Id=:LogObj.Id];
try{
List<WebResponse> Response = new List<WebResponse>();
List<Order> lst_Order = new List<Order>();
Schema.DescribeSObjectResult r = Order.sObjectType.getDescribe();
String keyPrefix = r.getKeyPrefix();
for(OrderDetail var : lstOrderDetail){
if(var.Order_Id != null && var.Order_Id.startsWith(keyPrefix) && (var.Order_Id.length() == 15 || var.Order_Id.length() == 18)){
set_OrderIds.add(var.Order_Id);
}
}
Map<Id, Order> map_Orders = new Map<Id, Order>([Select Id, Status, Cancel_Reason__c from Order Where Id IN:set_OrderIds]);
List<Services_and_Equipment__c> lst_SE = [ Select Id, Order__c, Cortx_Service_Id__c from Services_and_Equipment__c WHERE Order__c != null AND Order__c IN:map_Orders.keySet()];
Map<Id, List<Services_and_Equipment__c>> map_Order2lstSE = new Map<Id, List<Services_and_Equipment__c>> ();
for(Services_and_Equipment__c var : lst_SE ){
if(map_Order2lstSE.containsKey(var.Order__c)){
map_Order2lstSE.get(var.order__c).add(var);
}else{
List<Services_and_Equipment__c> lst_tmp = new List<Services_and_Equipment__c>{var};
map_Order2lstSE.put(var.order__c, lst_tmp);
}
}
List<Services_and_Equipment__c> lst_SE_ToUpdate = new List<Services_and_Equipment__c>();
Set<String> OrderIdsToUpdate = new Set<String>();
for(OrderDetail var : lstOrderDetail){
if(var.Order_Id != null && var.Order_Id.startsWith(keyPrefix)){
if(var.Order_Id.length() == 18 && map_Orders.containsKey(var.Order_Id)){
// Here We need to put data validation before update the Order status as Cancelled
if(var.CancelReason != null){
Order Order_Obj = new Order(Id=var.Order_Id, Cancel_Reason__c = var.CancelReason, status='Cancelled');
for(Services_and_Equipment__c se_var : map_Order2lstSE.get(var.Order_Id)){
se_var.Order__c = null;
se_var.Cortx_Service_Id__c = null;
lst_SE_ToUpdate.add(se_var);
}
OrderIdsToUpdate.add(var.Order_Id);
lst_Order.add(Order_Obj);
}else{
WebResponse Response_Obj = new WebResponse();
Response_Obj.Order_Id = var.Order_Id;
String ErrorMsg = 'CancelReaon is mandatory to cancel Order "'+ var.Order_Id ;
Response_Obj.Status = 'Failure';
Response_Obj.StatusDescription = ErrorMsg;
Response.add(Response_Obj);
}
}else{
WebResponse Response_Obj = new WebResponse();
Response_Obj.Order_Id = var.Order_Id;
String ErrorMsg = 'Order Record with Id "'+ var.Order_Id +'" not found in System';
Response_Obj.Status = 'Failure';
Response_Obj.StatusDescription = ErrorMsg;
Response.add(Response_Obj);
}
}else{
WebResponse Response_Obj = new WebResponse();
Response_Obj.Order_Id = var.Order_Id;
String ErrorMsg = 'Order_Id field can not be null';
Response_Obj.Status = 'Failure';
Response_Obj.StatusDescription = ErrorMsg;
Response.add(Response_Obj);
}
}
Database.SaveResult[] SR_OrderUpdate = Database.update(lst_Order, false);
Database.SaveResult[] SR_SE_Update = Database.update(lst_SE_ToUpdate, false);
for (Database.SaveResult sr : SR_OrderUpdate ) {
WebResponse Response_Obj = new WebResponse();
if (sr.isSuccess()) {
Response_Obj.Order_Id = sr.getId();
Response_Obj.Status = 'Success';
Response_Obj.StatusDescription = 'Order cancelled successfully. SFDC log number - '+ LogObj.Name;
}
else {
Response_Obj.Order_Id = sr.getId();
String ErrorMsg = 'The following error has occurred.';
for(Database.Error err : sr.getErrors()) {
ErrorMsg = ErrorMsg +' '+ err.getStatusCode() + ': ' + err.getMessage();
}
Response_Obj.Status = 'Failure';
Response_Obj.StatusDescription = ErrorMsg + ' SFDC log number - '+ LogObj.Name;
}
Response.add(Response_Obj);
}
LogObj.Response__c = JSON.serialize(Response);
LogObj.Is_Success__c = true;
update LogObj;
System.debug('--------WebResponse---- '+ Response);
return Response;
}catch(Exception ex){
WebResponse ExceptionResponse_Obj = new WebResponse();
ExceptionResponse_Obj.Status = 'Failure';
ExceptionResponse_Obj.StatusDescription = ex.getMessage();
LogObj.Response__c = JSON.serialize(ExceptionResponse_Obj);
LogObj.Is_Success__c = false;
update LogObj;
System.debug('--------WebResponse---- '+ ExceptionResponse_Obj);
return new List<WebResponse>{ExceptionResponse_Obj};
}
}
global class OrderDetail{
global String Order_Id; // SFDC Order Id 18 digit
global String CancelReason;
global OrderDetail(){}
}
global class WebResponse{
global String Order_Id{get;set;} // SFDC Order Id 18 digit
global String Status{get;set;} // Success or Failure
global String StatusDescription{get;set;} // Detail of Failure, In case of success it will have default value “Record Updated Successfully”
}
}
I need help in building test class for the below Apex Class
@RestResource(urlMapping='/Cortx/CancelOrderService/*')
global class CortxCancelOrderService {
@HttpPost
global static List<WebResponse> cancelOrders(List<OrderDetail> lstOrderDetail) {
Integration_Log__c LogObj = new Integration_Log__c(Request__c = JSON.serialize(lstOrderDetail));
insert LogObj;
Set<String> set_OrderIds = new Set<String>();
LogObj = [Select Id, Name, Response__c from Integration_Log__c WHERE Id=:LogObj.Id];
try{
List<WebResponse> Response = new List<WebResponse>();
List<Order> lst_Order = new List<Order>();
Schema.DescribeSObjectResult r = Order.sObjectType.getDescribe();
String keyPrefix = r.getKeyPrefix();
for(OrderDetail var : lstOrderDetail){
if(var.Order_Id != null && var.Order_Id.startsWith(keyPrefix) && (var.Order_Id.length() == 15 || var.Order_Id.length() == 18)){
set_OrderIds.add(var.Order_Id);
}
}
Map<Id, Order> map_Orders = new Map<Id, Order>([Select Id, Status, Cancel_Reason__c from Order Where Id IN:set_OrderIds]);
List<Services_and_Equipment__c> lst_SE = [ Select Id, Order__c, Cortx_Service_Id__c from Services_and_Equipment__c WHERE Order__c != null AND Order__c IN:map_Orders.keySet()];
Map<Id, List<Services_and_Equipment__c>> map_Order2lstSE = new Map<Id, List<Services_and_Equipment__c>> ();
for(Services_and_Equipment__c var : lst_SE ){
if(map_Order2lstSE.containsKey(var.Order__c)){
map_Order2lstSE.get(var.order__c).add(var);
}else{
List<Services_and_Equipment__c> lst_tmp = new List<Services_and_Equipment__c>{var};
map_Order2lstSE.put(var.order__c, lst_tmp);
}
}
List<Services_and_Equipment__c> lst_SE_ToUpdate = new List<Services_and_Equipment__c>();
Set<String> OrderIdsToUpdate = new Set<String>();
for(OrderDetail var : lstOrderDetail){
if(var.Order_Id != null && var.Order_Id.startsWith(keyPrefix)){
if(var.Order_Id.length() == 18 && map_Orders.containsKey(var.Order_Id)){
// Here We need to put data validation before update the Order status as Cancelled
if(var.CancelReason != null){
Order Order_Obj = new Order(Id=var.Order_Id, Cancel_Reason__c = var.CancelReason, status='Cancelled');
for(Services_and_Equipment__c se_var : map_Order2lstSE.get(var.Order_Id)){
se_var.Order__c = null;
se_var.Cortx_Service_Id__c = null;
lst_SE_ToUpdate.add(se_var);
}
OrderIdsToUpdate.add(var.Order_Id);
lst_Order.add(Order_Obj);
}else{
WebResponse Response_Obj = new WebResponse();
Response_Obj.Order_Id = var.Order_Id;
String ErrorMsg = 'CancelReaon is mandatory to cancel Order "'+ var.Order_Id ;
Response_Obj.Status = 'Failure';
Response_Obj.StatusDescription = ErrorMsg;
Response.add(Response_Obj);
}
}else{
WebResponse Response_Obj = new WebResponse();
Response_Obj.Order_Id = var.Order_Id;
String ErrorMsg = 'Order Record with Id "'+ var.Order_Id +'" not found in System';
Response_Obj.Status = 'Failure';
Response_Obj.StatusDescription = ErrorMsg;
Response.add(Response_Obj);
}
}else{
WebResponse Response_Obj = new WebResponse();
Response_Obj.Order_Id = var.Order_Id;
String ErrorMsg = 'Order_Id field can not be null';
Response_Obj.Status = 'Failure';
Response_Obj.StatusDescription = ErrorMsg;
Response.add(Response_Obj);
}
}
Database.SaveResult[] SR_OrderUpdate = Database.update(lst_Order, false);
Database.SaveResult[] SR_SE_Update = Database.update(lst_SE_ToUpdate, false);
for (Database.SaveResult sr : SR_OrderUpdate ) {
WebResponse Response_Obj = new WebResponse();
if (sr.isSuccess()) {
Response_Obj.Order_Id = sr.getId();
Response_Obj.Status = 'Success';
Response_Obj.StatusDescription = 'Order cancelled successfully. SFDC log number - '+ LogObj.Name;
}
else {
Response_Obj.Order_Id = sr.getId();
String ErrorMsg = 'The following error has occurred.';
for(Database.Error err : sr.getErrors()) {
ErrorMsg = ErrorMsg +' '+ err.getStatusCode() + ': ' + err.getMessage();
}
Response_Obj.Status = 'Failure';
Response_Obj.StatusDescription = ErrorMsg + ' SFDC log number - '+ LogObj.Name;
}
Response.add(Response_Obj);
}
LogObj.Response__c = JSON.serialize(Response);
LogObj.Is_Success__c = true;
update LogObj;
System.debug('--------WebResponse---- '+ Response);
return Response;
}catch(Exception ex){
WebResponse ExceptionResponse_Obj = new WebResponse();
ExceptionResponse_Obj.Status = 'Failure';
ExceptionResponse_Obj.StatusDescription = ex.getMessage();
LogObj.Response__c = JSON.serialize(ExceptionResponse_Obj);
LogObj.Is_Success__c = false;
update LogObj;
System.debug('--------WebResponse---- '+ ExceptionResponse_Obj);
return new List<WebResponse>{ExceptionResponse_Obj};
}
}
global class OrderDetail{
global String Order_Id; // SFDC Order Id 18 digit
global String CancelReason;
global OrderDetail(){}
}
global class WebResponse{
global String Order_Id{get;set;} // SFDC Order Id 18 digit
global String Status{get;set;} // Success or Failure
global String StatusDescription{get;set;} // Detail of Failure, In case of success it will have default value “Record Updated Successfully”
}
}
http://cloudyworlds.blogspot.in/2012/12/writing-test-classes-for-apex-rest.html