Search in sources :

Example 6 with ChannelService

use of com.commercetools.sync.services.ChannelService 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 7 with ChannelService

use of com.commercetools.sync.services.ChannelService 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 8 with ChannelService

use of com.commercetools.sync.services.ChannelService in project commercetools-sync-java by commercetools.

the class InventorySyncTest method sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback.

@Test
void sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback() {
    // preparation
    final InventoryEntryDraft inventoryEntryDraft = InventoryEntryDraftBuilder.of(SKU_1, 1L).build();
    final InventorySyncOptions optionsSpy = spy(getInventorySyncOptions(1, false));
    final InventoryService inventoryService = getMockInventoryService(existingInventories, mock(InventoryEntry.class), mock(InventoryEntry.class));
    final ChannelService channelService = getMockChannelService(getMockSupplyChannel(REF_3, KEY_3));
    final InventorySync inventorySync = new InventorySync(optionsSpy, inventoryService, channelService, mock(TypeService.class));
    // test
    inventorySync.sync(singletonList(inventoryEntryDraft)).toCompletableFuture().join();
    // assertion
    verify(optionsSpy).applyBeforeUpdateCallback(any(), any(), any());
    verify(optionsSpy, never()).applyBeforeCreateCallback(any());
}
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) 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 9 with ChannelService

use of com.commercetools.sync.services.ChannelService in project commercetools-sync-java by commercetools.

the class InventorySyncTest method createMockChannelService.

private ChannelService createMockChannelService() {
    final ChannelService channelService = mock(ChannelService.class);
    when(channelService.fetchCachedChannelId(anyString())).thenAnswer(invocation -> {
        Object argument = invocation.getArguments()[0];
        if (argument.equals(KEY_1)) {
            return completedFuture(Optional.of(REF_1));
        } else if (argument.equals(KEY_2)) {
            return completedFuture(Optional.of(REF_2));
        } else if (argument.equals(KEY_3)) {
            return completedFuture(Optional.of(REF_3));
        }
        return completedFuture(Optional.empty());
    });
    when(channelService.createAndCacheChannel(anyString())).thenAnswer(invocation -> {
        Object argument = invocation.getArguments()[0];
        if (argument.equals(KEY_1)) {
            return completedFuture(Optional.of(getMockSupplyChannel(REF_1, KEY_1)));
        } else if (argument.equals(KEY_2)) {
            return completedFuture(Optional.of(getMockSupplyChannel(REF_2, KEY_2)));
        } else if (argument.equals(KEY_3)) {
            return completedFuture(Optional.of(getMockSupplyChannel(REF_3, KEY_3)));
        }
        return completedFuture(Optional.empty());
    });
    return channelService;
}
Also used : ChannelService(com.commercetools.sync.services.ChannelService) InventorySyncMockUtils.getMockChannelService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService)

Example 10 with ChannelService

use of com.commercetools.sync.services.ChannelService 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

ChannelService (com.commercetools.sync.services.ChannelService)12 InventorySyncMockUtils.getMockChannelService (com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService)11 TypeService (com.commercetools.sync.services.TypeService)10 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 InventoryEntry (io.sphere.sdk.inventory.InventoryEntry)9 Test (org.junit.jupiter.api.Test)9 InventoryEntryDraft (io.sphere.sdk.inventory.InventoryEntryDraft)8 InventorySyncStatistics (com.commercetools.sync.inventories.helpers.InventorySyncStatistics)7 MockUtils.getMockTypeService (com.commercetools.sync.commons.MockUtils.getMockTypeService)1 ProductSyncMockUtils.getMockProductTypeService (com.commercetools.sync.products.ProductSyncMockUtils.getMockProductTypeService)1 ProductSyncOptions (com.commercetools.sync.products.ProductSyncOptions)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1