use of com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH 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);
}
Aggregations