Search in sources :

Example 6 with ProductTypeServiceImpl

use of com.commercetools.sync.services.impl.ProductTypeServiceImpl in project commercetools-sync-java by commercetools.

the class ProductTypeSyncTest method sync_WithErrorCachingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithErrorCachingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    // preparation
    final AttributeDefinitionDraft nestedTypeAttrDefDraft = AttributeDefinitionDraftBuilder.of(NestedAttributeType.of(referenceOfId("x")), "validNested", ofEnglish("koko"), true).build();
    final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", singletonList(nestedTypeAttrDefDraft));
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final SphereClient sphereClient = mock(SphereClient.class);
    final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(sphereClient).errorCallback((exception, oldResource, newResource, actions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception);
    }).build();
    final ProductTypeService mockProductTypeService = new ProductTypeServiceImpl(syncOptions);
    when(sphereClient.execute(any(ResourceKeyIdGraphQlRequest.class))).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions, mockProductTypeService);
    // test
    final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
    // assertions
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo("Failed to build a cache of keys to ids.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(SphereException.class);
    assertThat(productTypeSyncStatistics).hasValues(1, 0, 0, 1, 0);
}
Also used : Reference(io.sphere.sdk.models.Reference) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductTypeCreateCommand(io.sphere.sdk.producttypes.commands.ProductTypeCreateCommand) Collections.singletonList(java.util.Collections.singletonList) Collections.singleton(java.util.Collections.singleton) ProductTypeService(com.commercetools.sync.services.ProductTypeService) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) ProductTypeUpdateCommand(io.sphere.sdk.producttypes.commands.ProductTypeUpdateCommand) ProductTypeDraftBuilder(io.sphere.sdk.producttypes.ProductTypeDraftBuilder) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Collections.emptyList(java.util.Collections.emptyList) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductTypeServiceImpl(com.commercetools.sync.services.impl.ProductTypeServiceImpl) Sets.newLinkedHashSet(org.assertj.core.util.Sets.newLinkedHashSet) Optional(java.util.Optional) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) Mockito.mock(org.mockito.Mockito.mock) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) SphereException(io.sphere.sdk.models.SphereException) ChangeDescription(io.sphere.sdk.producttypes.commands.updateactions.ChangeDescription) Collections.emptyMap(java.util.Collections.emptyMap) AttributeDefinitionDraftBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder) Collections.emptySet(java.util.Collections.emptySet) ProductType.referenceOfId(io.sphere.sdk.producttypes.ProductType.referenceOfId) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Lists.list(org.assertj.core.util.Lists.list) Mockito.when(org.mockito.Mockito.when) ChangeName(io.sphere.sdk.producttypes.commands.updateactions.ChangeName) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) NestedAttributeType(io.sphere.sdk.products.attributes.NestedAttributeType) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Collections(java.util.Collections) ArrayList(java.util.ArrayList) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) ProductTypeServiceImpl(com.commercetools.sync.services.impl.ProductTypeServiceImpl) SphereException(io.sphere.sdk.models.SphereException) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) ProductTypeService(com.commercetools.sync.services.ProductTypeService) SphereClient(io.sphere.sdk.client.SphereClient) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.jupiter.api.Test)

Example 7 with ProductTypeServiceImpl

use of com.commercetools.sync.services.impl.ProductTypeServiceImpl in project commercetools-sync-java by commercetools.

the class ProductTypeSyncTest method sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback.

@Test
void sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback() {
    // preparation
    final ProductTypeDraft newProductTypeDraft = ProductTypeDraftBuilder.of("newProductType", "productType", "a cool type", emptyList()).build();
    final SphereClient sphereClient = mock(SphereClient.class);
    final ProductTypeSyncOptions productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(sphereClient).build();
    final ProductType mockedExistingProductType = mock(ProductType.class);
    when(mockedExistingProductType.getKey()).thenReturn(newProductTypeDraft.getKey());
    when(mockedExistingProductType.getId()).thenReturn(UUID.randomUUID().toString());
    final ProductTypeService productTypeService = new ProductTypeServiceImpl(productTypeSyncOptions);
    final PagedQueryResult<ProductType> productTypePagedQueryResult = spy(PagedQueryResult.empty());
    when(productTypePagedQueryResult.getResults()).thenReturn(singletonList(mockedExistingProductType));
    when(sphereClient.execute(any(ProductTypeQuery.class))).thenReturn(completedFuture(productTypePagedQueryResult));
    when(sphereClient.execute(any(ProductTypeUpdateCommand.class))).thenReturn(completedFuture(mockedExistingProductType));
    final ProductTypeSyncOptions spyProductTypeSyncOptions = spy(productTypeSyncOptions);
    // test
    new ProductTypeSync(spyProductTypeSyncOptions, productTypeService).sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
    // assertion
    verify(spyProductTypeSyncOptions).applyBeforeUpdateCallback(any(), any(), any());
    verify(spyProductTypeSyncOptions, never()).applyBeforeCreateCallback(newProductTypeDraft);
}
Also used : ProductTypeService(com.commercetools.sync.services.ProductTypeService) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) ProductTypeUpdateCommand(io.sphere.sdk.producttypes.commands.ProductTypeUpdateCommand) SphereClient(io.sphere.sdk.client.SphereClient) ProductType(io.sphere.sdk.producttypes.ProductType) ProductTypeServiceImpl(com.commercetools.sync.services.impl.ProductTypeServiceImpl) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Test(org.junit.jupiter.api.Test)

