Search in sources :

Example 21 with InventorySyncStatistics

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

the class InventorySyncIT method sync_WithKeyToExistingSupplyChannelInPlaceOfId_ShouldUpdateInventory.

@Test
void sync_WithKeyToExistingSupplyChannelInPlaceOfId_ShouldUpdateInventory() {
    /*
     * Fetch existing Channel of key SUPPLY_CHANNEL_KEY_1 from target project.
     * This is done only for test assertion reasons, not necessary for sync.
     */
    final Optional<Channel> supplyChannel = getChannelByKey(CTP_TARGET_CLIENT, SUPPLY_CHANNEL_KEY_1);
    assertThat(supplyChannel).isNotEmpty();
    /*
     * Prepare InventoryEntryDraft of sku SKU_1 and reference to supply channel of key SUPPLY_CHANNEL_KEY_1.
     */
    final ResourceIdentifier<Channel> supplyChannelReference = ResourceIdentifier.ofKey(SUPPLY_CHANNEL_KEY_1);
    final InventoryEntryDraft newInventoryDraft = InventoryEntryDraftBuilder.of(SKU_1, QUANTITY_ON_STOCK_2, EXPECTED_DELIVERY_2, RESTOCKABLE_IN_DAYS_2, supplyChannelReference).build();
    // Ensure old entry values before sync.
    final Optional<InventoryEntry> oldInventoryBeforeSync = getInventoryEntryBySkuAndSupplyChannel(CTP_TARGET_CLIENT, SKU_1, supplyChannel.get().toReference());
    assertThat(oldInventoryBeforeSync).isPresent();
    assertValues(oldInventoryBeforeSync.get(), QUANTITY_ON_STOCK_1, EXPECTED_DELIVERY_1, RESTOCKABLE_IN_DAYS_1);
    // Prepare sync options and perform sync of draft to target project.
    final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
    final InventorySync inventorySync = new InventorySync(inventorySyncOptions);
    final InventorySyncStatistics inventorySyncStatistics = inventorySync.sync(singletonList(newInventoryDraft)).toCompletableFuture().join();
    assertThat(inventorySyncStatistics).hasValues(1, 0, 1, 0);
    // Ensure old entry values after sync.
    final Optional<InventoryEntry> oldInventoryAfterSync = getInventoryEntryBySkuAndSupplyChannel(CTP_TARGET_CLIENT, SKU_1, supplyChannel.get().toReference());
    assertThat(oldInventoryAfterSync).isPresent();
    assertValues(oldInventoryAfterSync.get(), QUANTITY_ON_STOCK_2, EXPECTED_DELIVERY_2, RESTOCKABLE_IN_DAYS_2);
}
Also used : InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) InventorySync(com.commercetools.sync.inventories.InventorySync) Channel(io.sphere.sdk.channels.Channel) InventoryITUtils.getInventoryEntryBySkuAndSupplyChannel(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.getInventoryEntryBySkuAndSupplyChannel) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions) Test(org.junit.jupiter.api.Test)

Example 22 with InventorySyncStatistics

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

the class InventorySyncTest method sync_WithExistingInventoryEntryButWithEmptyCustomTypeReference_ShouldFailSync.

