Search in sources :

Example 1 with InventoryCustomActionBuilder

use of com.commercetools.sync.inventories.helpers.InventoryCustomActionBuilder in project commercetools-sync-java by commercetools.

the class InventoryUpdateActionUtilsIT method buildCustomUpdateActions_ShouldBuildActionThatSetCustomField.

@Test
void buildCustomUpdateActions_ShouldBuildActionThatSetCustomField() {
    // Fetch old inventory and ensure it has custom fields.
    final Optional<InventoryEntry> oldInventoryOptional = getInventoryEntryBySkuAndSupplyChannel(CTP_TARGET_CLIENT, SKU_1, null);
    assertThat(oldInventoryOptional).isNotEmpty();
    final InventoryEntry oldInventoryBeforeSync = oldInventoryOptional.get();
    assertThat(oldInventoryBeforeSync.getCustom()).isNotNull();
    assertThat(oldInventoryBeforeSync.getCustom().getType().getObj()).isNotNull();
    assertThat(oldInventoryBeforeSync.getCustom().getType().getObj().getKey()).isEqualTo(CUSTOM_TYPE);
    // Prepare draft with updated data.
    final CustomFieldsDraft customFieldsDraft = CustomFieldsDraftBuilder.ofTypeId(oldInventoryBeforeSync.getCustom().getType().getId()).addObject(CUSTOM_FIELD_NAME, CUSTOM_FIELD_VALUE).build();
    final InventoryEntryDraft newInventory = InventoryEntryDraft.of(SKU_1, QUANTITY_ON_STOCK_2, EXPECTED_DELIVERY_2, RESTOCKABLE_IN_DAYS_2, null).withCustom(customFieldsDraft);
    // Build update actions.
    final InventorySyncOptions options = InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
    final List<UpdateAction<InventoryEntry>> updateActions = buildPrimaryResourceCustomUpdateActions(oldInventoryBeforeSync, newInventory, new InventoryCustomActionBuilder(), options);
    assertThat(updateActions).isNotEmpty();
    // Execute update command and ensure returned entry is properly updated.
    final InventoryEntry oldEntryAfterSync = CTP_TARGET_CLIENT.execute(InventoryEntryUpdateCommand.of(oldInventoryBeforeSync, updateActions)).toCompletableFuture().join();
    assertThat(oldEntryAfterSync.getCustom()).isNotNull();
    assertThat(oldEntryAfterSync.getCustom().getFieldAsString(CUSTOM_FIELD_NAME)).isEqualTo(CUSTOM_FIELD_VALUE);
}
Also used : InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) UpdateAction(io.sphere.sdk.commands.UpdateAction) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) InventoryCustomActionBuilder(com.commercetools.sync.inventories.helpers.InventoryCustomActionBuilder) Test(org.junit.jupiter.api.Test)

Example 2 with InventoryCustomActionBuilder

use of com.commercetools.sync.inventories.helpers.InventoryCustomActionBuilder in project commercetools-sync-java by commercetools.

the class InventoryEntryCustomUpdateActionUtilsTest method buildSetCustomFieldAction_WithInventoryResource_ShouldBuildInventoryUpdateAction.

@Test
void buildSetCustomFieldAction_WithInventoryResource_ShouldBuildInventoryUpdateAction() {
    final JsonNode customFieldValue = JsonNodeFactory.instance.textNode("foo");
    final String customFieldName = "name";
    final UpdateAction<InventoryEntry> updateAction = new InventoryCustomActionBuilder().buildSetCustomFieldAction(null, null, customFieldName, customFieldValue);
    assertThat(updateAction).isInstanceOf(SetCustomField.class);
    assertThat((SetCustomField) updateAction).hasValues("setCustomField", customFieldName, customFieldValue);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) SetCustomField(io.sphere.sdk.inventory.commands.updateactions.SetCustomField) InventoryCustomActionBuilder(com.commercetools.sync.inventories.helpers.InventoryCustomActionBuilder) Test(org.junit.jupiter.api.Test)

Example 3 with InventoryCustomActionBuilder

use of com.commercetools.sync.inventories.helpers.InventoryCustomActionBuilder in project commercetools-sync-java by commercetools.

the class InventoryEntryCustomUpdateActionUtilsTest method buildRemoveCustomTypeAction_WithInventoryResource_ShouldBuildChannelUpdateAction.

@Test
void buildRemoveCustomTypeAction_WithInventoryResource_ShouldBuildChannelUpdateAction() {
    final UpdateAction<InventoryEntry> updateAction = new InventoryCustomActionBuilder().buildRemoveCustomTypeAction(null, null);
    assertThat(updateAction).isInstanceOf(SetCustomType.class);
    assertThat((SetCustomType) updateAction).hasValues("setCustomType", null, ofId(null));
}
Also used : SetCustomType(io.sphere.sdk.inventory.commands.updateactions.SetCustomType) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) InventoryCustomActionBuilder(com.commercetools.sync.inventories.helpers.InventoryCustomActionBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

InventoryCustomActionBuilder (com.commercetools.sync.inventories.helpers.InventoryCustomActionBuilder)3 InventoryEntry (io.sphere.sdk.inventory.InventoryEntry)3 Test (org.junit.jupiter.api.Test)3 InventorySyncOptions (com.commercetools.sync.inventories.InventorySyncOptions)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 UpdateAction (io.sphere.sdk.commands.UpdateAction)1 InventoryEntryDraft (io.sphere.sdk.inventory.InventoryEntryDraft)1 SetCustomField (io.sphere.sdk.inventory.commands.updateactions.SetCustomField)1 SetCustomType (io.sphere.sdk.inventory.commands.updateactions.SetCustomType)1 CustomFieldsDraft (io.sphere.sdk.types.CustomFieldsDraft)1