Search in sources :

Example 1 with ProductServiceImpl

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);
}
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 ProductServiceImpl

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);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) PRODUCT_KEY_2_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH) STAGED(io.sphere.sdk.products.ProductProjectionType.STAGED) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) ProductSyncMockUtils.createProductDraftBuilder(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Collections.singleton(java.util.Collections.singleton) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) Map(java.util.Map) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) SphereException(io.sphere.sdk.models.SphereException) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) com.commercetools.sync.services(com.commercetools.sync.services) Collections.emptyMap(java.util.Collections.emptyMap) PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH) Collections.emptySet(java.util.Collections.emptySet) Collections.emptyList(java.util.Collections.emptyList) CompletionException(java.util.concurrent.CompletionException) Mockito.when(org.mockito.Mockito.when) Product(io.sphere.sdk.products.Product) UUID(java.util.UUID) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) Optional(java.util.Optional) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) AssertionsForStatistics(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) ArrayList(java.util.ArrayList) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) ProductDraft(io.sphere.sdk.products.ProductDraft) SphereClient(io.sphere.sdk.client.SphereClient) SphereException(io.sphere.sdk.models.SphereException) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.jupiter.api.Test)

Example 3 with ProductServiceImpl

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);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) PRODUCT_KEY_2_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH) STAGED(io.sphere.sdk.products.ProductProjectionType.STAGED) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) ProductSyncMockUtils.createProductDraftBuilder(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Collections.singleton(java.util.Collections.singleton) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) Map(java.util.Map) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) SphereException(io.sphere.sdk.models.SphereException) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) com.commercetools.sync.services(com.commercetools.sync.services) Collections.emptyMap(java.util.Collections.emptyMap) PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH) Collections.emptySet(java.util.Collections.emptySet) Collections.emptyList(java.util.Collections.emptyList) CompletionException(java.util.concurrent.CompletionException) Mockito.when(org.mockito.Mockito.when) Product(io.sphere.sdk.products.Product) UUID(java.util.UUID) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) Optional(java.util.Optional) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) AssertionsForStatistics(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) SphereClient(io.sphere.sdk.client.SphereClient) SphereException(io.sphere.sdk.models.SphereException) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.jupiter.api.Test)

Example 4 with ProductServiceImpl

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();
}
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 5 with ProductServiceImpl

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));
}
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)

Aggregations

PRODUCT_KEY_2_RESOURCE_PATH (com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH)7 ProductSyncMockUtils.createProductDraftBuilder (com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder)7 ProductServiceImpl (com.commercetools.sync.services.impl.ProductServiceImpl)7 SphereClient (io.sphere.sdk.client.SphereClient)7 Product (io.sphere.sdk.products.Product)7 ProductDraft (io.sphere.sdk.products.ProductDraft)7 ProductProjection (io.sphere.sdk.products.ProductProjection)7 OLD_CATEGORY_CUSTOM_TYPE_KEY (com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY)4 OLD_CATEGORY_CUSTOM_TYPE_NAME (com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME)4 CategoryITUtils.createCategories (com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories)4 CategoryITUtils.createCategoriesCustomType (com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType)4 CategoryITUtils.getCategoryDrafts (com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts)4 CategoryITUtils.getReferencesWithIds (com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds)4 ProductITUtils.deleteAllProducts (com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts)4 ProductITUtils.deleteProductSyncTestData (com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData)4 ProductTypeITUtils.createProductType (com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType)4 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)4 PRODUCT_KEY_1_RESOURCE_PATH (com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH)4 PRODUCT_TYPE_RESOURCE_PATH (com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH)4 ProductSyncMockUtils.createProductDraft (com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft)4