@Test
void sync_WithExistingInventoryEntryButWithEmptyCustomTypeReference_ShouldFailSync() {
    final InventorySyncOptions options = getInventorySyncOptions(3, false);
    final InventoryService inventoryService = getMockInventoryService(existingInventories, mock(InventoryEntry.class), mock(InventoryEntry.class));
    final ChannelService channelService = mock(ChannelService.class);
    when(channelService.fetchCachedChannelId(anyString())).thenReturn(completedFuture(Optional.of(REF_2)));
    final InventorySync inventorySync = new InventorySync(options, inventoryService, channelService, mock(TypeService.class));
    final List<InventoryEntryDraft> newDrafts = new ArrayList<>();
    final InventoryEntryDraft draftWithNullCustomTypeId = InventoryEntryDraft.of(SKU_1, QUANTITY_1, DATE_1, RESTOCKABLE_1, null).withCustom(CustomFieldsDraft.ofTypeKeyAndJson("", new HashMap<>()));
    newDrafts.add(draftWithNullCustomTypeId);
    final InventorySyncStatistics syncStatistics = inventorySync.sync(newDrafts).toCompletableFuture().join();
    assertThat(syncStatistics).hasValues(1, 0, 0, 1);
    assertThat(errorCallBackMessages).isNotEmpty();
    assertThat(errorCallBackMessages.get(0)).contains(format("Failed to process the" + " InventoryEntryDraft with SKU:'%s'. Reason: %s: Failed to resolve custom type resource identifier on " + "InventoryEntryDraft with SKU:'1000'. Reason: %s", SKU_1, ReferenceResolutionException.class.getCanonicalName(), BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER));
    assertThat(errorCallBackExceptions).isNotEmpty();
    assertThat(errorCallBackExceptions.get(0)).isExactlyInstanceOf(CompletionException.class);
    assertThat(errorCallBackExceptions.get(0).getCause()).isExactlyInstanceOf(ReferenceResolutionException.class);
}
Also used : InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) HashMap(java.util.HashMap) ChannelService(com.commercetools.sync.services.ChannelService) InventorySyncMockUtils.getMockChannelService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService) InventorySyncMockUtils.getMockInventoryService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryService) InventoryService(com.commercetools.sync.services.InventoryService) ArrayList(java.util.ArrayList) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) InventorySyncMockUtils.getMockInventoryEntry(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryEntry) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 23 with InventorySyncStatistics

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

the class InventorySyncTest method sync_WithExceptionWhenUpdatingEntries_ShouldNotSync.

@Test
void sync_WithExceptionWhenUpdatingEntries_ShouldNotSync() {
    final InventorySyncOptions options = getInventorySyncOptions(3, false);
    final InventoryService inventoryService = getMockInventoryService(existingInventories, mock(InventoryEntry.class), mock(InventoryEntry.class));
    when(inventoryService.updateInventoryEntry(any(), any())).thenReturn(getCompletionStageWithException());
    final ChannelService channelService = getMockChannelService(getMockSupplyChannel(REF_2, KEY_2));
    final InventorySync inventorySync = new InventorySync(options, inventoryService, channelService, mock(TypeService.class));
    final InventoryEntryDraft inventoryEntryDraft = InventoryEntryDraftBuilder.of(SKU_2, QUANTITY_2, DATE_1, RESTOCKABLE_1, ResourceIdentifier.ofKey(KEY_2)).build();
    final InventorySyncStatistics stats = inventorySync.sync(Collections.singletonList(inventoryEntryDraft)).toCompletableFuture().join();
    assertThat(stats).hasValues(1, 0, 0, 1);
    assertThat(errorCallBackMessages).hasSize(1);
    assertThat(errorCallBackExceptions).hasSize(1);
    assertThat(errorCallBackMessages.get(0)).isEqualTo(format("Failed to update inventory entry of SKU '%s' and supply channel id '%s'.", SKU_2, REF_2));
    assertThat(errorCallBackExceptions.get(0)).isExactlyInstanceOf(RuntimeException.class);
}
Also used : InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) ChannelService(com.commercetools.sync.services.ChannelService) InventorySyncMockUtils.getMockChannelService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService) InventorySyncMockUtils.getMockInventoryService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryService) InventoryService(com.commercetools.sync.services.InventoryService) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) InventorySyncMockUtils.getMockInventoryEntry(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryEntry) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 24 with InventorySyncStatistics

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

the class InventorySyncTest method sync_WithDraftsWithEmptySku_ShouldNotSync.

