Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@W-14146755 lwrShippingItegSupportMGD #262

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions examples/b2c/checkout/integrations/classes/B2CDeliverySample.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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<CartDeliveryGroup> cartDeliveryGroups = new List<CartDeliveryGroup>([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.
Expand All @@ -30,12 +30,20 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How many times you call shippingOptionsAndRatesFromExternalService()? once or in a loop?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here before populating the cartDeliveryGroupMethods for a deliveryGroup, we need to check if there are any cartItems in the deliveryGroup, else we will have ShippingMethods for an emptyDeliveryGroup which is confusing

cdgmToBeCreated += 1;
}
}

List<CartDeliveryGroupMethod> cdgms = new List<CartDeliveryGroupMethod>([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;

Expand Down Expand Up @@ -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}},
Expand Down
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

init() is also called from setup()

Copy link
Author

@gkirpichnikov gkirpichnikov Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gurpreetsainisalesforce I'm aware, unfortunately those common 'static' variables in apex are actually 'null' if not explicitly populated; may be it use a different instance to run the setup and to run the test - not sure, anyway it is null whence the init is not called from the test

// Test: execute the integration for the test cart ID.
B2CDeliverySample apexSample = new B2CDeliverySample();
sfdc_checkout.IntegrationInfo integInfo = new sfdc_checkout.IntegrationInfo();
Expand All @@ -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<CartDeliveryGroupMethod> CDGMs = new List<CartDeliveryGroupMethod>([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();
}
}