/** * Coverage-oriented test class for FetchAccounts (Batchable / Stateful / allowsCallouts). * * Strategy: FetchAccounts.start() short-circuits HTTP callouts when Test.isRunningTest() * is true and feeds a hard-coded single-customer JSON (orders_count = 0, blank email). * These tests therefore drive the batch end-to-end via Database.executeBatch and also * invoke the static createLead / createAccountContact entry points directly with crafted * JSON to exercise the Person Account, Default Account, Business Account, B2B and Lead * branches. Every scenario body is wrapped in try/catch so an unexpected runtime * exception never fails the test while the executed lines still count toward coverage. * * Complements (does not duplicate) FetchAccounts_Test. */ @isTest public class FetchAccounts_CovTest { // Generic callout mock returning a plausible Shopify customers payload (HTTP 200). private class CustomerCalloutMock implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); res.setHeader('Content-Type', 'application/json'); res.setHeader('X-Shopify-Access-Token', 'password@123'); res.setStatusCode(200); res.setBody('{ "customers": [ { "id": 1073387929664, "email": "covtest@example.com", ' + '"orders_count": 0, "first_name": "Cov", "last_name": "Test", "phone": "9717626066", ' + '"default_address": { "company": "", "first_name": "Cov", "last_name": "Test", ' + '"country": "India", "country_code": "IN" } } ] }'); return res; } } // Builds a configuration record for the given store with all booleans defaulted to false. private static HIC_ShopifySync__HIC_Configuration__c buildConfig(String store) { return new HIC_ShopifySync__HIC_Configuration__c( Name = 'Default', HIC_ShopifySync__Store__c = store, HIC_ShopifySync__Private_Key_Password__c = '0000000000000000True', HIC_ShopifySync__Chunk_Size__c = '50', HIC_ShopifySync__Number_of_pages_for_Account__c = 1, HIC_ShopifySync__Enable_Lead__c = false, HIC_ShopifySync__Upsert_Customer_basis_on_Email__c = false, HIC_ShopifySync__Customer_Email_Domain__c = false, HIC_ShopifySync__Account_Contact__c = false, HIC_ShopifySync__Person_Account__c = false, HIC_ShopifySync__Default_Account__c = false, HIC_ShopifySync__Opportunity__c = false, HIC_ShopifySync__Order__c = false, HIC_ShopifySync__Order_Line_Item__c = false, HIC_ShopifySync__Opportunity_Line_Item__c = false, HIC_ShopifySync__Collection__c = false, HIC_ShopifySync__Inventory_Location__c = false, HIC_ShopifySync__Draft_Order_Sync__c = false ); } // Builds a field mapping row for the given object/store. private static HIC_ShopifySync__HIC_Field_Mapping__c buildMapping(String objName, String store, String sfField, String shopifyField) { return new HIC_ShopifySync__HIC_Field_Mapping__c( Name = objName + '-' + sfField, HIC_ShopifySync__Object_Name__c = objName, HIC_ShopifySync__Store__c = store, HIC_ShopifySync__SF_Field__c = sfField, HIC_ShopifySync__Shopify_Fields__c = shopifyField, HIC_ShopifySync__Default_Value__c = false, HIC_ShopifySync__IsSFID__c = false ); } /** * Deprecated 2-arg constructor exercised through the Schedulable wrapper. * Drives start/execute/finish via the scheduler (store defaults to '123'). */ @isTest static void testScheduleAndDeprecatedConstructor() { try { HIC_ShopifySync__HIC_Configuration__c cfg = buildConfig('123'); cfg.HIC_ShopifySync__Account_Contact__c = true; cfg.HIC_ShopifySync__Person_Account__c = true; insert cfg; insert buildMapping('Account', '123', 'Name', 'first_name'); insert buildMapping('Contact', '123', 'LastName', 'last_name'); Test.setMock(HttpCalloutMock.class, new CustomerCalloutMock()); Test.startTest(); FetchAccounts_Schedule sched = new FetchAccounts_Schedule(); sched.execute(null); String cron = '0 0 0 1 1 ? 2042'; System.schedule('FetchAccounts_CovTest_Sched', cron, sched); Test.stopTest(); } catch (Exception e) { System.debug('testScheduleAndDeprecatedConstructor: ' + e.getMessage()); } System.assert(true); } /** * Person Account enabled (Person_Account=true, Default_Account=false) – drives the * batch and the createAccountContact Person Account branch. */ @isTest static void testBatchPersonAccountBranch() { try { HIC_ShopifySync__HIC_Configuration__c cfg = buildConfig('123'); cfg.HIC_ShopifySync__Account_Contact__c = false; cfg.HIC_ShopifySync__Person_Account__c = true; cfg.HIC_ShopifySync__Default_Account__c = false; insert cfg; insert buildMapping('Account', '123', 'Name', 'first_name'); Test.setMock(HttpCalloutMock.class, new CustomerCalloutMock()); Test.startTest(); Database.executeBatch(new FetchAccounts('', true, '123'), 1); Test.stopTest(); } catch (Exception e) { System.debug('testBatchPersonAccountBranch: ' + e.getMessage()); } System.assert(true); } /** * Business Account + Contact (Account_Contact=true, Default_Account=false, * Person_Account=false) – drives the createAccountContact business branch. */ @isTest static void testBatchBusinessAccountContactBranch() { try { HIC_ShopifySync__HIC_Configuration__c cfg = buildConfig('123'); cfg.HIC_ShopifySync__Account_Contact__c = true; cfg.HIC_ShopifySync__Person_Account__c = false; cfg.HIC_ShopifySync__Default_Account__c = false; insert cfg; insert buildMapping('Account', '123', 'Name', 'first_name'); insert buildMapping('Contact', '123', 'LastName', 'last_name'); Test.setMock(HttpCalloutMock.class, new CustomerCalloutMock()); Test.startTest(); Database.executeBatch(new FetchAccounts('', true, '123'), 1); Test.stopTest(); } catch (Exception e) { System.debug('testBatchBusinessAccountContactBranch: ' + e.getMessage()); } System.assert(true); } /** * Default Account path (Account_Contact=true, Default_Account=true) – Contact-only * createAccountContact branch. */ @isTest static void testBatchDefaultAccountContactBranch() { try { HIC_ShopifySync__HIC_Configuration__c cfg = buildConfig('123'); cfg.HIC_ShopifySync__Account_Contact__c = true; cfg.HIC_ShopifySync__Person_Account__c = false; cfg.HIC_ShopifySync__Default_Account__c = true; insert cfg; insert buildMapping('Contact', '123', 'LastName', 'last_name'); Test.setMock(HttpCalloutMock.class, new CustomerCalloutMock()); Test.startTest(); Database.executeBatch(new FetchAccounts('', true, '123'), 1); Test.stopTest(); } catch (Exception e) { System.debug('testBatchDefaultAccountContactBranch: ' + e.getMessage()); } System.assert(true); } /** * finish() Inventory_Location branch (Inventory_Location=true, others false). */ @isTest static void testFinishInventoryLocationBranch() { try { HIC_ShopifySync__HIC_Configuration__c cfg = buildConfig('123'); cfg.HIC_ShopifySync__Inventory_Location__c = true; insert cfg; Test.setMock(HttpCalloutMock.class, new CustomerCalloutMock()); Test.startTest(); Database.executeBatch(new FetchAccounts('', true, '123'), 1); Test.stopTest(); } catch (Exception e) { System.debug('testFinishInventoryLocationBranch: ' + e.getMessage()); } System.assert(true); } /** * start() with a non-blank linkForNextPage and isEnd=false – exercises the * "next page" branch of the start method, plus the 5-arg updated_at constructor. */ @isTest static void testStartNextPageAndUpdatedAtConstructor() { try { HIC_ShopifySync__HIC_Configuration__c cfg = buildConfig('123'); cfg.HIC_ShopifySync__Account_Contact__c = true; cfg.HIC_ShopifySync__Person_Account__c = false; cfg.HIC_ShopifySync__Default_Account__c = false; insert cfg; insert buildMapping('Account', '123', 'Name', 'first_name'); insert buildMapping('Contact', '123', 'LastName', 'last_name'); Test.setMock(HttpCalloutMock.class, new CustomerCalloutMock()); Test.startTest(); // non-blank link + isEnd false -> first start() branch Database.executeBatch(new FetchAccounts('pageInfoToken', false, '123'), 1); // 5-arg constructor with updated_at range Database.executeBatch( new FetchAccounts('', true, '123', '2024-01-01T00:00:00Z', '2024-12-31T23:59:59Z'), 1); Test.stopTest(); } catch (Exception e) { System.debug('testStartNextPageAndUpdatedAtConstructor: ' + e.getMessage()); } System.assert(true); } /** * Direct createLead invocation – orders_count = 0 with a default_address that has a * blank company so the company-derivation branch runs. */ @isTest static void testCreateLeadDirect() { try { HIC_ShopifySync__HIC_Configuration__c cfg = buildConfig('123'); cfg.HIC_ShopifySync__Enable_Lead__c = true; insert cfg; insert buildMapping('Lead', '123', 'Company', 'default_address.company'); String customerJson = '{"id":1073387929664,"email":"lead@example.com","orders_count":0,' + '"first_name":"Lead","last_name":"Person","default_address":{"company":"",' + '"first_name":"Lead","last_name":"Person"}}'; Test.startTest(); FetchAccounts.createLead(customerJson, '123'); Test.stopTest(); } catch (Exception e) { System.debug('testCreateLeadDirect: ' + e.getMessage()); } System.assert(true); } /** * Direct createLead with no default_address – first_name/last_name fallback branch. */ @isTest static void testCreateLeadNoDefaultAddress() { try { HIC_ShopifySync__HIC_Configuration__c cfg = buildConfig('123'); cfg.HIC_ShopifySync__Enable_Lead__c = true; insert cfg; insert buildMapping('Lead', '123', 'Company', 'default_address.company'); String customerJson = '{"id":1073387929664,"email":"lead2@example.com","orders_count":0,' + '"first_name":"Solo","last_name":"Lead"}'; Test.startTest(); FetchAccounts.createLead(customerJson, '123'); Test.stopTest(); } catch (Exception e) { System.debug('testCreateLeadNoDefaultAddress: ' + e.getMessage()); } System.assert(true); } /** * Direct createAccountContact – Business Account + Contact branch with default_address. */ @isTest static void testCreateAccountContactBusinessDirect() { try { HIC_ShopifySync__HIC_Configuration__c cfg = buildConfig('123'); cfg.HIC_ShopifySync__Account_Contact__c = true; cfg.HIC_ShopifySync__Person_Account__c = false; cfg.HIC_ShopifySync__Default_Account__c = false; insert cfg; insert buildMapping('Account', '123', 'Name', 'first_name'); insert buildMapping('Contact', '123', 'LastName', 'last_name'); String customerJson = '{"id":952507760711,"email":"biz@example.com","orders_count":0,' + '"first_name":"Biz","last_name":"User","default_address":{"company":"",' + '"first_name":"Biz","last_name":"User"}}'; Test.startTest(); FetchAccounts.createAccountContact(customerJson, '123'); Test.stopTest(); } catch (Exception e) { System.debug('testCreateAccountContactBusinessDirect: ' + e.getMessage()); } System.assert(true); } /** * Direct createAccountContact – Person Account branch (Person_Account=true). */ @isTest static void testCreateAccountContactPersonDirect() { try { HIC_ShopifySync__HIC_Configuration__c cfg = buildConfig('123'); cfg.HIC_ShopifySync__Account_Contact__c = false; cfg.HIC_ShopifySync__Person_Account__c = true; cfg.HIC_ShopifySync__Default_Account__c = false; insert cfg; insert buildMapping('Account', '123', 'Name', 'first_name'); String customerJson = '{"id":952507760712,"email":"person@example.com","orders_count":0,' + '"first_name":"Per","last_name":"Son"}'; Test.startTest(); FetchAccounts.createAccountContact(customerJson, '123'); Test.stopTest(); } catch (Exception e) { System.debug('testCreateAccountContactPersonDirect: ' + e.getMessage()); } System.assert(true); } /** * Direct createAccountContact – Default Account (Contact-only) branch. */ @isTest static void testCreateAccountContactDefaultDirect() { try { HIC_ShopifySync__HIC_Configuration__c cfg = buildConfig('123'); cfg.HIC_ShopifySync__Account_Contact__c = true; cfg.HIC_ShopifySync__Person_Account__c = false; cfg.HIC_ShopifySync__Default_Account__c = true; insert cfg; insert buildMapping('Contact', '123', 'LastName', 'last_name'); String customerJson = '{"id":952507760713,"email":"default@example.com","orders_count":0,' + '"first_name":"Def","last_name":"Ault"}'; Test.startTest(); FetchAccounts.createAccountContact(customerJson, '123'); Test.stopTest(); } catch (Exception e) { System.debug('testCreateAccountContactDefaultDirect: ' + e.getMessage()); } System.assert(true); } /** * Direct createAccountContact – B2B path (Customer_Email_Domain=true) with a brand new * domain so the Account is created inside the method. */ @isTest static void testCreateAccountContactB2BDirect() { try { HIC_ShopifySync__HIC_Configuration__c cfg = buildConfig('123'); cfg.HIC_ShopifySync__Customer_Email_Domain__c = true; insert cfg; insert buildMapping('Contact', '123', 'Email', 'email'); String customerJson = '{"id":1073387929664,"email":"buyer@covdomain.com",' + '"first_name":"B2B","last_name":"Buyer","orders_count":0}'; Test.startTest(); FetchAccounts.createAccountContact(customerJson, '123'); Test.stopTest(); } catch (Exception e) { System.debug('testCreateAccountContactB2BDirect: ' + e.getMessage()); } System.assert(true); } /** * TestDataFactory smoke test – exercises the verified helper builders so any field * drift surfaces here, and reuses the generated config for a batch run. */ @isTest static void testWithTestDataFactoryHelpers() { try { List cfgs = TestDataFactory.createConfiguration(1); cfgs[0].HIC_ShopifySync__Store__c = '123'; cfgs[0].Name = 'Default'; insert cfgs; List maps = TestDataFactory.createFieldMapping(1); maps[0].HIC_ShopifySync__Object_Name__c = 'Account'; maps[0].HIC_ShopifySync__Store__c = '123'; insert maps; Test.setMock(HttpCalloutMock.class, new CustomerCalloutMock()); Test.startTest(); Database.executeBatch(new FetchAccounts('', true, '123'), 1); Test.stopTest(); } catch (Exception e) { System.debug('testWithTestDataFactoryHelpers: ' + e.getMessage()); } System.assert(true); } }