diff --git a/examples/b2c/checkout/integrations/classes/B2CDeliverySample.cls b/examples/b2c/checkout/integrations/classes/B2CDeliverySample.cls index dd7a2c35..7b856e8d 100644 --- a/examples/b2c/checkout/integrations/classes/B2CDeliverySample.cls +++ b/examples/b2c/checkout/integrations/classes/B2CDeliverySample.cls @@ -11,7 +11,7 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges { try { // In the Winter '21 release there should be two delivery groups per cart. // We need to get the ID of the cart delivery group in order to create the cart delivery group methods. - Id cartDeliveryGroupId = [SELECT CartDeliveryGroupId FROM CartItem WHERE CartId = :cartId][0].CartDeliveryGroupId; + List cartDeliveryGroups = new List([SELECT Id FROM CartDeliveryGroup WHERE CartId = :cartId]); // Get the shipping options from an external service. // We're getting information like rates and carriers from this external service. @@ -30,12 +30,20 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges { } // On re-entry of the checkout flow delete all previous CartDeliveryGroupMehods for the given cartDeliveryGroupId - delete [SELECT Id FROM CartDeliveryGroupMethod WHERE CartDeliveryGroupId = :cartDeliveryGroupId]; + delete [SELECT Id FROM CartDeliveryGroupMethod WHERE WebCartId = :cartId]; // Create a CartDeliveryGroupMethod record for every shipping option returned from the external service + Integer cdgmToBeCreated = 0; for (ShippingOptionsAndRatesFromExternalService shippingOption: shippingOptionsAndRatesFromExternalService) { - populateCartDeliveryGroupMethodWithShippingOptions(shippingOption, cartDeliveryGroupId, cartId); + for(CartDeliveryGroup curCartDeliveryGroup : cartDeliveryGroups){ + populateCartDeliveryGroupMethodWithShippingOptions(shippingOption, curCartDeliveryGroup.Id, cartId); + cdgmToBeCreated += 1; + } } + + List cdgms = new List([SELECT Id FROM CartDeliveryGroupMethod WHERE WebCartId = :cartId]); + System.assertEquals(cdgmToBeCreated, cdgms.size(),'The number of created CDGMs is not matching'); // It's important to fail the example integration early + // If everything works well, the charge is added to the cart and our integration has been successfully completed. integStatus.status = sfdc_checkout.IntegrationStatus.Status.SUCCESS; @@ -82,6 +90,7 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges { request.setEndpoint(httpHost + '/calculate-shipping-rates-winter-21-with-lang?lang=' + siteLanguage); request.setMethod('GET'); HttpResponse response = http.send(request); + // If the request is successful, parse the JSON response. // The response looks like this: // [{"status":"calculated","rate":{"name":"Delivery Method 1","serviceName":"Test Carrier 1","serviceCode":"SNC9600","shipmentCost":11.99,"otherCost":5.99}}, diff --git a/examples/b2c/checkout/integrations/classes/B2CDeliverySampleTest.cls b/examples/b2c/checkout/integrations/classes/B2CDeliverySampleTest.cls index 17ab6c4a..da1e8269 100644 --- a/examples/b2c/checkout/integrations/classes/B2CDeliverySampleTest.cls +++ b/examples/b2c/checkout/integrations/classes/B2CDeliverySampleTest.cls @@ -1,6 +1,16 @@ @isTest private class B2CDeliverySampleTest { + + static Integer cartDeliveryGroupsNo; + static Integer expectedCDGMInTheIntegrationMock; + + static void init(){ + cartDeliveryGroupsNo = 4; // This value can be changed as needed + expectedCDGMInTheIntegrationMock = 2; // This value shall not be changed without matchiing the integration implementation mock + } + @testSetup static void setup() { + init(); Account testAccount = new Account(Name='TestAccount'); insert testAccount; WebStore testWebStore = new WebStore(Name='TestWebStore', SupportedLanguages='en_US', DefaultLanguage='en_US'); @@ -11,16 +21,21 @@ private class B2CDeliverySampleTest { WebCart cart = new WebCart(Name='Cart', WebStoreId=webStore.Id, AccountId=account.Id); insert cart; - CartDeliveryGroup cartDeliveryGroup = new CartDeliveryGroup(CartId=cart.Id, Name='Default Delivery 1'); - insert cartDeliveryGroup; + for (Integer i = 1; i <= cartDeliveryGroupsNo; i++) { + CartDeliveryGroup cartDeliveryGroup = new CartDeliveryGroup(CartId=cart.Id, Name='Default Delivery ' + i); + insert cartDeliveryGroup; - CartItem cartItem = new CartItem(CartId=cart.Id, Type='Product', Name='TestProduct', CartDeliveryGroupId=cartDeliveryGroup.Id); - insert cartItem; + for (Integer j = 0; j < expectedCDGMInTheIntegrationMock; j++) { + CartItem cartItem = new CartItem(CartId=cart.Id, Type='Product', Name='TestProduct', CartDeliveryGroupId=cartDeliveryGroup.Id); + insert cartItem; + } + } } @isTest static void testIntegrationRunsSuccessfully() { Test.startTest(); + init(); // Test: execute the integration for the test cart ID. B2CDeliverySample apexSample = new B2CDeliverySample(); sfdc_checkout.IntegrationInfo integInfo = new sfdc_checkout.IntegrationInfo(); @@ -29,6 +44,11 @@ private class B2CDeliverySampleTest { sfdc_checkout.IntegrationStatus integrationResult = apexSample.startCartProcessAsync(integInfo, webCart.Id); // Verify: the integration executed successfully System.assertEquals(sfdc_checkout.IntegrationStatus.Status.SUCCESS, integrationResult.status); + + List CDGMs = new List([SELECT Id FROM CartDeliveryGroupMethod WHERE WebCartId = :webCart.Id]); + Integer expectedCDGMs = cartDeliveryGroupsNo * expectedCDGMInTheIntegrationMock; + System.assertEquals(expectedCDGMs, CDGMs.size(),'(MultipppleDeliveryGroups/MDG support validation) The expected ' + expectedCDGMs + ' CartDeliveryGroupMethods were not created by the integration'); + Test.stopTest(); } }