use of com.commercetools.sync.services.ProductService 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.ProductService 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.ProductService 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));
}
use of com.commercetools.sync.services.ProductService in project commercetools-sync-java by commercetools.
the class ProductServiceImplIT method setupTest.
/**
* Deletes Products and Types from target CTP projects, then it populates target CTP project with
* product test data.
*/
@BeforeEach
void setupTest() {
errorCallBackMessages = new ArrayList<>();
errorCallBackExceptions = new ArrayList<>();
warningCallBackMessages = new ArrayList<>();
deleteAllProducts(CTP_TARGET_CLIENT);
final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).warningCallback((exception, oldResource, newResource) -> warningCallBackMessages.add(exception.getMessage())).build();
// Create a mock new product in the target project.
final ProductDraft productDraft = createProductDraft(PRODUCT_KEY_1_RESOURCE_PATH, productType.toReference(), null, null, categoryReferencesWithIds, createRandomCategoryOrderHints(categoryReferencesWithIds));
product = CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraft)).thenApply(p -> p.toProjection(ProductProjectionType.STAGED)).toCompletableFuture().join();
productService = new ProductServiceImpl(productSyncOptions);
}
use of com.commercetools.sync.services.ProductService in project commercetools-sync-java by commercetools.
the class ProductServiceImplIT method fetchMatchingProductsByKeys_WithBadGateWayExceptionAlways_ShouldFail.
@Test
void fetchMatchingProductsByKeys_WithBadGateWayExceptionAlways_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());
}).build();
final ProductService spyProductService = new ProductServiceImpl(spyOptions);
final Set<String> keys = new HashSet<>();
keys.add(product.getKey());
// test and assert
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(spyProductService.fetchMatchingProductsByKeys(keys)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
Aggregations