Search in sources :

Example 6 with PRODUCT_TYPE_KEY_1

use of com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_KEY_1 in project commercetools-sync-java by commercetools.

the class ProductTypeWithNestedAttributeSyncIT method sync_WithNewProductTypeWithFailedFetchOnReferenceResolution_ShouldFail.

@Test
void sync_WithNewProductTypeWithFailedFetchOnReferenceResolution_ShouldFail() {
    // preparation
    final AttributeDefinitionDraft nestedTypeAttr = AttributeDefinitionDraftBuilder.of(AttributeDefinitionBuilder.of("nestedattr", ofEnglish("nestedattr"), NestedAttributeType.of(ProductType.referenceOfId(PRODUCT_TYPE_KEY_5))).build()).searchable(false).build();
    final ProductTypeDraft withMissingNestedTypeRef = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, PRODUCT_TYPE_NAME_1, PRODUCT_TYPE_DESCRIPTION_1, asList(ATTRIBUTE_DEFINITION_DRAFT_1, ATTRIBUTE_DEFINITION_DRAFT_2, nestedTypeAttr));
    final ProductTypeDraft productTypeDraft5 = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_5, PRODUCT_TYPE_NAME_5, PRODUCT_TYPE_DESCRIPTION_5, singletonList(ATTRIBUTE_DEFINITION_DRAFT_3));
    final SphereClient ctpClient = spy(CTP_TARGET_CLIENT);
    final BadGatewayException badGatewayException = new BadGatewayException();
    when(ctpClient.execute(any(ResourceKeyIdGraphQlRequest.class))).thenCallRealMethod();
    when(ctpClient.execute(any(ProductTypeQuery.class))).thenCallRealMethod().thenCallRealMethod().thenReturn(// fail on fetching during resolution
    exceptionallyCompletedFuture(badGatewayException)).thenCallRealMethod();
    productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(ctpClient).batchSize(// this ensures the drafts are in separate batches.
    1).beforeUpdateCallback((actions, draft, oldProductType) -> {
        builtUpdateActions.addAll(actions);
        return actions;
    }).errorCallback((exception, oldResource, newResource, actions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception);
    }).build();
    final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
    // tests
    final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(asList(withMissingNestedTypeRef, productTypeDraft5)).toCompletableFuture().join();
    // assertions
    final Optional<ProductType> productType1 = getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_1);
    assert productType1.isPresent();
    assertThat(errorMessages).containsExactly("Failed to fetch existing product types with keys: '[key_1]'.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(BadGatewayException.class);
    assertThat(builtUpdateActions).isEmpty();
    assertThat(productTypeSyncStatistics).hasValues(2, 1, 0, 0, 1);
    assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 product types were processed in total" + " (1 created, 0 updated, 0 failed to sync and 1 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
    assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).hasSize(1);
    final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>> children = productTypeSyncStatistics.getProductTypeKeysWithMissingParents().get(PRODUCT_TYPE_KEY_5);
    assertThat(children).hasSize(1);
    assertThat(children.get(PRODUCT_TYPE_KEY_1)).containsExactly(nestedTypeAttr);
    final Optional<ProductType> productType5 = getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_5);
    assertThat(productType5).hasValueSatisfying(productType -> {
        assertThat(productType.getName()).isEqualTo(PRODUCT_TYPE_NAME_5);
        assertThat(productType.getDescription()).isEqualTo(PRODUCT_TYPE_DESCRIPTION_5);
        assertThat(productType.getAttributes()).hasSize(1);
    });
    assertThat(productType1).hasValueSatisfying(productType -> assertThat(productType.getAttributes()).hasSize(2));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SyncException(com.commercetools.sync.commons.exceptions.SyncException) ProductTypeITUtils.removeAttributeReferencesAndDeleteProductTypes(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.removeAttributeReferencesAndDeleteProductTypes) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) AddAttributeDefinition(io.sphere.sdk.producttypes.commands.updateactions.AddAttributeDefinition) Collections.singletonList(java.util.Collections.singletonList) AfterAll(org.junit.jupiter.api.AfterAll) Arrays.asList(java.util.Arrays.asList) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) AttributeDefinition(io.sphere.sdk.products.attributes.AttributeDefinition) ProductTypeITUtils.getProductTypeByKey(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.getProductTypeByKey) ProductTypeITUtils.assertAttributesAreEqual(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.assertAttributesAreEqual) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) PRODUCT_TYPE_NAME_1(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_NAME_1) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Collections.emptyList(java.util.Collections.emptyList) PRODUCT_TYPE_NAME_5(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_NAME_5) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PRODUCT_TYPE_NAME_3(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_NAME_3) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.jupiter.api.Test) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) Optional(java.util.Optional) ProductTypeSync(com.commercetools.sync.producttypes.ProductTypeSync) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) ProductTypeSyncOptionsBuilder(com.commercetools.sync.producttypes.ProductTypeSyncOptionsBuilder) Assertions.as(org.assertj.core.api.Assertions.as) PRODUCT_TYPE_KEY_1(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_KEY_1) PRODUCT_TYPE_KEY_3(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_KEY_3) PRODUCT_TYPE_KEY_5(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_KEY_5) AttributeDefinitionBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionBuilder) SetAttributeType(io.sphere.sdk.products.attributes.SetAttributeType) AttributeDefinitionDraftBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder) ProductTypeITUtils.populateProjectWithNestedAttributes(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.populateProjectWithNestedAttributes) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Mockito.when(org.mockito.Mockito.when) ProductTypeSyncOptions(com.commercetools.sync.producttypes.ProductTypeSyncOptions) Resource(io.sphere.sdk.models.Resource) ATTRIBUTE_DEFINITION_DRAFT_3(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.ATTRIBUTE_DEFINITION_DRAFT_3) NestedAttributeType(io.sphere.sdk.products.attributes.NestedAttributeType) RemoveAttributeDefinition(io.sphere.sdk.producttypes.commands.updateactions.RemoveAttributeDefinition) PRODUCT_TYPE_DESCRIPTION_5(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_DESCRIPTION_5) PRODUCT_TYPE_DESCRIPTION_3(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_DESCRIPTION_3) PRODUCT_TYPE_DESCRIPTION_1(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_DESCRIPTION_1) ATTRIBUTE_DEFINITION_DRAFT_2(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.ATTRIBUTE_DEFINITION_DRAFT_2) ATTRIBUTE_DEFINITION_DRAFT_1(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.ATTRIBUTE_DEFINITION_DRAFT_1) ProductTypeSync(com.commercetools.sync.producttypes.ProductTypeSync) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ProductType(io.sphere.sdk.producttypes.ProductType) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) SphereClient(io.sphere.sdk.client.SphereClient) CompletionException(java.util.concurrent.CompletionException) BadGatewayException(io.sphere.sdk.client.BadGatewayException) Test(org.junit.jupiter.api.Test)