@Test
void sync_WithDraftsWithEmptySku_ShouldNotSync() {
    final InventoryEntryDraft draftWithEmptySku = InventoryEntryDraft.of("", 12);
    final InventorySync inventorySync = getInventorySync(30, false);
    final InventorySyncStatistics stats = inventorySync.sync(singletonList(draftWithEmptySku)).toCompletableFuture().join();
    assertThat(stats).hasValues(1, 0, 0, 1);
    assertThat(errorCallBackMessages).hasSize(1);
    assertThat(errorCallBackMessages.get(0)).isEqualTo("InventoryEntryDraft doesn't have a SKU. " + "Please make sure all inventory entry drafts have SKUs.");
    assertThat(errorCallBackExceptions).hasSize(1);
    assertThat(errorCallBackExceptions.get(0)).isEqualTo(null);
}
Also used : InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) Test(org.junit.jupiter.api.Test)

Example 25 with InventorySyncStatistics

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

the class InventorySyncTest method sync_WithNullInInputList_ShouldIncrementFailedStatistics.

@Test
void sync_WithNullInInputList_ShouldIncrementFailedStatistics() {
    final InventoryService inventoryService = getMockInventoryService(existingInventories, mock(InventoryEntry.class), mock(InventoryEntry.class));
    final ChannelService channelService = getMockChannelService(getMockSupplyChannel(REF_3, KEY_3));
    final InventorySyncOptions options = getInventorySyncOptions(3, false);
    final InventorySync inventorySync = new InventorySync(options, inventoryService, channelService, mock(TypeService.class));
    final InventorySyncStatistics stats = inventorySync.sync(singletonList(null)).toCompletableFuture().join();
    assertThat(stats).hasValues(1, 0, 0, 1);
    assertThat(errorCallBackMessages).isNotEmpty();
    assertThat(errorCallBackMessages.get(0)).isEqualTo("InventoryEntryDraft is null.");
    assertThat(errorCallBackExceptions).isNotEmpty();
    assertThat(errorCallBackExceptions.get(0)).isEqualTo(null);
}
Also used : ChannelService(com.commercetools.sync.services.ChannelService) InventorySyncMockUtils.getMockChannelService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService) InventorySyncMockUtils.getMockInventoryService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryService) InventoryService(com.commercetools.sync.services.InventoryService) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) InventorySyncMockUtils.getMockInventoryEntry(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryEntry) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Aggregations

InventorySyncStatistics (com.commercetools.sync.inventories.helpers.InventorySyncStatistics)26 Test (org.junit.jupiter.api.Test)26 InventoryEntryDraft (io.sphere.sdk.inventory.InventoryEntryDraft)22 InventoryEntry (io.sphere.sdk.inventory.InventoryEntry)19 InventorySync (com.commercetools.sync.inventories.InventorySync)12 InventorySyncOptions (com.commercetools.sync.inventories.InventorySyncOptions)12 InventorySyncMockUtils.getMockInventoryEntry (com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryEntry)9 InventorySyncMockUtils.getMockInventoryService (com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryService)9 InventoryService (com.commercetools.sync.services.InventoryService)9 TypeService (com.commercetools.sync.services.TypeService)9 InventorySyncMockUtils.getMockChannelService (com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService)8 ChannelService (com.commercetools.sync.services.ChannelService)8 Channel (io.sphere.sdk.channels.Channel)7 InventoryITUtils.getInventoryEntryBySkuAndSupplyChannel (com.commercetools.sync.integration.inventories.utils.InventoryITUtils.getInventoryEntryBySkuAndSupplyChannel)6 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)4 ChannelQuery (io.sphere.sdk.channels.queries.ChannelQuery)4 SyncException (com.commercetools.sync.commons.exceptions.SyncException)3 QuadConsumer (com.commercetools.sync.commons.utils.QuadConsumer)3 ChannelITUtils.deleteChannelsFromTargetAndSource (com.commercetools.sync.integration.commons.utils.ChannelITUtils.deleteChannelsFromTargetAndSource)3 ITUtils.deleteTypesFromTargetAndSource (com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypesFromTargetAndSource)3