use of com.commercetools.sync.services.impl.ProductServiceImpl in project commercetools-sync-java by commercetools.
the class ProductServiceImplIT method fetchProduct_WithBadGatewayException_ShouldFail.
@Test
void fetchProduct_WithBadGatewayException_ShouldFail() {
// preparation
// Mock sphere client to return BadGatewayException on any request.
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
when(spyClient.execute(any(ProductProjectionQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
final ProductSyncOptions spyOptions = ProductSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).warningCallback((exception, oldResource, newResource) -> warningCallBackMessages.add(exception.getMessage())).build();
final ProductService spyProductService = new ProductServiceImpl(spyOptions);
final String productKey = product.getKey();
// test and assertion
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(spyProductService.fetchProduct(productKey)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
use of com.commercetools.sync.services.impl.ProductServiceImpl in project commercetools-sync-java by commercetools.
the class ProductSyncTest method sync_WithErrorCachingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
@SuppressWarnings("unchecked")
void sync_WithErrorCachingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey("productTypeKey")).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final ProductService productService = spy(new ProductServiceImpl(syncOptions));
final ProductTypeService productTypeService = mock(ProductTypeService.class);
when(productTypeService.cacheKeysToIds(any())).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final ProductSync productSync = new ProductSync(syncOptions, productService, productTypeService, mock(CategoryService.class), 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
final ProductSyncStatistics productSyncStatistics = productSync.sync(singletonList(productDraft)).toCompletableFuture().join();
// assertions
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);
assertThat(productSyncStatistics).hasValues(1, 0, 0, 1);
}
use of com.commercetools.sync.services.impl.ProductServiceImpl in project commercetools-sync-java by commercetools.
the class ProductSyncTest method sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
@SuppressWarnings("unchecked")
void sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey("productTypeKey")).taxCategory(null).state(null).build();
final SphereClient mockClient = mock(SphereClient.class);
when(mockClient.execute(any(ProductProjectionQuery.class))).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(mockClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final ProductService productService = spy(new ProductServiceImpl(syncOptions));
final Map<String, String> keyToIds = new HashMap<>();
keyToIds.put(productDraft.getKey(), UUID.randomUUID().toString());
when(productService.cacheKeysToIds(anySet())).thenReturn(completedFuture(keyToIds));
final ProductTypeService productTypeService = mock(ProductTypeService.class);
when(productTypeService.fetchCachedProductTypeId(any())).thenReturn(completedFuture(Optional.of(UUID.randomUUID().toString())));
final CategoryService categoryService = mock(CategoryService.class);
when(categoryService.fetchMatchingCategoriesByKeys(any())).thenReturn(completedFuture(emptySet()));
final ProductSync productSync = new ProductSync(syncOptions, 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
final ProductSyncStatistics productSyncStatistics = productSync.sync(singletonList(productDraft)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to fetch existing products");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(CompletionException.class).hasCauseExactlyInstanceOf(SphereException.class);
assertThat(productSyncStatistics).hasValues(1, 0, 0, 1);
}
use of com.commercetools.sync.services.impl.ProductServiceImpl in project commercetools-sync-java by commercetools.
the class ProductServiceImplIT method cacheKeysToIds_WithAlreadyCachedKeys_ShouldNotMakeRequestsAndReturnCurrentCache.
@Test
void cacheKeysToIds_WithAlreadyCachedKeys_ShouldNotMakeRequestsAndReturnCurrentCache() {
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).warningCallback((exception, oldResource, newResource) -> warningCallBackMessages.add(exception.getMessage())).build();
final ProductService spyProductService = new ProductServiceImpl(productSyncOptions);
final ProductDraft productDraft1 = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, productType.toReference()).categories(emptyList()).taxCategory(null).state(null).categoryOrderHints(null).build();
Product product2 = CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraft1)).toCompletableFuture().join();
Set<String> keys = Arrays.asList(product.getKey(), product2.getKey()).stream().collect(Collectors.toSet());
Map<String, String> cache = spyProductService.cacheKeysToIds(keys).toCompletableFuture().join();
assertThat(cache).hasSize(2);
// Attempt to cache same (already cached) key.
cache = spyProductService.cacheKeysToIds(singleton(product.getKey())).toCompletableFuture().join();
assertThat(cache).hasSize(2);
assertThat(cache).containsKeys(product.getKey(), product2.getKey());
// verify only 1 request was made to fetch id the first time, but not second time since it's
// already in cache.
verify(spyClient, times(1)).execute(any());
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
}
use of com.commercetools.sync.services.impl.ProductServiceImpl in project commercetools-sync-java by commercetools.
the class ProductServiceImplIT method createProduct_WithValidProduct_ShouldCreateProductAndCacheId.
@Test
void createProduct_WithValidProduct_ShouldCreateProductAndCacheId() {
// preparation
final ProductDraft productDraft1 = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, productType.toReference()).taxCategory(null).state(null).categories(emptyList()).categoryOrderHints(null).build();
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final ProductSyncOptions spyOptions = ProductSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final ProductService spyProductService = new ProductServiceImpl(spyOptions);
// test
final Optional<ProductProjection> createdProductOptional = spyProductService.createProduct(productDraft1).toCompletableFuture().join();
// assertion
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
// assert CTP state
final Optional<ProductProjection> queriedOptional = CTP_TARGET_CLIENT.execute(ProductProjectionQuery.ofCurrent().withPredicates(QueryPredicate.of(format("key = \"%s\"", productDraft1.getKey())))).toCompletableFuture().join().head();
assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdProductOptional).hasValueSatisfying(created -> {
assertThat(queried.getKey()).isEqualTo(created.getKey());
assertThat(queried.getName()).isEqualTo(created.getName());
assertThat(queried.getSlug()).isEqualTo(created.getSlug());
}));
// Assert that the created product is cached
final Optional<String> productId = spyProductService.getIdFromCacheOrFetch(productDraft1.getKey()).toCompletableFuture().join();
assertThat(productId).isPresent();
verify(spyClient, times(0)).execute(any(ProductTypeQuery.class));
}
Aggregations