Example 7 with PRODUCT_TYPE_KEY_1

use of com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_KEY_1 in project commercetools-sync-java by commercetools.

the class ProductTypeWithNestedAttributeSyncIT method sync_WithUpdatedProductType_WithNewNestedAttributeInSeparateBatch_ShouldUpdateProductTypeAddingAttribute.

@Test
void sync_WithUpdatedProductType_WithNewNestedAttributeInSeparateBatch_ShouldUpdateProductTypeAddingAttribute() {
    // preparation
    final AttributeDefinitionDraft nestedTypeAttr = AttributeDefinitionDraftBuilder.of(AttributeDefinitionBuilder.of("nestedattr", ofEnglish("nestedattr"), SetAttributeType.of(SetAttributeType.of(SetAttributeType.of(SetAttributeType.of(NestedAttributeType.of(ProductType.referenceOfId(PRODUCT_TYPE_KEY_1))))))).build()).searchable(false).build();
    final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, PRODUCT_TYPE_NAME_1, PRODUCT_TYPE_DESCRIPTION_1, asList(ATTRIBUTE_DEFINITION_DRAFT_1, ATTRIBUTE_DEFINITION_DRAFT_2, nestedTypeAttr));
    final ProductTypeDraft productTypeDraft5 = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_5, PRODUCT_TYPE_NAME_5, PRODUCT_TYPE_DESCRIPTION_5, singletonList(ATTRIBUTE_DEFINITION_DRAFT_3));
    productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(1).beforeUpdateCallback((actions, draft, oldProductType) -> {
        builtUpdateActions.addAll(actions);
        return actions;
    }).errorCallback((exception, oldResource, newResource, actions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception);
    }).build();
    final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
    // tests
    final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(asList(newProductTypeDraft, productTypeDraft5)).toCompletableFuture().join();
    // assertions
    final Optional<ProductType> productType1 = getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_1);
    assert productType1.isPresent();
    assertThat(errorMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    assertThat(builtUpdateActions).containsExactly(AddAttributeDefinition.of(AttributeDefinitionDraftBuilder.of(AttributeDefinitionBuilder.of("nestedattr", ofEnglish("nestedattr"), SetAttributeType.of(SetAttributeType.of(SetAttributeType.of(SetAttributeType.of(NestedAttributeType.of(productType1.get())))))).build()).searchable(false).build()));
    assertThat(productTypeSyncStatistics).hasValues(2, 1, 1, 0, 0);
    assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 product types were processed in total" + " (1 created, 1 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
    assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).isEmpty();
    final Optional<ProductType> productType5 = getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_5);
    assertThat(productType5).hasValueSatisfying(productType -> {
        assertThat(productType.getName()).isEqualTo(PRODUCT_TYPE_NAME_5);
        assertThat(productType.getDescription()).isEqualTo(PRODUCT_TYPE_DESCRIPTION_5);
        assertThat(productType.getAttributes()).hasSize(1);
    });
    assertThat(productType1).hasValueSatisfying(productType -> assertThat(productType.getAttributes()).hasSize(3));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SyncException(com.commercetools.sync.commons.exceptions.SyncException) ProductTypeITUtils.removeAttributeReferencesAndDeleteProductTypes(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.removeAttributeReferencesAndDeleteProductTypes) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) AddAttributeDefinition(io.sphere.sdk.producttypes.commands.updateactions.AddAttributeDefinition) Collections.singletonList(java.util.Collections.singletonList) AfterAll(org.junit.jupiter.api.AfterAll) Arrays.asList(java.util.Arrays.asList) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) AttributeDefinition(io.sphere.sdk.products.attributes.AttributeDefinition) ProductTypeITUtils.getProductTypeByKey(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.getProductTypeByKey) ProductTypeITUtils.assertAttributesAreEqual(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.assertAttributesAreEqual) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) PRODUCT_TYPE_NAME_1(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_NAME_1) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Collections.emptyList(java.util.Collections.emptyList) PRODUCT_TYPE_NAME_5(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_NAME_5) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PRODUCT_TYPE_NAME_3(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_NAME_3) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.jupiter.api.Test) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) Optional(java.util.Optional) ProductTypeSync(com.commercetools.sync.producttypes.ProductTypeSync) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) ProductTypeSyncOptionsBuilder(com.commercetools.sync.producttypes.ProductTypeSyncOptionsBuilder) Assertions.as(org.assertj.core.api.Assertions.as) PRODUCT_TYPE_KEY_1(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_KEY_1) PRODUCT_TYPE_KEY_3(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_KEY_3) PRODUCT_TYPE_KEY_5(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_KEY_5) AttributeDefinitionBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionBuilder) SetAttributeType(io.sphere.sdk.products.attributes.SetAttributeType) AttributeDefinitionDraftBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder) ProductTypeITUtils.populateProjectWithNestedAttributes(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.populateProjectWithNestedAttributes) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Mockito.when(org.mockito.Mockito.when) ProductTypeSyncOptions(com.commercetools.sync.producttypes.ProductTypeSyncOptions) Resource(io.sphere.sdk.models.Resource) ATTRIBUTE_DEFINITION_DRAFT_3(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.ATTRIBUTE_DEFINITION_DRAFT_3) NestedAttributeType(io.sphere.sdk.products.attributes.NestedAttributeType) RemoveAttributeDefinition(io.sphere.sdk.producttypes.commands.updateactions.RemoveAttributeDefinition) PRODUCT_TYPE_DESCRIPTION_5(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_DESCRIPTION_5) PRODUCT_TYPE_DESCRIPTION_3(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_DESCRIPTION_3) PRODUCT_TYPE_DESCRIPTION_1(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_DESCRIPTION_1) ATTRIBUTE_DEFINITION_DRAFT_2(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.ATTRIBUTE_DEFINITION_DRAFT_2) ATTRIBUTE_DEFINITION_DRAFT_1(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.ATTRIBUTE_DEFINITION_DRAFT_1) ProductTypeSync(com.commercetools.sync.producttypes.ProductTypeSync) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ProductType(io.sphere.sdk.producttypes.ProductType) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Test(org.junit.jupiter.api.Test)

