Advertisement
Learnify_Rectify

Test callout logic

Jul 11th, 2024
10,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | Source Code | 0 0
  1. Test callout logic from (Superbadge - Apex Specialist)
  2.  
  3.  
  4. ----------------------------------------------------------------------------------------------------
  5. SOURCE CODE1: WarehouseCalloutService
  6.  
  7. public with sharing class WarehouseCalloutService {
  8.  
  9. private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
  10.  
  11. //@future(callout=true)
  12. public static void runWarehouseEquipmentSync(){
  13.  
  14. Http http = new Http();
  15. HttpRequest request = new HttpRequest();
  16.  
  17. request.setEndpoint(WAREHOUSE_URL);
  18. request.setMethod('GET');
  19. HttpResponse response = http.send(request);
  20.  
  21.  
  22. List<Product2> warehouseEq = new List<Product2>();
  23.  
  24. if (response.getStatusCode() == 200){
  25. List<Object> jsonResponse = (List<Object>)JSON.deserializeUntyped(response.getBody());
  26. System.debug(response.getBody());
  27.  
  28. for (Object eq : jsonResponse){
  29. Map<String,Object> mapJson = (Map<String,Object>)eq;
  30. Product2 myEq = new Product2();
  31. myEq.Replacement_Part__c = (Boolean) mapJson.get('replacement');
  32. myEq.Name = (String) mapJson.get('name');
  33. myEq.Maintenance_Cycle__c = (Integer) mapJson.get('maintenanceperiod');
  34. myEq.Lifespan_Months__c = (Integer) mapJson.get('lifespan');
  35. myEq.Cost__c = (Decimal) mapJson.get('lifespan');
  36. myEq.Warehouse_SKU__c = (String) mapJson.get('sku');
  37. myEq.Current_Inventory__c = (Double) mapJson.get('quantity');
  38. warehouseEq.add(myEq);
  39. }
  40.  
  41. if (warehouseEq.size() > 0){
  42. upsert warehouseEq;
  43. System.debug('Your equipment was synced with the warehouse one');
  44. System.debug(warehouseEq);
  45. }
  46.  
  47. }
  48. }
  49. }
  50.  
  51. -------------------------------------------------------------------------------------
  52. SOURCE CODE2 : WarehouseCalloutServiceTest
  53.  
  54. @isTest
  55.  
  56. private class WarehouseCalloutServiceTest {
  57. @isTest
  58. static void testWareHouseCallout(){
  59. Test.startTest();
  60. // implement mock callout test here
  61. Test.setMock(HTTPCalloutMock.class, new WarehouseCalloutServiceMock());
  62. WarehouseCalloutService.runWarehouseEquipmentSync();
  63. Test.stopTest();
  64. System.assertEquals(1, [SELECT count() FROM Product2]);
  65. }
  66. }
  67.  
  68. ------------------------------------------------------------------------------
  69.  
  70. SOURCE CODE3 : WarehouseCalloutServiceMock
  71.  
  72. @isTest
  73. global class WarehouseCalloutServiceMock implements HttpCalloutMock {
  74. // implement http mock callout
  75. global static HttpResponse respond(HttpRequest request){
  76.  
  77. System.assertEquals('https://th-superbadge-apex.herokuapp.com/equipment', request.getEndpoint());
  78. System.assertEquals('GET', request.getMethod());
  79.  
  80. // Create a fake response
  81. HttpResponse response = new HttpResponse();
  82. response.setHeader('Content-Type', 'application/json');
  83. response.setBody('[{"_id":"55d66226726b611100aaf741","replacement":false,"quantity":5,"name":"Generator 1000 kW","maintenanceperiod":365,"lifespan":120,"cost":5000,"sku":"100003"}]');
  84. response.setStatusCode(200);
  85. return response;
  86. }
  87. }
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement