use of com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat in project commercetools-sync-java by commercetools.
the class CartDiscountSyncTest method sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats.
@Test
void sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats() {
// preparation
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final TypeService typeService = spy(new TypeServiceImpl(cartDiscountSyncOptions));
when(typeService.cacheKeysToIds(anySet())).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final CartDiscountSync cartDiscountSync = new CartDiscountSync(cartDiscountSyncOptions, typeService, mock(CartDiscountService.class));
final CartDiscountDraft newCartDiscountDraftWithCustomType = mock(CartDiscountDraft.class);
when(newCartDiscountDraftWithCustomType.getKey()).thenReturn("cart-discount-key");
when(newCartDiscountDraftWithCustomType.getCustom()).thenReturn(CustomFieldsDraft.ofTypeKeyAndJson("typeKey", emptyMap()));
// test
final CartDiscountSyncStatistics cartDiscountSyncStatistics = cartDiscountSync.sync(singletonList(newCartDiscountDraftWithCustomType)).toCompletableFuture().join();
// assertions
AssertionsForStatistics.assertThat(cartDiscountSyncStatistics).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().matches(throwable -> {
assertThat(throwable).isExactlyInstanceOf(CompletionException.class);
assertThat(throwable).hasCauseExactlyInstanceOf(SphereException.class);
return true;
});
}
use of com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat in project commercetools-sync-java by commercetools.
the class ProductSyncTest method sync_WithEmptyAttributeMetaDataMap_ShouldCallErrorCallback.
@Test
@SuppressWarnings("unchecked")
void sync_WithEmptyAttributeMetaDataMap_ShouldCallErrorCallback() {
// preparation
final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH, ResourceIdentifier.ofKey("productTypeKey")).taxCategory(null).state(null).build();
final ProductProjection mockedExistingProduct = readObjectFromResource(PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH, Product.class).toProjection(STAGED);
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final ProductService productService = mock(ProductService.class);
final Map<String, String> keyToIds = new HashMap<>();
keyToIds.put(productDraft.getKey(), UUID.randomUUID().toString());
when(productService.cacheKeysToIds(anySet())).thenReturn(completedFuture(keyToIds));
when(productService.fetchMatchingProductsByKeys(anySet())).thenReturn(completedFuture(singleton(mockedExistingProduct)));
when(productService.updateProduct(any(), any())).thenReturn(completedFuture(mockedExistingProduct));
final ProductTypeService productTypeService = mock(ProductTypeService.class);
when(productTypeService.fetchCachedProductTypeId(any())).thenReturn(completedFuture(Optional.of(UUID.randomUUID().toString())));
when(productTypeService.fetchCachedProductAttributeMetaDataMap(any())).thenReturn(completedFuture(Optional.empty()));
final CategoryService categoryService = mock(CategoryService.class);
when(categoryService.fetchMatchingCategoriesByKeys(any())).thenReturn(completedFuture(emptySet()));
final ProductSyncOptions spyProductSyncOptions = spy(productSyncOptions);
final ProductSync productSync = new ProductSync(spyProductSyncOptions, productService, productTypeService, categoryService, mock(TypeService.class), mock(ChannelService.class), mock(CustomerGroupService.class), mock(TaxCategoryService.class), mock(StateService.class), mock(UnresolvedReferencesService.class), mock(CustomObjectService.class), mock(CustomerService.class));
// test
ProductSyncStatistics productSyncStatistics = productSync.sync(singletonList(productDraft)).toCompletableFuture().join();
// assertion
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to update Product with key: 'productKey1'. Reason: Failed to" + " fetch a productType for the product to build the products' attributes metadata.");
AssertionsForStatistics.assertThat(productSyncStatistics).hasValues(1, 0, 0, 1);
}
use of com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat 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);
}
use of com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat in project commercetools-sync-java by commercetools.
the class ShoppingListSyncIT method sync_WithUpdatedCustomerOnShoppingList_ShouldReturnProperStatistics.
@Test
void sync_WithUpdatedCustomerOnShoppingList_ShouldReturnProperStatistics() {
final List<ShoppingList> shoppingLists = CTP_SOURCE_CLIENT.execute(buildShoppingListQuery()).toCompletableFuture().join().getResults();
createSampleCustomerJaneDoe(CTP_SOURCE_CLIENT);
final Customer sampleCustomerJaneDoe = createSampleCustomerJaneDoe(CTP_TARGET_CLIENT);
final List<ShoppingListDraft> updatedShoppingListDrafts = ShoppingListTransformUtils.toShoppingListDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, shoppingLists).join().stream().map(shoppingListDraft -> ShoppingListDraftBuilder.of(shoppingListDraft).name(LocalizedString.ofEnglish("second-shopping-list")).anonymousId(null).customer(ResourceIdentifier.ofKey(sampleCustomerJaneDoe.getKey())).build()).collect(Collectors.toList());
final ShoppingListSyncStatistics shoppingListSyncStatistics = shoppingListSync.sync(updatedShoppingListDrafts).toCompletableFuture().join();
assertThat(errorMessages).isEmpty();
assertThat(warningMessages).isEmpty();
assertThat(exceptions).isEmpty();
// order is important, otherwise the error below could occur:
// "message" : "The resource was already claimed by a customer..
// "action" : {
// "action" : "setAnonymousId"
// }
assertThat(updateActionList).containsExactly(ChangeName.of(LocalizedString.ofEnglish("second-shopping-list")), SetAnonymousId.of(null), SetCustomer.of(Reference.of(Customer.referenceTypeId(), sampleCustomerJaneDoe.getId())));
AssertionsForStatistics.assertThat(shoppingListSyncStatistics).hasValues(2, 1, 1, 0);
assertThat(shoppingListSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 shopping lists were processed in total " + "(1 created, 1 updated and 0 failed to sync).");
}
Aggregations