Example 8 with ProductTypeServiceImpl

use of com.commercetools.sync.services.impl.ProductTypeServiceImpl in project commercetools-sync-java by commercetools.

the class ProductTypeServiceImplIT method setup.

/**
 * Deletes product types from the target CTP project, then it populates the project with test
 * data.
 */
@BeforeEach
void setup() {
    errorCallBackMessages = new ArrayList<>();
    errorCallBackExceptions = new ArrayList<>();
    deleteProductTypes(CTP_TARGET_CLIENT);
    createProductType(OLD_PRODUCT_TYPE_KEY, OLD_PRODUCT_TYPE_LOCALE, OLD_PRODUCT_TYPE_NAME, CTP_TARGET_CLIENT);
    final ProductTypeSyncOptions productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
    productTypeService = new ProductTypeServiceImpl(productTypeSyncOptions);
}
Also used : ProductTypeSyncOptions(com.commercetools.sync.producttypes.ProductTypeSyncOptions) ProductTypeServiceImpl(com.commercetools.sync.services.impl.ProductTypeServiceImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 9 with ProductTypeServiceImpl

use of com.commercetools.sync.services.impl.ProductTypeServiceImpl in project commercetools-sync-java by commercetools.

the class ProductTypeSyncTest method sync_WithErrorCachingKeysButNoKeysToCache_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithErrorCachingKeysButNoKeysToCache_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    // preparation
    final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", emptyList());
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final SphereClient sphereClient = mock(SphereClient.class);
    final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(sphereClient).errorCallback((exception, oldResource, newResource, actions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception);
    }).build();
    final ProductTypeService mockProductTypeService = new ProductTypeServiceImpl(syncOptions);
    when(sphereClient.execute(any(ProductTypeQuery.class))).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions, mockProductTypeService);
    // test
    final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
    // assertions
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo("Failed to fetch existing product types with keys: '[foo]'.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(SphereException.class);
    assertThat(productTypeSyncStatistics).hasValues(1, 0, 0, 1, 0);
}
Also used : Reference(io.sphere.sdk.models.Reference) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductTypeCreateCommand(io.sphere.sdk.producttypes.commands.ProductTypeCreateCommand) Collections.singletonList(java.util.Collections.singletonList) Collections.singleton(java.util.Collections.singleton) ProductTypeService(com.commercetools.sync.services.ProductTypeService) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) ProductTypeUpdateCommand(io.sphere.sdk.producttypes.commands.ProductTypeUpdateCommand) ProductTypeDraftBuilder(io.sphere.sdk.producttypes.ProductTypeDraftBuilder) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Collections.emptyList(java.util.Collections.emptyList) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductTypeServiceImpl(com.commercetools.sync.services.impl.ProductTypeServiceImpl) Sets.newLinkedHashSet(org.assertj.core.util.Sets.newLinkedHashSet) Optional(java.util.Optional) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) Mockito.mock(org.mockito.Mockito.mock) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) SphereException(io.sphere.sdk.models.SphereException) ChangeDescription(io.sphere.sdk.producttypes.commands.updateactions.ChangeDescription) Collections.emptyMap(java.util.Collections.emptyMap) AttributeDefinitionDraftBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder) Collections.emptySet(java.util.Collections.emptySet) ProductType.referenceOfId(io.sphere.sdk.producttypes.ProductType.referenceOfId) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Lists.list(org.assertj.core.util.Lists.list) Mockito.when(org.mockito.Mockito.when) ChangeName(io.sphere.sdk.producttypes.commands.updateactions.ChangeName) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) NestedAttributeType(io.sphere.sdk.products.attributes.NestedAttributeType) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Collections(java.util.Collections) ArrayList(java.util.ArrayList) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ProductTypeServiceImpl(com.commercetools.sync.services.impl.ProductTypeServiceImpl) SphereException(io.sphere.sdk.models.SphereException) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) ProductTypeService(com.commercetools.sync.services.ProductTypeService) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) SphereClient(io.sphere.sdk.client.SphereClient) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.jupiter.api.Test)

Aggregations

ProductTypeServiceImpl (com.commercetools.sync.services.impl.ProductTypeServiceImpl)9 ProductTypeService (com.commercetools.sync.services.ProductTypeService)8 SphereClient (io.sphere.sdk.client.SphereClient)8 ProductType (io.sphere.sdk.producttypes.ProductType)8 ProductTypeDraft (io.sphere.sdk.producttypes.ProductTypeDraft)8 ProductTypeQuery (io.sphere.sdk.producttypes.queries.ProductTypeQuery)8 Test (org.junit.jupiter.api.Test)7 ChangeName (io.sphere.sdk.producttypes.commands.updateactions.ChangeName)6 ArrayList (java.util.ArrayList)6 Collections (java.util.Collections)6 Collections.singleton (java.util.Collections.singleton)6 Collections.singletonList (java.util.Collections.singletonList)6 List (java.util.List)6 Optional (java.util.Optional)6 Assertions.as (org.assertj.core.api.Assertions.as)6 ProductTypeSyncOptions (com.commercetools.sync.producttypes.ProductTypeSyncOptions)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 STRING (org.assertj.core.api.InstanceOfAssertFactories.STRING)5 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)5 Mockito.spy (org.mockito.Mockito.spy)5