Search in sources :

Example 26 with InventorySyncStatistics

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

the class InventorySyncTest method sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats.

@Test
void sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats() {
    // preparation
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, actions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception.getCause());
    }).build();
    final TypeService typeService = spy(new TypeServiceImpl(inventorySyncOptions));
    when(typeService.cacheKeysToIds(anySet())).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final InventorySync inventorySync = new InventorySync(inventorySyncOptions, mock(InventoryService.class), mock(ChannelService.class), typeService);
    final InventoryEntryDraft newInventoryDraftWithCustomType = mock(InventoryEntryDraft.class);
    when(newInventoryDraftWithCustomType.getSku()).thenReturn("sku");
    when(newInventoryDraftWithCustomType.getCustom()).thenReturn(CustomFieldsDraft.ofTypeKeyAndJson("typeKey", emptyMap()));
    // test
    final InventorySyncStatistics inventorySyncStatistics = inventorySync.sync(singletonList(newInventoryDraftWithCustomType)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(inventorySyncStatistics).hasValues(1, 0, 0, 1);
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to build a cache of keys to ids.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(CompletionException.class).hasCauseExactlyInstanceOf(SphereException.class);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Reference(io.sphere.sdk.models.Reference) ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ZonedDateTime(java.time.ZonedDateTime) InventorySyncMockUtils.getCompletionStageWithException(com.commercetools.sync.inventories.InventorySyncMockUtils.getCompletionStageWithException) Channel(io.sphere.sdk.channels.Channel) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) InventorySyncMockUtils.getMockSupplyChannel(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockSupplyChannel) InventorySyncMockUtils.getMockInventoryService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryService) Collections.emptyList(java.util.Collections.emptyList) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) String.format(java.lang.String.format) ZoneId(java.time.ZoneId) Test(org.junit.jupiter.api.Test) InventoryEntryDraftBuilder(io.sphere.sdk.inventory.InventoryEntryDraftBuilder) ChannelService(com.commercetools.sync.services.ChannelService) List(java.util.List) SphereInternalUtils.asSet(io.sphere.sdk.utils.SphereInternalUtils.asSet) Optional(java.util.Optional) InventorySyncMockUtils.getMockInventoryEntry(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryEntry) AssertionsForStatistics(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) InventorySyncMockUtils.getMockChannelService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService) InventoryService(com.commercetools.sync.services.InventoryService) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) TypeService(com.commercetools.sync.services.TypeService) SphereException(io.sphere.sdk.models.SphereException) Collections.emptyMap(java.util.Collections.emptyMap) Mockito.when(org.mockito.Mockito.when) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Collections(java.util.Collections) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER(com.commercetools.sync.commons.helpers.BaseReferenceResolver.BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InventorySyncMockUtils.getMockInventoryService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryService) InventoryService(com.commercetools.sync.services.InventoryService) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) SphereException(io.sphere.sdk.models.SphereException) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) ChannelService(com.commercetools.sync.services.ChannelService) InventorySyncMockUtils.getMockChannelService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService) SphereClient(io.sphere.sdk.client.SphereClient) CompletionException(java.util.concurrent.CompletionException) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) 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