Search in sources :

Example 1 with ProductService

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);
}
Also used : ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Reference(io.sphere.sdk.models.Reference) PRODUCT_KEY_2_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryITUtils.getCategoryDrafts(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts) ProductSyncMockUtils.createProductDraftBuilder(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder) UpdateAction(io.sphere.sdk.commands.UpdateAction) StringUtils(org.apache.commons.lang3.StringUtils) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) AfterAll(org.junit.jupiter.api.AfterAll) ChangeName(io.sphere.sdk.products.commands.updateactions.ChangeName) Collections.singleton(java.util.Collections.singleton) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) OLD_CATEGORY_CUSTOM_TYPE_NAME(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) PRODUCT_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Product(io.sphere.sdk.products.Product) CategoryITUtils.getReferencesWithIds(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ChangeSlug(io.sphere.sdk.products.commands.updateactions.ChangeSlug) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) List(java.util.List) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ProductProjectionType(io.sphere.sdk.products.ProductProjectionType) Optional(java.util.Optional) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) AddExternalImage(io.sphere.sdk.products.commands.updateactions.AddExternalImage) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) ProductType(io.sphere.sdk.producttypes.ProductType) ProductITUtils.deleteAllProducts(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) ImageDimensions(io.sphere.sdk.products.ImageDimensions) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Image(io.sphere.sdk.products.Image) Collections.emptySet(java.util.Collections.emptySet) QueryPredicate(io.sphere.sdk.queries.QueryPredicate) SetKey(io.sphere.sdk.products.commands.updateactions.SetKey) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) ProductSyncMockUtils.createRandomCategoryOrderHints(com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints) ProductService(com.commercetools.sync.services.ProductService) Collections(java.util.Collections) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) ProductService(com.commercetools.sync.services.ProductService) SphereClient(io.sphere.sdk.client.SphereClient) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) LocalizedString(io.sphere.sdk.models.LocalizedString) ExecutionException(java.util.concurrent.ExecutionException) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) Test(org.junit.jupiter.api.Test)

Example 2 with ProductService

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();
}
Also used : ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Reference(io.sphere.sdk.models.Reference) PRODUCT_KEY_2_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryITUtils.getCategoryDrafts(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts) ProductSyncMockUtils.createProductDraftBuilder(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder) UpdateAction(io.sphere.sdk.commands.UpdateAction) StringUtils(org.apache.commons.lang3.StringUtils) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) AfterAll(org.junit.jupiter.api.AfterAll) ChangeName(io.sphere.sdk.products.commands.updateactions.ChangeName) Collections.singleton(java.util.Collections.singleton) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) OLD_CATEGORY_CUSTOM_TYPE_NAME(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) PRODUCT_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Product(io.sphere.sdk.products.Product) CategoryITUtils.getReferencesWithIds(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ChangeSlug(io.sphere.sdk.products.commands.updateactions.ChangeSlug) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) List(java.util.List) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ProductProjectionType(io.sphere.sdk.products.ProductProjectionType) Optional(java.util.Optional) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) AddExternalImage(io.sphere.sdk.products.commands.updateactions.AddExternalImage) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) ProductType(io.sphere.sdk.producttypes.ProductType) ProductITUtils.deleteAllProducts(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) ImageDimensions(io.sphere.sdk.products.ImageDimensions) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Image(io.sphere.sdk.products.Image) Collections.emptySet(java.util.Collections.emptySet) QueryPredicate(io.sphere.sdk.queries.QueryPredicate) SetKey(io.sphere.sdk.products.commands.updateactions.SetKey) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) ProductSyncMockUtils.createRandomCategoryOrderHints(com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints) ProductService(com.commercetools.sync.services.ProductService) Collections(java.util.Collections) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ProductService(com.commercetools.sync.services.ProductService) SphereClient(io.sphere.sdk.client.SphereClient) Product(io.sphere.sdk.products.Product) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) LocalizedString(io.sphere.sdk.models.LocalizedString) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) Test(org.junit.jupiter.api.Test)

Example 3 with ProductService

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));
}
Also used : ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Reference(io.sphere.sdk.models.Reference) PRODUCT_KEY_2_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryITUtils.getCategoryDrafts(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts) ProductSyncMockUtils.createProductDraftBuilder(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder) UpdateAction(io.sphere.sdk.commands.UpdateAction) StringUtils(org.apache.commons.lang3.StringUtils) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) AfterAll(org.junit.jupiter.api.AfterAll) ChangeName(io.sphere.sdk.products.commands.updateactions.ChangeName) Collections.singleton(java.util.Collections.singleton) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) OLD_CATEGORY_CUSTOM_TYPE_NAME(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) PRODUCT_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Product(io.sphere.sdk.products.Product) CategoryITUtils.getReferencesWithIds(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ChangeSlug(io.sphere.sdk.products.commands.updateactions.ChangeSlug) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) List(java.util.List) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ProductProjectionType(io.sphere.sdk.products.ProductProjectionType) Optional(java.util.Optional) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) AddExternalImage(io.sphere.sdk.products.commands.updateactions.AddExternalImage) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) ProductType(io.sphere.sdk.producttypes.ProductType) ProductITUtils.deleteAllProducts(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) ImageDimensions(io.sphere.sdk.products.ImageDimensions) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Image(io.sphere.sdk.products.Image) Collections.emptySet(java.util.Collections.emptySet) QueryPredicate(io.sphere.sdk.queries.QueryPredicate) SetKey(io.sphere.sdk.products.commands.updateactions.SetKey) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) ProductSyncMockUtils.createRandomCategoryOrderHints(com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints) ProductService(com.commercetools.sync.services.ProductService) Collections(java.util.Collections) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ProductService(com.commercetools.sync.services.ProductService) SphereClient(io.sphere.sdk.client.SphereClient) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) LocalizedString(io.sphere.sdk.models.LocalizedString) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) Test(org.junit.jupiter.api.Test)

