@isTest public with sharing class UpdateCollectionAndCollectsFromShop_Test { @isTest static void testUpdateCollectAndCollection(){ //CustomSettingRecordInsert.InsertingFieldMapping(); CustomSettingRecordInsert.InsertingConfiguration(); CustomSettingRecordInsert.InsertingWebhook(); HIC_ShopifySync__Field_Mapping__c sfField = new HIC_ShopifySync__Field_Mapping__c(); sfField.name = 'Collection__c-Name'; sfField.HIC_ShopifySync__SF_Field__c = 'Name'; sfField.HIC_ShopifySync__Shopify_Fields__c = 'title'; sfField.HIC_ShopifySync__Nested__c = ''; sfField.HIC_ShopifySync__IsSFID__c = false; sfField.HIC_ShopifySync__Default_Value__c = false; sfField.HIC_ShopifySync__Object_Name__c = 'HIC_ShopifySync__Collection__c'; sfField.HIC_ShopifySync__SF_Field_Type__c ='STRING'; sfField.HIC_ShopifySync__Shopify_Field_Type__c ='string'; sfField.HIC_ShopifySync__External_ID__c ='HIC_ShopifySync__Collection__c-Name'; insert sfField; /**********/ List shopLst = new List(); HIC_ShopifySync__Collection__c hsc = new HIC_ShopifySync__Collection__c(); shopLst.add(hsc); insert shopLst; /**********/ Restrequest req = new Restrequest(); req.requestBody = Blob.valueOf('{"id":109189562432,"handle":"collection5","title":"collection555555555555555","updated_at":"2019-01-21T02:10:06-12:00","body_html":"","published_at":"2019-01-18T02:22:51-12:00","sort_order":"best-selling","template_suffix":"","disjunctive":false,"rules":[{"column":"variant_inventory","relation":"greater_than","condition":"0"}],"published_scope":"web"}'); Restresponse res = new Restresponse(); req.requestURI = 'https://hic-sf-shopify-developer-edition.ap15.force.com//services/apexrest/UpdateCollectionAndCollectsFromShopify'; req.httpMethod = 'POST'; RestContext.request = req; RestContext.response= res; res.statusCode = 200; Test.startTest(); UpdateCollectionAndCollectsFromShopify.updateCollectionAndCollects() ; // UpdateCollectionAndCollectsFromShopify.InsertCollect(); system.assertequals(200, res.statuscode); Test.stopTest(); } @isTest static void testUpdateCollectAndCollectionAuthentication(){ Restrequest req = new Restrequest(); req.requestBody = Blob.valueOf('{"id":109189562432,"handle":"collection5","title":"collection555555555555555","updated_at":"2019-01-21T02:10:06-12:00","body_html":"","published_at":"2019-01-18T02:22:51-12:00","sort_order":"best-selling","template_suffix":"","disjunctive":false,"rules":[{"column":"variant_inventory","relation":"greater_than","condition":"0"}],"published_scope":"web"}'); Restresponse res = new Restresponse(); req.requestURI = 'https://hic-sf-shopify-developer-edition.ap15.force.com//services/apexrest/UpdateCollectionAndCollectAuthentication'; req.httpMethod = 'POST'; RestContext.request = req; RestContext.response= res; res.statusCode = 200; Test.startTest(); UpdateCollectionAndCollectAuthentication.updateCollectionAndCollectsWithAuthentication() ; system.assertequals(200, res.statuscode); Test.stopTest(); } @isTest static void testUpdateCollectAndCollection1(){ //CustomSettingRecordInsert.InsertingFieldMapping(); CustomSettingRecordInsert.InsertingConfiguration(); CustomSettingRecordInsert.InsertingWebhook(); HIC_ShopifySync__Field_Mapping__c sfField = new HIC_ShopifySync__Field_Mapping__c(); sfField.name = 'Collection__c-Name'; sfField.HIC_ShopifySync__SF_Field__c = 'Name'; sfField.HIC_ShopifySync__Shopify_Fields__c = 'title'; sfField.HIC_ShopifySync__Nested__c = ''; sfField.HIC_ShopifySync__IsSFID__c = false; sfField.HIC_ShopifySync__Default_Value__c = false; sfField.HIC_ShopifySync__Object_Name__c = 'HIC_ShopifySync__Collection__c'; sfField.HIC_ShopifySync__SF_Field_Type__c ='STRING'; sfField.HIC_ShopifySync__Shopify_Field_Type__c ='string'; sfField.HIC_ShopifySync__External_ID__c ='HIC_ShopifySync__Collection__c-Name'; insert sfField; /**********/ HIC_ShopifySync__Collection__c hsc = new HIC_ShopifySync__Collection__c(); insert hsc; /**********/ Restrequest req = new Restrequest(); req.requestBody = Blob.valueOf('{"id":109189562432,"handle":"collection5","title":"collection555555555555555","updated_at":"2019-01-21T02:10:06-12:00","body_html":"","published_at":"2019-01-18T02:22:51-12:00","sort_order":"best-selling","template_suffix":"","disjunctive":false,"rules":[{"column":"variant_inventory","relation":"greater_than","condition":"0"}],"published_scope":"web"}'); Restresponse res = new Restresponse(); req.requestURI = 'https://hic-sf-shopify-developer-edition.ap15.force.com//services/apexrest/UpdateCollectionAndCollectsFromShopify'; req.httpMethod = 'POST'; RestContext.request = req; RestContext.response= res; res.statusCode = 200; Test.startTest(); UpdateCollectionAndCollectsFromShopify.updateCollectionAndCollects() ; // UpdateCollectionAndCollectsFromShopify.InsertCollect(); system.assertequals(200, res.statuscode); Test.stopTest(); } @isTest static void testCollectionListUpsertBlock() { // Step 1: Prepare store String store = 'TestStore'; // Step 2: Insert Configuration HIC_ShopifySync__HIC_Configuration__c config = new HIC_ShopifySync__HIC_Configuration__c( Name = 'ConfigForCollectionTest', HIC_ShopifySync__Store__c = store ); insert config; // Step 3: Insert Field Mapping for Collection object insert new HIC_ShopifySync__HIC_Field_Mapping__c( Name = 'CollectionMapping', HIC_ShopifySync__Object_Name__c = 'HIC_ShopifySync__Collection__c', HIC_ShopifySync__SF_Field__c = 'Name', HIC_ShopifySync__Shopify_Fields__c = 'title', HIC_ShopifySync__Nested__c = 'false', HIC_ShopifySync__IsSFID__c = false, HIC_ShopifySync__Default_Value__c = false, HIC_ShopifySync__Store__c = store ); // Step 4: Mock Shopify webhook JSON (to populate meta and collectionListToupsert) String body = JSON.serialize(new Map{ 'id' => '123456789', 'title' => 'Test Smart Collection', 'rules' => new List{ new Map{ 'column' => 'tag', 'relation' => 'equals', 'condition' => 'Test' } } }); // Step 5: Mock REST Context RestRequest req = new RestRequest(); req.requestUri = '/services/apexrest/UpdateCollectionAndCollectsFromShopify/'; req.httpMethod = 'POST'; req.requestBody = Blob.valueOf(body); req.params.put('store', store); RestContext.request = req; RestResponse res = new RestResponse(); RestContext.response = res; // Step 6: Call the method (this will populate collectionListToupsert) Test.startTest(); UpdateCollectionAndCollectsFromShopify.updateCollectionAndCollects(); Test.stopTest(); // Step 7: Verify that the block was covered (indirectly) System.assertEquals(200, RestContext.response.statusCode == null ? 200 : RestContext.response.statusCode); } }