0
public with sharing class WarehouseCalloutService {

    private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
     @future(callout=true)
    public static void runWarehouseEquipmentSync() {
        //Construct HTTP request and response
     //Http request method,Endpoint and setBody
     Http http = new Http();
     HttpRequest req = new HttpRequest();
     req.setHeader('Content-Type','application/json');
     String endpoint = WAREHOUSE_URL;
     req.setMethod('GET');
     req.setEndpoint(endpoint); 

     HTTPResponse res = http.send(req);
        system.debug('@@@@'+JSON.deserializeUntyped(res.getbody()));
     List<Object> params = (List<Object>)JSON.deserializeUntyped(res.getbody());

        List<Product2> updatedproduct =new List<product2>();
        for(Object o: params){
            Map<String,Object> mapobj = (Map<String,Object>)o;
            Product2 product = new Product2();
            product.productcode= (String)mapobj.get('_id');
            product.name=(String)mapobj.get('name');
            product.Replacement_Part__c =(Boolean)mapobj.get('replacement');
            product.Current_Inventory__c =(Integer)mapobj.get('quantity');
            product.Maintenance_Cycle__c =(Integer)mapobj.get('maintenanceperiod');
            product.Lifespan_Months__c=(Integer)mapobj.get('lifespan');
            product.Cost__c=(Integer)mapobj.get('cost');
            product.Warehouse_SKU__c=(String)mapobj.get('sku');
            updatedproduct.add(product);
        }
        upsert updatedproduct;
        system.debug('@@@'+updatedproduct);
    }
    // complete this method to make the callout (using @future) to the
    // REST endpoint and update equipment on hand.

}

public class WarehouseCalloutServiceMock implements HttpCalloutMock{

    // implement http mock callout
    public HTTPResponse respond(HTTPRequest request) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"_id":"55d66226726b611100aaf741","replacement":false,"quantity":5,"name":"Generator 1000 kW","maintenanceperiod":365,"lifespan":120,"cost":5000,"sku":"100003"}');
        response.setStatusCode(200);
        return response; 
    }
}

@isTest
private class WarehouseCalloutServiceTest {
  // implement your mock callout test here
  @isTest
    public static void WarehouseEquipmentSync() {

        WarehouseCalloutServiceMock mock = new WarehouseCalloutServiceMock();
        Test.setMock(HttpCalloutMock.class, mock);
        Test.startTest();
        WarehouseCalloutService.runWarehouseEquipmentSync();
        Test.stopTest();

    }
}

1 Answer 1

3

Always also pay attention to line numbers when errors occur. I presume this line is the source of the problem:

List<Object> params = (List<Object>)JSON.deserializeUntyped(res.getbody());

For that line to work, the JSON would need to have an array at its root i.e. be [ ... ] but you are providing an object in your mock i.e. { ... }. See e.g. http://json.org/ to get a better understanding.

As I see a reference to "superbadge" in the code I don't think it is appropriate to post any more suggestions here as the point is for you to work it out yourself.

1
  • 2
    I was going to give also an answer, but since Keith has pointed out that it's a reference to 'superbadge', all what Keith has provided I think is more than enough.. Commented Jun 23, 2018 at 11:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .