• Abhishek Kumar 793
  • NEWBIE
  • 5 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
I have developed some code for Step 5 in the Superbadge, but appear to stuck as I run the test. I am encountering an error and attempting to determine how to configure my code to pass the test. Are there any steps I am missing? I've covered much of the superbadge, but remain stuck here. 
@isTest
global class WarehouseCalloutServiceMock implements HttpCalloutMock {
    // implement http mock callout
    global HttpResponse respond(HttpRequest request){
        
        System.assertEquals('https://th-superbadge-apex.herokuapp.com/equipment', request.getEndpoint());
        System.assertEquals('GET', request.getMethod());
        
    	// 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
    static void WarehouseEquipmentSync(){
        Test.startTest();
        // Set mock callout class 
        Test.setMock(HttpCalloutMock.class, new WarehouseCalloutServiceMock()); 
        // This causes a fake response to be sent from the class that implements HttpCalloutMock. 
        WarehouseCalloutService.runWarehouseEquipmentSync();
        Test.stopTest();        
        System.assertEquals(1, [SELECT count() FROM Product2]);        
        
    }
    
}
public with sharing class WarehouseCalloutService {

    private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
    
    // complete this method to make the callout (using @future) to the
    // REST endpoint and update equipment on hand.
    @future(callout=true)
    public static void runWarehouseEquipmentSync(){
        Http http = new Http();
		HttpRequest request = new HttpRequest();
		request.setEndpoint(WAREHOUSE_URL);
		request.setMethod('GET');
		HttpResponse response = http.send(request);
		// If the request is successful, parse the JSON response.
		if (response.getStatusCode() == 200) {
    		// Deserialize the JSON string into collections of primitive data types.
    		List<Object> equipments = (List<Object>) JSON.deserializeUntyped(response.getBody());
            List<Product2> products = new List<Product2>();
            for(Object o :  equipments){
                Map<String, Object> mapProduct = (Map<String, Object>)o;
                Product2 product = new Product2();
                product.Name = (String)mapProduct.get('name');
                product.Cost__c = (Integer)mapProduct.get('cost');
                product.Current_Inventory__c = (Integer)mapProduct.get('quantity');
                product.Maintenance_Cycle__c = (Integer)mapProduct.get('maintenanceperiod');
                product.Replacement_Part__c = (Boolean)mapProduct.get('replacement');
                product.Lifespan_Months__c = (Integer)mapProduct.get('lifespan');
                product.Warehouse_SKU__c = (String)mapProduct.get('sku');
                product.ProductCode = (String)mapProduct.get('_id');
                products.add(product);
            }
            if(products.size() > 0){
                System.debug(products);
                upsert products;
            }
		}
    }

}

Please Help!



 
I am stuck with this challenge for more than 2 days and getting this error. Please have a look at my code and let me know where i need to make a change.
Challenge Not yet complete... here's what's wrong: 
The 'MaintenanceRequest' trigger does not appear to be handling bulk operations correctly. For the positive use case of inserting and updating more than 200 records, it did not produce the expected outcome.

My helper Class : 
public class MaintenanceRequestHelper {
    
    public static void updateWorkOrders(List<Case> ClosedCaseList){
		
        List <Case> insertCaseList = new List<Case>();
        for(Case c : ClosedCaseList){
            Case newCase = new Case();
            newCase.Type = 'Routine Maintenance';
            //newCase.Subject = c.Subject;
            newCase.Status = 'New';
            newCase.Vehicle__c = c.Vehicle__c;
            newCase.Date_Reported__c = Date.today();
            newCase.Date_Due__c = date.today();
            newCase.Equipment__c = c.Equipment__c;
            insertCaseList.add(newCase);
        }
        if(insertCaseList.size()>0){
            insert insertCaseList;
        }
    }        
    
}

My Trigger : 
trigger MaintenanceRequest on Case (after update) {
    List<Case> caseList = [Select Id, Vehicle__c, Vehicle__r.Name, type, Equipment__c from Case where status = 'Closed' Limit 10];
    
    for(Case c : caseList){
        if((c.Type == 'Repair') || (c.Type == 'Routine Maintenance')){
            MaintenanceRequestHelper.updateWorkOrders(caseList);
        }    
    }
}

MaintenanceHelper Test class:
@isTest
private class MaintenanceRequestHelperTest {
	
    @isTest
    static void test_Method_one(){
        
        List<case> caseList = new List<case>();
        List<case> secondList = new List<case>();
        
        Account acc = new Account();
        acc.Name = 'Test';
        insert acc;
        
        Contact con = new Contact();
        con.FirstName = 'test';
        con.LastName = 'last';
        con.AccountId = acc.Id;
        con.Email = 'test@abc.com';
        insert con;
        
        Vehicle__c vehicle = new Vehicle__c();
        vehicle.Name = 'Ford Figo';
        insert vehicle;
        
        Product2 product = new Product2();
        product.Name = 'test';
        product.Maintenance_Cycle__c = 2;
        product.IsActive = true;
        product.Replacement_Part__c = true;
        insert product;
        
        for(Integer i = 0; i<1000; i++){
            Case maintenanceNew             = new Case();
           // maintenanceNew.Subject          = 'Other';
            maintenanceNew.Vehicle__c       = vehicle.Id;
            maintenanceNew.Product__c       = product.Id;
            maintenanceNew.ContactId        = con.Id;
            maintenanceNew.AccountId        = acc.Id;
            maintenanceNew.Type             = 'Other';
            maintenanceNew.Status           = 'New';
            maintenanceNew.Equipment__c     = product.Id;
            maintenanceNew.Date_Reported__c = Date.today();
            maintenanceNew.Date_Due__c      = Date.today();

            caseList.add(maintenanceNew); 
        }
        if(caseList.size()>0){
       		 insert caseList;
            
        }
        
        for(case cas : caseList){
            cas.Type = 'Repair';
            cas.status = 'Closed';
            secondList.add(cas);
        }
        test.startTest();
        update secondList;
        test.stopTest();
    }
}

Thanks in advance. :)