Search in sources :

Example 1 with PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH

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);
}
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) Product(io.sphere.sdk.products.Product) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductDraft(io.sphere.sdk.products.ProductDraft) SphereClient(io.sphere.sdk.client.SphereClient) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) Test(org.junit.jupiter.api.Test)

Aggregations

AssertionsForStatistics (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics)1 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)1 PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH (com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH)1 PRODUCT_KEY_2_RESOURCE_PATH (com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH)1 ProductSyncMockUtils.createProductDraftBuilder (com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder)1 ProductSyncStatistics (com.commercetools.sync.products.helpers.ProductSyncStatistics)1 com.commercetools.sync.services (com.commercetools.sync.services)1 ProductServiceImpl (com.commercetools.sync.services.impl.ProductServiceImpl)1 SphereClient (io.sphere.sdk.client.SphereClient)1 SphereJsonUtils.readObjectFromResource (io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource)1 LocalizedString.ofEnglish (io.sphere.sdk.models.LocalizedString.ofEnglish)1 ResourceIdentifier (io.sphere.sdk.models.ResourceIdentifier)1 SphereException (io.sphere.sdk.models.SphereException)1 Product (io.sphere.sdk.products.Product)1 ProductDraft (io.sphere.sdk.products.ProductDraft)1 ProductDraftBuilder (io.sphere.sdk.products.ProductDraftBuilder)1 ProductProjection (io.sphere.sdk.products.ProductProjection)1 STAGED (io.sphere.sdk.products.ProductProjectionType.STAGED)1 ProductProjectionQuery (io.sphere.sdk.products.queries.ProductProjectionQuery)1 ArrayList (java.util.ArrayList)1