Aggregations

ATTRIBUTE_DEFINITION_DRAFT_1 (com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.ATTRIBUTE_DEFINITION_DRAFT_1)7 PRODUCT_TYPE_DESCRIPTION_1 (com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_DESCRIPTION_1)7 PRODUCT_TYPE_KEY_1 (com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_KEY_1)7 PRODUCT_TYPE_NAME_1 (com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_NAME_1)7 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)7 ProductTypeSyncOptions (com.commercetools.sync.producttypes.ProductTypeSyncOptions)7 ProductTypeSyncOptionsBuilder (com.commercetools.sync.producttypes.ProductTypeSyncOptionsBuilder)7 BadGatewayException (io.sphere.sdk.client.BadGatewayException)7 SphereClient (io.sphere.sdk.client.SphereClient)7 ProductType (io.sphere.sdk.producttypes.ProductType)7 ProductTypeDraft (io.sphere.sdk.producttypes.ProductTypeDraft)7 ProductTypeQuery (io.sphere.sdk.producttypes.queries.ProductTypeQuery)7 ArrayList (java.util.ArrayList)7 Collections.singletonList (java.util.Collections.singletonList)7 List (java.util.List)7 Optional (java.util.Optional)7 Assertions.as (org.assertj.core.api.Assertions.as)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 AfterAll (org.junit.jupiter.api.AfterAll)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7