Example 4 with ProductService

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);
}
Also used : ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Reference(io.sphere.sdk.models.Reference) PRODUCT_KEY_2_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryITUtils.getCategoryDrafts(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts) ProductSyncMockUtils.createProductDraftBuilder(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder) UpdateAction(io.sphere.sdk.commands.UpdateAction) StringUtils(org.apache.commons.lang3.StringUtils) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) AfterAll(org.junit.jupiter.api.AfterAll) ChangeName(io.sphere.sdk.products.commands.updateactions.ChangeName) Collections.singleton(java.util.Collections.singleton) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) OLD_CATEGORY_CUSTOM_TYPE_NAME(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) PRODUCT_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Product(io.sphere.sdk.products.Product) CategoryITUtils.getReferencesWithIds(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ChangeSlug(io.sphere.sdk.products.commands.updateactions.ChangeSlug) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) List(java.util.List) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ProductProjectionType(io.sphere.sdk.products.ProductProjectionType) Optional(java.util.Optional) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) AddExternalImage(io.sphere.sdk.products.commands.updateactions.AddExternalImage) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) ProductType(io.sphere.sdk.producttypes.ProductType) ProductITUtils.deleteAllProducts(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) ImageDimensions(io.sphere.sdk.products.ImageDimensions) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Image(io.sphere.sdk.products.Image) Collections.emptySet(java.util.Collections.emptySet) QueryPredicate(io.sphere.sdk.queries.QueryPredicate) SetKey(io.sphere.sdk.products.commands.updateactions.SetKey) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) ProductSyncMockUtils.createRandomCategoryOrderHints(com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints) ProductService(com.commercetools.sync.services.ProductService) Collections(java.util.Collections) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with ProductService

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);
}
Also used : ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Reference(io.sphere.sdk.models.Reference) PRODUCT_KEY_2_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryITUtils.getCategoryDrafts(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts) ProductSyncMockUtils.createProductDraftBuilder(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder) UpdateAction(io.sphere.sdk.commands.UpdateAction) StringUtils(org.apache.commons.lang3.StringUtils) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) AfterAll(org.junit.jupiter.api.AfterAll) ChangeName(io.sphere.sdk.products.commands.updateactions.ChangeName) Collections.singleton(java.util.Collections.singleton) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) OLD_CATEGORY_CUSTOM_TYPE_NAME(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) PRODUCT_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Product(io.sphere.sdk.products.Product) CategoryITUtils.getReferencesWithIds(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ChangeSlug(io.sphere.sdk.products.commands.updateactions.ChangeSlug) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) List(java.util.List) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ProductProjectionType(io.sphere.sdk.products.ProductProjectionType) Optional(java.util.Optional) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) AddExternalImage(io.sphere.sdk.products.commands.updateactions.AddExternalImage) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) ProductType(io.sphere.sdk.producttypes.ProductType) ProductITUtils.deleteAllProducts(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) ImageDimensions(io.sphere.sdk.products.ImageDimensions) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Image(io.sphere.sdk.products.Image) Collections.emptySet(java.util.Collections.emptySet) QueryPredicate(io.sphere.sdk.queries.QueryPredicate) SetKey(io.sphere.sdk.products.commands.updateactions.SetKey) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) ProductSyncMockUtils.createRandomCategoryOrderHints(com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints) ProductService(com.commercetools.sync.services.ProductService) Collections(java.util.Collections) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) ProductService(com.commercetools.sync.services.ProductService) SphereClient(io.sphere.sdk.client.SphereClient) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) LocalizedString(io.sphere.sdk.models.LocalizedString) ExecutionException(java.util.concurrent.ExecutionException) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

ProductService (com.commercetools.sync.services.ProductService)6 OLD_CATEGORY_CUSTOM_TYPE_KEY (com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY)5 OLD_CATEGORY_CUSTOM_TYPE_NAME (com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME)5 CategoryITUtils.createCategories (com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories)5 CategoryITUtils.createCategoriesCustomType (com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType)5 CategoryITUtils.getCategoryDrafts (com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts)5 CategoryITUtils.getReferencesWithIds (com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds)5 ProductITUtils.deleteAllProducts (com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts)5 ProductITUtils.deleteProductSyncTestData (com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData)5 ProductTypeITUtils.createProductType (com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType)5 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)5 PRODUCT_KEY_1_RESOURCE_PATH (com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH)5 PRODUCT_KEY_2_RESOURCE_PATH (com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH)5 PRODUCT_TYPE_RESOURCE_PATH (com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH)5 ProductSyncMockUtils.createProductDraft (com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft)5 ProductSyncMockUtils.createProductDraftBuilder (com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder)5 ProductSyncMockUtils.createRandomCategoryOrderHints (com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints)5 ProductSyncOptions (com.commercetools.sync.products.ProductSyncOptions)5 ProductSyncOptionsBuilder (com.commercetools.sync.products.ProductSyncOptionsBuilder)5 ProductServiceImpl (com.commercetools.sync.services.impl.ProductServiceImpl)5