Shipment construction
  • 02 Nov 2023
  • 1 Minute to read
  • Dark
    Light

Shipment construction

  • Dark
    Light

Article Summary

Example

ExampleShipmentsConstructor

global class ExampleShipmentsConstructor extends welisacommerce.DefaultShipmentsConstructor {
    //override the default constructor to create new shipments
    //based on the shopping cart and its items
    global override List<welisacommerce.ShipmentData> doConstruct(welisacommerce.ConstructShipmentsContext context) {
        //Create two example shipments
        welisacommerce.ShipmentData shipment1 = new welisacommerce.ShipmentData();
        shipment1.record = new welisacommerce__Shipment__c(
            Name = 'Example shipment 1');
        shipment1.items = new List<welisacommerce.ShoppingCartData.Item> { context.shoppingCart.items[0] };

        welisacommerce.ShipmentData shipment2 = new welisacommerce.ShipmentData();
        shipment2.record = new welisacommerce__Shipment__c(
            Name = 'Example shipment 2');
        shipment2.items = new List<welisacommerce.ShoppingCartData.Item> { context.shoppingCart.items[1] };

        return new List<welisacommerce.ShipmentData> { shipment1, shipment2 };
    }

    global override void populateDefaultValues(welisacommerce.ShipmentData shipment) {
        //optional override to implement custom logic for populating default shipment values
        shipment.record.welisacommerce__Delivery_Method__c = 'Shipping';
        shipment.record.welisacommerce__Shipping_City__c = 'Default City';
        shipment.record.welisacommerce__Shipping_Country__c = 'US';
        shipment.record.welisacommerce__Shipping_Postal_Code__c = '12345';
        shipment.record.welisacommerce__Shipping_State__c = 'US-AL';
        shipment.record.welisacommerce__Shipping_Street__c = 'Default Street';
    }
}

ExampleShipmentsConstructorTest

@IsTest
private class ExampleShipmentsConstructorTest {
    @IsTest
    private static void itShouldSetShipmentNameDuringConstruction() {
        //GIVEN
        welisacommerce.ConstructShipmentsContext context = generateContext();

        //WHEN
        Test.startTest();
        List<welisacommerce.ShipmentData> constructedShipments = new ExampleShipmentsConstructor().doConstruct(context);
        Test.stopTest();

        //THEN
        Assert.isNotNull(constructedShipments[0].record.Name);
        Assert.isNotNull(constructedShipments[1].record.Name);
    }

    @IsTest
    private static void itShouldAssignAShoppingCartItemPerShipment() {
        //GIVEN
        welisacommerce.ConstructShipmentsContext context = generateContext();

        //WHEN
        Test.startTest();
        List<welisacommerce.ShipmentData> constructedShipments = new ExampleShipmentsConstructor().doConstruct(context);
        Test.stopTest();

        //THEN
        Assert.areEqual(constructedShipments[0].items[0], context.shoppingCart.items[0]);
        Assert.areEqual(constructedShipments[1].items[0], context.shoppingCart.items[1]);
    }

    @IsTest
    private static void itShouldPopulateDefaultValues() {
        //GIVEN
        welisacommerce.ShipmentData shipmentData = generateShipmentData();

        //WHEN
        Test.startTest();
        new ExampleShipmentsConstructor().populateDefaultValues(shipmentData);
        Test.stopTest();

        //THEN
        Assert.isNotNull(shipmentData.record.welisacommerce__Delivery_Method__c);
        Assert.isNotNull(shipmentData.record.welisacommerce__Shipping_City__c);
        Assert.isNotNull(shipmentData.record.welisacommerce__Shipping_Country__c);
        Assert.isNotNull(shipmentData.record.welisacommerce__Shipping_Postal_Code__c);
        Assert.isNotNull(shipmentData.record.welisacommerce__Shipping_State__c);
        Assert.isNotNull(shipmentData.record.welisacommerce__Shipping_Street__c);
    }

    private static welisacommerce.ShipmentData generateShipmentData() {
        welisacommerce.ShipmentData shipmentData = new welisacommerce.ShipmentData();
        shipmentData.record = new welisacommerce__Shipment__c(
            Name = 'Example Shipment 1'
        );
        return shipmentData;
    }

    private static welisacommerce.ConstructShipmentsContext generateContext() {
        welisacommerce.ConstructShipmentsContext context = new welisacommerce.ConstructShipmentsContext();

        context.shoppingCart = new welisacommerce.ShoppingCartData();
        context.shoppingCart.record = new welisacommerce__Shopping_Cart__c(
            Id = generateId(welisacommerce__Shopping_Cart__c.SObjectType)
        );

        welisacommerce.ShoppingCartData.Item item1 = new welisacommerce.ShoppingCartData.Item();
        item1.record = new welisacommerce__Shopping_Cart_Item__c(
            Id = generateId(welisacommerce__Shopping_Cart_Item__c.SObjectType)
        );
        welisacommerce.ShoppingCartData.Item item2 = new welisacommerce.ShoppingCartData.Item();
        item2.record = new welisacommerce__Shopping_Cart_Item__c(
            Id = generateId(welisacommerce__Shopping_Cart_Item__c.SObjectType)
        );

        context.shoppingCart.items = new List<welisacommerce.ShoppingCartData.Item> { item1, item2 };

        return context;
    }

    private static Integer fakeIdCount = 0;
	/**
	 * Generate a fake Salesforce Id for the given SObjectType
	 */
	private static Id generateId(Schema.SObjectType sobjectType)
	{
		String keyPrefix = sobjectType.getDescribe().getKeyPrefix();
		fakeIdCount++;

		String fakeIdPrefix = ('000000000000').substring(0, 12 - String.valueOf(fakeIdCount).length());

		return Id.valueOf(keyPrefix + fakeIdPrefix + fakeIdCount);
	}
}