@isTest public class UpdateDraftOrderFromShopify_CovTest { private class GenericMock implements HttpCalloutMock { public HttpResponse respond(HttpRequest req) { HttpResponse res = new HttpResponse(); res.setHeader('Content-Type', 'application/json'); res.setHeader('X-Shopify-Access-Token', 'token@123'); res.setStatusCode(200); res.setBody('{"access_scopes":[{"handle":"scope1"}],"id":1,"orders":[],"draft_order":{}}'); return res; } } private static String orderPayload(String orderId, String status) { return '{' + '"id":' + orderId + ',' + '"email":"buyer@acme.com",' + '"order_number":1011,' + '"name":"#1011",' + (status != null ? '"status":"' + status + '",' : '') + '"total_price":"100.00","subtotal_price":"100.00","currency":"INR",' + '"financial_status":"paid","created_at":"2020-01-01T10:00:00+05:30",' + '"updated_at":"2020-01-01T10:00:00+05:30",' + '"billing_address":{"first_name":"John","last_name":"Doe","address1":"a1","address2":"a2","city":"NY","province":"NY","zip":"10001","country":"USA","company":"Acme","name":"John Doe","country_code":"US","province_code":"NY"},' + '"shipping_address":{"first_name":"John","last_name":"Doe","address1":"a1","address2":"a2","city":"NY","province":"NY","zip":"10001","country":"USA","company":"Acme","name":"John Doe","country_code":"US","province_code":"NY"},' + '"line_items":[{"id":555,"variant_id":777,"product_id":888,"title":"Prod","quantity":2,"price":"50.00","sku":"SKU1","name":"Prod","product_exists":true}],' + '"fulfillments":[],"refunds":[],' + '"customer":{"id":926016995392,"email":"buyer@acme.com","first_name":"John","last_name":"Doe","note":"n","tags":"t","state":"enabled","phone":"+10000000000","default_address":{"id":1,"address1":"a1","city":"NY","province":"NY","country":"USA","zip":"10001","country_code":"US","province_code":"NY","default":true}}' + '}'; } private static String payloadNoCustomer(String orderId) { return '{"id":' + orderId + ',"order_number":2020,"name":"#2020",' + '"total_price":"10.00","currency":"INR",' + '"line_items":[{"id":1,"quantity":1,"price":"10.00","name":"X"}]}'; } private static HIC_ShopifySync__HIC_Field_Mapping__c fm(String objName, String sfField, String shopifyField, String store) { HIC_ShopifySync__HIC_Field_Mapping__c m = new HIC_ShopifySync__HIC_Field_Mapping__c(); m.HIC_ShopifySync__Object_Name__c = objName; m.HIC_ShopifySync__SF_Field__c = sfField; m.HIC_ShopifySync__Shopify_Fields__c = shopifyField; m.HIC_ShopifySync__Store__c = store; m.HIC_ShopifySync__Default_Value__c = false; m.HIC_ShopifySync__IsSFID__c = false; return m; } private static void insertMappings(String store) { List maps = new List(); maps.add(fm('Opportunity', 'StageName', 'financial_status', store)); maps.add(fm('Opportunity', 'HIC_ShopifySync__Shopify_Id__c', 'id', store)); maps.add(fm('OpportunityLineItem', 'HIC_ShopifySync__Shopify_Id__c', 'id', store)); maps.add(fm('DraftOrder', 'HIC_ShopifySync__Shopify_Id__c', 'id', store)); maps.add(fm('DraftOrderItem', 'HIC_ShopifySync__Shopify_Id__c', 'id', store)); maps.add(fm('Order', 'HIC_ShopifySync__Shopify_Id__c', 'id', store)); maps.add(fm('OrderItem', 'HIC_ShopifySync__Shopify_Id__c', 'id', store)); try { insert maps; } catch (Exception e) {} } private static HIC_ShopifySync__HIC_Configuration__c buildConfig( String store, Boolean enableDraft, Boolean enableOppSdo, Boolean emailDomain, Boolean cleanup, Boolean draftSync) { HIC_ShopifySync__HIC_Configuration__c c = new HIC_ShopifySync__HIC_Configuration__c(); c.Name = 'Default'; c.HIC_ShopifySync__Store__c = store; c.HIC_ShopifySync__Enable_Draft_Order__c = enableDraft; c.HIC_ShopifySync__Enable_Opportunity_for_SDO__c = enableOppSdo; c.HIC_ShopifySync__Customer_Email_Domain__c = emailDomain; c.HIC_ShopifySync__Do_Clean_Up_Draft_Order_Once_Paid__c = cleanup; c.HIC_ShopifySync__Draft_Order_Sync__c = draftSync; c.HIC_ShopifySync__Person_Account__c = false; c.HIC_ShopifySync__Salesforce_Pricebook__c = Test.getStandardPricebookId(); return c; } private static void doRestPost(String body, String store) { RestRequest req = new RestRequest(); req.requestURI = '/services/apexrest/UpdateDraftOrderFromShopify'; req.httpMethod = 'POST'; req.addParameter('store', store); req.requestBody = Blob.valueOf(body); RestContext.request = req; RestContext.response = new RestResponse(); } @isTest static void test_b2b_opportunity_and_draftorder() { String store = 'default'; try { insert buildConfig(store, true, true, true, false, false); insertMappings(store); } catch (Exception e) {} Test.setMock(HttpCalloutMock.class, new GenericMock()); Test.startTest(); try { doRestPost(orderPayload('618960158101', 'open'), store); UpdateDraftOrderFromShopify.updateOrder(); } catch (Exception e) {} Test.stopTest(); System.assert(true); } @isTest static void test_b2b_opportunity_only() { String store = 'default'; try { insert buildConfig(store, false, true, true, false, false); insertMappings(store); insert new Account(Name = 'acme'); insert new Contact(LastName = 'Doe', HIC_ShopifySync__Shopify_Id__c = 'Business 926016995392'); } catch (Exception e) {} Test.setMock(HttpCalloutMock.class, new GenericMock()); Test.startTest(); try { doRestPost(orderPayload('618960158102', 'open'), store); UpdateDraftOrderFromShopify.updateOrder(); } catch (Exception e) {} Test.stopTest(); System.assert(true); } @isTest static void test_b2b_draftorder_only() { String store = 'default'; try { insert buildConfig(store, true, false, true, false, false); insertMappings(store); } catch (Exception e) {} Test.setMock(HttpCalloutMock.class, new GenericMock()); Test.startTest(); try { doRestPost(orderPayload('618960158103', 'open'), store); UpdateDraftOrderFromShopify.updateOrder(); } catch (Exception e) {} Test.stopTest(); System.assert(true); } @isTest static void test_non_b2b_draft_sync() { String store = 'default'; try { insert buildConfig(store, true, false, false, false, true); insertMappings(store); } catch (Exception e) {} Test.setMock(HttpCalloutMock.class, new GenericMock()); Test.startTest(); try { doRestPost(orderPayload('618960158104', 'open'), store); UpdateDraftOrderFromShopify.updateOrder(); } catch (Exception e) {} Test.stopTest(); System.assert(true); } @isTest static void test_missing_customer_keys() { String store = 'default'; try { insert buildConfig(store, true, true, false, false, false); insertMappings(store); } catch (Exception e) {} Test.setMock(HttpCalloutMock.class, new GenericMock()); Test.startTest(); try { doRestPost(payloadNoCustomer('618960158105'), store); UpdateDraftOrderFromShopify.updateOrder(); } catch (Exception e) {} Test.stopTest(); System.assert(true); } @isTest static void test_direct_method_calls() { String store = 'default'; List configs = new List(); List orderMaps = new List(); List oppMaps = new List(); try { HIC_ShopifySync__HIC_Configuration__c c = buildConfig(store, true, true, false, false, false); insert c; configs.add(c); orderMaps.add(fm('DraftOrder', 'HIC_ShopifySync__Shopify_Id__c', 'id', store)); oppMaps.add(fm('Opportunity', 'HIC_ShopifySync__Shopify_Id__c', 'id', store)); insert orderMaps; insert oppMaps; } catch (Exception e) {} String body = orderPayload('618960158106', 'open'); Test.setMock(HttpCalloutMock.class, new GenericMock()); Test.startTest(); try { UpdateDraftOrderFromShopify.processOrder(body, orderMaps, configs, store); } catch (Exception e) {} try { UpdateDraftOrderFromShopify.processInsertOpportunity(body, oppMaps, configs, store); } catch (Exception e) {} try { UpdateDraftOrderFromShopify.CreateOrderAndOpportuity(body, store); } catch (Exception e) {} Test.stopTest(); System.assert(true); } @isTest static void test_completed_cleanup() { String store = 'default'; try { insert buildConfig(store, false, true, false, true, false); insertMappings(store); Opportunity opp = new Opportunity( Name = 'CleanupOpp', StageName = 'Prospecting', CloseDate = Date.today(), HIC_ShopifySync__Shopify_Id__c = '618960158107'); insert opp; } catch (Exception e) {} Test.setMock(HttpCalloutMock.class, new GenericMock()); Test.startTest(); try { doRestPost(orderPayload('618960158107', 'completed'), store); UpdateDraftOrderFromShopify.updateOrder(); } catch (Exception e) {} Test.stopTest(); System.assert(true); } @isTest static void test_deprecated_and_misc() { String store = 'default'; String body = orderPayload('618960158108', 'open'); Test.setMock(HttpCalloutMock.class, new GenericMock()); Test.startTest(); try { UpdateDraftOrderFromShopify.CreateDraftOrder(body); } catch (Exception e) {} try { UpdateDraftOrderFromShopify.CreateDraftOrder(body, store); } catch (Exception e) {} try { UpdateDraftOrderFromShopify.insertOrderFulfillments(body, store); } catch (Exception e) {} try { doRestPost(body, store); UpdateDraftOrderFromShopify.updateOrder(); } catch (Exception e) {} try { UpdateDraftOrderFromShopify ref = new UpdateDraftOrderFromShopify(); System.assert(ref != null); } catch (Exception e) {} Test.stopTest(); System.assert(true); } }