0

I have created a REST api that allows a separate system to perform a GET by sending down an external_order_number. My code then takes that number and performs a query to acquire all the fields that need to be past back in the response to that other system. My code functions correctly and sends down the correct fields. Where I'm stuck is getting it to production. I am trying to write a test case but the only examples I seem to find are for POST. My code can be found below. Can someone point me in the right direction?

@RestResource(urlMapping='/api/order')
global with sharing class REST_order_controller {

  @HttpGet
  global static void doGet (){
    
    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;
    
    System.debug('req: ' + req);
    System.debug('res: ' + res);
    
    Map<String, Object> deserialized_response;
    String external_order_number;
    List<Order> order_query;
    Map<String, String> params;
    String order_json;
    
    params = RestContext.request.params;
    System.debug('params: ' + params);
    
    external_order_number = (String)params.get('external_order_number');
    System.debug('external_order_number: ' + external_order_number);
    
    if( [select id from Order where External_Order_Number__c = :external_order_number].size() > 0 ) {
    order_query = ([SELECT 
                    OrderNumber,
                    Account.Id,
                    Account.Name,
                    Account.SalesForce_Account_ID__c,
                    (SELECT 
                     Product2.Name,
                     Product2.ProductCode, 
                     Product_Description__c,
                     SBQQ__OrderedQuantity__c,
                     Shipped_Quantity__c,
                     OrderItemNumber,
                     Product2.Gateway__c, 
                     SBQQ__RequiredBy__c, 
                     Product_Family__c
                     FROM OrderItems
                     Order BY OrderItemNumber ASC)
                    FROM Order WHERE External_Order_Number__c = :external_order_number]);
    
    System.debug('order_num get test: ' + order_query);
    
    
        OrderRecord ord = new OrderRecord();
        for(Order o : order_query)
        {
            
            
            ord.external_order_number = external_order_number;
            ord.salesforce_order_number = o.OrderNumber;
            ord.salesforce_account_id = o.Account.SalesForce_Account_ID__c;
            ord.salesforce_account_name = o.Account.Name;
            
            System.debug('ord: ' + ord);
            
            ord.shipment_details = new List<shipment_details>();
            
            for(OrderItem ol : o.OrderItems)
            {
                
                if(ol.Product2.Gateway__c == true)
                {
                    
                    List<OrderItem> items = new List<OrderItem>();
                    shipment_details sd = new shipment_details();
                    
                    sd.product_code = ol.Product2.ProductCode;
                    sd.product_short_name = ol.Product2.product_short_name__c;
                    sd.description = ol.Product_Description__c;
                    sd.quantity_ordered = Integer.valueOf(ol.SBQQ__OrderedQuantity__c);
                    
                    if(ol.Shipped_Quantity__c != null)
                    {
                        sd.quantity_shipped =  Integer.valueOf(ol.Shipped_Quantity__c);
                    }
                    else if(ol.Shipped_Quantity__c == null)
                    {
                        sd.quantity_shipped = 0;
                    }
                    sd.order_item_number = ol.OrderItemNumber;
                    
                    sd.sensor_list = new List<sensor_list>();
                    
                    System.debug('product_code: ' + sd.product_code);
                    System.debug('quantity_shipped: ' + sd.quantity_ordered);
                    System.debug('order_item_number: ' + sd.order_item_number);
                    
                    String temp_sensors_list;
                    
                    for(OrderItem olx : o.OrderItems)
                    {
                        sensor_list sl = new sensor_list();
                        
                        if(olx.SBQQ__RequiredBY__c == ol.Id && 
                           olx.Product_Family__c == 'Sensors')
                        {
                            
                            sl.sensor_product_name = olx.Product2.Name;
                            sl.sensor_product_code = olx.Product2.ProductCode;
                            
                            sd.sensor_list.add(sl);
                            System.debug('sd: ' + sd);
                        }
                    }
                    
                    ord.shipment_details.add(sd);
                    System.debug('ord: ' + ord);
                }
            }
        }
        
        order_json = JSON.serialize(ord);
        System.debug('order_json: ' + order_json);
        
        res.statusCode = 200; 
        res.responseBody = blob.valueOf(order_json);
        return;
    }
    
    else if ([select id from Order where External_Order_Number__c = :external_order_number].size() <= 0 )
    {
        
        System.debug('Error');
        
        res.statusCode = 400; 
        order_json = JSON.serialize('Order not found');
        res.responseBody = blob.valueOf(order_json);
        System.debug('order_json: ' + order_json);
        return;
    }
} 

public class OrderRecord
{
    public string external_order_number;
    public String salesforce_order_number;
    public String salesforce_account_id;
    public String salesforce_account_name;
    public List<shipment_details> shipment_details;
}

public class shipment_details
{
    public string product_code;
    public string description;
    public integer quantity_ordered;
    public integer quantity_shipped;
    public string order_item_number;
    public List<String> imei_list;
    public List<sensor_list> sensor_list;
}

public class sensor_list
{
    
    public string sensor_product_name;
    public string sensor_product_code;
}
}

I have written the following test case and seem to be getting 29% code coverage. the issue I am now facing is that the blob is returning Order not found. Thing is that order actually exist. Any ideas on what I should do next?

@isTest
private class REST_order_controller_test
{
 static testMethod void ApiControllerTest1() 
 {
     
     RestRequest req = new RestRequest();
     RestResponse res = new RestResponse();
     
     req.requestURI = '/services/apexrest/api/order';
     req.addParameter('external_order_number', '57890');
     req.httpMethod = 'GET';
  
     RestContext.request = req;
     RestContext.response= res;
     
     Test.startTest();
     REST_order_controller.doGet();
     Test.stopTest();
     
     String test_blob = res.responseBody.toString();
     
     System.debug('test_blob_raw: ' + res.responseBody);
     System.debug('test_blob: ' + test_blob);
     
 }
}

Edit: Added my test class

4
  • 2
    Have you created required test data in the test class? I don't see test data creation in your test class. Refer this example for a sample test class for Inbound REST API Jul 23 '20 at 19:32
  • Tests are isolated from your org data. Prior to calling Test.startTest() you need to insert an Account, an Order with the External_Order_Number__c field set to your 57890 value, OrderLines, etc. so that your code has some data to query and return.
    – Mark Pond
    Jul 23 '20 at 21:05
  • @HemantJain thank you for your help. I managed to get my test case working properly.
    – Richard
    Jul 23 '20 at 23:00
  • @MarkPond thank you for your help. I managed to get my test case working properly.
    – Richard
    Jul 23 '20 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.