You need to sign in to do that
Don't have an account?
Prasanth R
apex specialist superbadge (challenge 5)
I'm not able to clear the challenge ,kindly help me to pass the challenge .Below i adding my code
warehousecalloutservice:
public with sharing class WarehouseCalloutService implements Queueable, Database.AllowsCallouts {
private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
public static void runWarehouseEquipmentSync(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setMethod('GET');
request.setEndpoint(WAREHOUSE_URL);
HttpResponse response = http.send(request);
if(response.getStatusCode() == 200) {
List<Object> jsonResponse = (List<Object>)JSON.deserializeUntyped(response.getBody());
system.debug('~~ '+jsonResponse);
List<Product2> productList = new List<Product2>();
for(Object ob : jsonResponse) {
Map<String,Object> mapJson = (Map<String,Object>)ob;
Product2 pr = new Product2();
pr.Replacement_Part__c = (Boolean)mapJson.get('replacement');
pr.Name = (String)mapJson.get('name');
pr.Maintenance_Cycle__c = (Integer)mapJson.get('maintenanceperiod');
pr.Lifespan_Months__c = (Integer)mapJson.get('lifespan');
pr.Cost__c = (Decimal) mapJson.get('lifespan');
pr.Warehouse_SKU__c = (String)mapJson.get('sku');
pr.Current_Inventory__c = (Double) mapJson.get('quantity');
productList.add(pr);
}
if(productList.size()>0)
upsert productList;
}
}
}
warehousecalloutserviceMock:
@isTest
global class WarehouseCalloutServiceMock implements HttpCalloutMock {
// implement http mock callout
global static 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;
}
}
warehousecalloutservicetest
@isTest
private class WarehouseCalloutServiceTest {
@isTest
static void testWareHouseCallout(){
Test.startTest();
// implement mock callout test here
Test.setMock(HTTPCalloutMock.class, new WarehouseCalloutServiceMock());
WarehouseCalloutService.runWarehouseEquipmentSync();
System.enqueueJob(new WarehouseCalloutService());
Test.stopTest();
System.assertEquals(1, [SELECT count() FROM Product2]);
}
}
warehousecalloutservice:
public with sharing class WarehouseCalloutService implements Queueable, Database.AllowsCallouts {
private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
public static void runWarehouseEquipmentSync(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setMethod('GET');
request.setEndpoint(WAREHOUSE_URL);
HttpResponse response = http.send(request);
if(response.getStatusCode() == 200) {
List<Object> jsonResponse = (List<Object>)JSON.deserializeUntyped(response.getBody());
system.debug('~~ '+jsonResponse);
List<Product2> productList = new List<Product2>();
for(Object ob : jsonResponse) {
Map<String,Object> mapJson = (Map<String,Object>)ob;
Product2 pr = new Product2();
pr.Replacement_Part__c = (Boolean)mapJson.get('replacement');
pr.Name = (String)mapJson.get('name');
pr.Maintenance_Cycle__c = (Integer)mapJson.get('maintenanceperiod');
pr.Lifespan_Months__c = (Integer)mapJson.get('lifespan');
pr.Cost__c = (Decimal) mapJson.get('lifespan');
pr.Warehouse_SKU__c = (String)mapJson.get('sku');
pr.Current_Inventory__c = (Double) mapJson.get('quantity');
productList.add(pr);
}
if(productList.size()>0)
upsert productList;
}
}
}
warehousecalloutserviceMock:
@isTest
global class WarehouseCalloutServiceMock implements HttpCalloutMock {
// implement http mock callout
global static 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;
}
}
warehousecalloutservicetest
@isTest
private class WarehouseCalloutServiceTest {
@isTest
static void testWareHouseCallout(){
Test.startTest();
// implement mock callout test here
Test.setMock(HTTPCalloutMock.class, new WarehouseCalloutServiceMock());
WarehouseCalloutService.runWarehouseEquipmentSync();
System.enqueueJob(new WarehouseCalloutService());
Test.stopTest();
System.assertEquals(1, [SELECT count() FROM Product2]);
}
}
Please note that Questions about how to pass Trailhead challenges are not on topic, because these challenges are intended to be independent demonstrations of your abilities.
Trailhead Help (https://trailhead.salesforce.com/en/help?support=home) can provide assistance for situations where Trailhead does not appear to be functioning correctly.
You can reach out to them if this is the case.Hope above information helps.
Please mark as Best Answer so that it can help others in future.
Thanks,