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);
}
Aggregations