diff --git a/src/integration-test/java/com/commercetools/sync/integration/commons/utils/ProductTypeITUtils.java b/src/integration-test/java/com/commercetools/sync/integration/commons/utils/ProductTypeITUtils.java index 40b3b6c585..b1bc6f62bd 100644 --- a/src/integration-test/java/com/commercetools/sync/integration/commons/utils/ProductTypeITUtils.java +++ b/src/integration-test/java/com/commercetools/sync/integration/commons/utils/ProductTypeITUtils.java @@ -443,7 +443,11 @@ public static void assertAttributesAreEqual( assertThat(attribute.getLabel()).isEqualTo(attributeDraft.getLabel()); - assertThat(attribute.getType()).isEqualTo(attributeDraft.getType()); + final boolean isAttributeOfTypeSetType = + attribute.getType() instanceof AttributeSetType; + if (!isAttributeOfTypeSetType) { + assertThat(attribute.getType()).isEqualTo(attributeDraft.getType()); + } assertThat(attribute.getInputHint()) .isEqualTo( @@ -461,11 +465,11 @@ public static void assertAttributesAreEqual( ofNullable(attributeDraft.getAttributeConstraint()) .orElse(AttributeConstraintEnum.NONE)); - if (attribute.getType().getClass() == AttributeEnumType.class) { + if (attribute.getType() instanceof AttributeEnumType) { assertPlainEnumsValuesAreEqual( ((AttributeEnumType) attribute.getType()).getValues(), ((AttributeEnumType) attributeDraft.getType()).getValues()); - } else if (attribute.getType().getClass() == AttributeLocalizedEnumType.class) { + } else if (attribute.getType() instanceof AttributeLocalizedEnumType) { assertLocalizedEnumsValuesAreEqual( ((AttributeLocalizedEnumType) attribute.getType()).getValues(), ((AttributeLocalizedEnumType) attributeDraft.getType()).getValues()); diff --git a/src/integration-test/java/com/commercetools/sync/integration/commons/utils/TestUtils.java b/src/integration-test/java/com/commercetools/sync/integration/commons/utils/TestUtils.java index 39f4992ce6..ad71ee2048 100644 --- a/src/integration-test/java/com/commercetools/sync/integration/commons/utils/TestUtils.java +++ b/src/integration-test/java/com/commercetools/sync/integration/commons/utils/TestUtils.java @@ -2,25 +2,9 @@ import static io.vrap.rmf.base.client.utils.json.JsonUtils.fromInputStream; -import java.io.IOException; import java.io.InputStream; -import java.io.UncheckedIOException; -import java.nio.charset.StandardCharsets; -import java.util.Objects; -import org.apache.commons.io.IOUtils; public class TestUtils { - public static String stringFromResource(final String resourcePath) { - try { - return IOUtils.toString( - Objects.requireNonNull( - Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath)), - StandardCharsets.UTF_8); - } catch (final IOException e) { - throw new UncheckedIOException(e); - } - } - public static T readObjectFromResource(final String resourcePath, final Class objectType) { final InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath); diff --git a/src/integration-test/java/com/commercetools/sync/integration/ctpprojectsource/producttypes/ProductTypeWithNestedAttributeSyncIT.java b/src/integration-test/java/com/commercetools/sync/integration/ctpprojectsource/producttypes/ProductTypeWithNestedAttributeSyncIT.java index b6d79e5e2c..a8e6a9acea 100644 --- a/src/integration-test/java/com/commercetools/sync/integration/ctpprojectsource/producttypes/ProductTypeWithNestedAttributeSyncIT.java +++ b/src/integration-test/java/com/commercetools/sync/integration/ctpprojectsource/producttypes/ProductTypeWithNestedAttributeSyncIT.java @@ -2,20 +2,32 @@ import static com.commercetools.api.models.common.LocalizedString.ofEnglish; import static com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat; +import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_DESCRIPTION_5; +import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_KEY_5; +import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_NAME_5; +import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.assertAttributesAreEqual; +import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.getProductTypeByKey; import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.populateProjectWithNestedAttributes; import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.removeAttributeReferencesAndDeleteProductTypes; import static com.commercetools.sync.integration.commons.utils.TestClientUtils.CTP_SOURCE_CLIENT; import static com.commercetools.sync.integration.commons.utils.TestClientUtils.CTP_TARGET_CLIENT; import static org.assertj.core.api.Assertions.assertThat; +import com.commercetools.api.models.common.LocalizedString; +import com.commercetools.api.models.product_type.AttributeDefinition; import com.commercetools.api.models.product_type.AttributeDefinitionDraft; import com.commercetools.api.models.product_type.AttributeDefinitionDraftBuilder; import com.commercetools.api.models.product_type.AttributeNestedType; +import com.commercetools.api.models.product_type.AttributeNestedTypeBuilder; +import com.commercetools.api.models.product_type.AttributeSetType; +import com.commercetools.api.models.product_type.AttributeSetTypeBuilder; import com.commercetools.api.models.product_type.ProductType; import com.commercetools.api.models.product_type.ProductTypeChangeLabelActionBuilder; import com.commercetools.api.models.product_type.ProductTypeDraft; import com.commercetools.api.models.product_type.ProductTypeDraftBuilder; import com.commercetools.api.models.product_type.ProductTypePagedQueryResponse; +import com.commercetools.api.models.product_type.ProductTypeReference; +import com.commercetools.api.models.product_type.ProductTypeReferenceBuilder; import com.commercetools.api.models.product_type.ProductTypeUpdateAction; import com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl; import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache; @@ -27,6 +39,7 @@ import io.vrap.rmf.base.client.ApiHttpResponse; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeEach; @@ -116,6 +129,87 @@ void sync_WithEmptyTargetProject_ShouldReturnProperStatistics() { + " of NestedType attribute definition(s) referencing a missing product type)."); } + @Test + void sync_WithProductTypeReferencingItselfAsAttribute_ShouldCreateProductType() { + // preparation + final AttributeDefinitionDraft nestedTypeAttr = + AttributeDefinitionDraftBuilder.of() + .name("selfReferenceAttr") + .label(LocalizedString.ofEnglish("selfReferenceAttr")) + .type( + AttributeSetTypeBuilder.of() + .elementType( + AttributeNestedTypeBuilder.of() + .typeReference( + ProductTypeReferenceBuilder.of().id(PRODUCT_TYPE_KEY_5).build()) + .build()) + .build()) + .isSearchable(true) + .isRequired(false) + .build(); + + final ProductTypeDraft oldProductTypeDraft = + ProductTypeDraftBuilder.of() + .key(PRODUCT_TYPE_KEY_5) + .name(PRODUCT_TYPE_NAME_5) + .description(PRODUCT_TYPE_DESCRIPTION_5) + .attributes(nestedTypeAttr) + .build(); + + // Sync productDraft with attribute referencing itself to source project + new ProductTypeSync(ProductTypeSyncOptionsBuilder.of(CTP_SOURCE_CLIENT).build()) + .sync(List.of(oldProductTypeDraft)) + .toCompletableFuture() + .join(); + final ProductType oldProductType = + CTP_SOURCE_CLIENT + .productTypes() + .withKey(PRODUCT_TYPE_KEY_5) + .get() + .executeBlocking() + .getBody(); + + // test + final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions); + final ProductTypeSyncStatistics productTypeSyncStatistics = + ProductTypeTransformUtils.toProductTypeDrafts( + CTP_SOURCE_CLIENT, referenceIdToKeyCache, List.of(oldProductType)) + .thenCompose(newDrafts -> productTypeSync.sync(newDrafts)) + .toCompletableFuture() + .join(); + + // assertion + assertThat(errorMessages).isEmpty(); + assertThat(exceptions).isEmpty(); + assertThat(builtUpdateActions).isEmpty(); + assertThat(productTypeSyncStatistics).hasValues(1, 1, 0, 0, 0); + assertThat(productTypeSyncStatistics.getReportMessage()) + .isEqualTo( + "Summary: 1 product types were processed in total" + + " (1 created, 0 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)."); + + final Optional newProductType = + getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_5); + assertThat(newProductType).isPresent(); + assertThat(newProductType) + .hasValueSatisfying( + productType -> { + assertAttributesAreEqual(productType.getAttributes(), List.of(nestedTypeAttr)); + final AttributeDefinition attributeDefinition1 = productType.getAttributes().get(0); + assertThat(attributeDefinition1.getType()).isInstanceOf(AttributeSetType.class); + final AttributeSetType attributeSetType = + (AttributeSetType) attributeDefinition1.getType(); + assertThat(attributeSetType.getElementType()).isInstanceOf(AttributeNestedType.class); + final AttributeNestedType attributeNestedType = + (AttributeNestedType) attributeSetType.getElementType(); + assertThat(attributeNestedType.getTypeReference()) + .isInstanceOf(ProductTypeReference.class); + assertThat(attributeNestedType.getTypeReference().getId()) + .isEqualTo(productType.getId()); + }); + } + @Test void sync_WithOneDraftPerBatchOnEmptyProject_ShouldReturnProperStatistics() { // preparation diff --git a/src/integration-test/java/com/commercetools/sync/integration/externalsource/producttypes/ProductTypeSyncIT.java b/src/integration-test/java/com/commercetools/sync/integration/externalsource/producttypes/ProductTypeSyncIT.java index af3d3b28ff..187eb8a82a 100644 --- a/src/integration-test/java/com/commercetools/sync/integration/externalsource/producttypes/ProductTypeSyncIT.java +++ b/src/integration-test/java/com/commercetools/sync/integration/externalsource/producttypes/ProductTypeSyncIT.java @@ -18,7 +18,6 @@ import com.commercetools.api.defaultconfig.ApiRootBuilder; import com.commercetools.api.models.product_type.AttributeConstraintEnum; import com.commercetools.api.models.product_type.AttributeDefinition; -import com.commercetools.api.models.product_type.AttributeDefinitionBuilder; import com.commercetools.api.models.product_type.AttributeDefinitionDraft; import com.commercetools.api.models.product_type.AttributeDefinitionDraftBuilder; import com.commercetools.api.models.product_type.AttributeLocalizedEnumValueBuilder; @@ -36,7 +35,6 @@ import com.commercetools.sync.producttypes.ProductTypeSyncOptions; import com.commercetools.sync.producttypes.ProductTypeSyncOptionsBuilder; import com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics; -import com.commercetools.sync.producttypes.helpers.ResourceToDraftConverters; import io.vrap.rmf.base.client.ApiHttpMethod; import io.vrap.rmf.base.client.ApiHttpResponse; import io.vrap.rmf.base.client.error.BadGatewayException; @@ -870,8 +868,8 @@ void sync_withProductTypeWithCategoryReference_ShouldAddNewAttributesToTheProduc final ProductTypeSyncOptions productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).build(); - final AttributeDefinition referenceTypeAttr = - AttributeDefinitionBuilder.of() + final AttributeDefinitionDraft referenceTypeAttr = + AttributeDefinitionDraftBuilder.of() .name("referenceTypeAttr") .label(ofEnglish("referenceTypeAttr")) .type( @@ -893,9 +891,7 @@ void sync_withProductTypeWithCategoryReference_ShouldAddNewAttributesToTheProduc .key(PRODUCT_TYPE_KEY_3) .name(PRODUCT_TYPE_NAME_3) .description(PRODUCT_TYPE_DESCRIPTION_3) - .attributes( - ResourceToDraftConverters.toAttributeDefinitionDraftBuilder(referenceTypeAttr) - .build()) + .attributes(referenceTypeAttr) .build(); final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions); @@ -906,10 +902,7 @@ void sync_withProductTypeWithCategoryReference_ShouldAddNewAttributesToTheProduc .key(PRODUCT_TYPE_KEY_3) .name(PRODUCT_TYPE_NAME_3) .description(PRODUCT_TYPE_DESCRIPTION_3) - .attributes( - ATTRIBUTE_DEFINITION_DRAFT_1, - ResourceToDraftConverters.toAttributeDefinitionDraftBuilder(referenceTypeAttr) - .build()) + .attributes(ATTRIBUTE_DEFINITION_DRAFT_1, referenceTypeAttr) .build(); productTypeSync.sync(singletonList(updatedProductTypeDraft)).toCompletableFuture().join(); diff --git a/src/main/java/com/commercetools/sync/producttypes/helpers/ResourceToDraftConverters.java b/src/main/java/com/commercetools/sync/producttypes/helpers/ResourceToDraftConverters.java index 76e339b72c..783e47867f 100644 --- a/src/main/java/com/commercetools/sync/producttypes/helpers/ResourceToDraftConverters.java +++ b/src/main/java/com/commercetools/sync/producttypes/helpers/ResourceToDraftConverters.java @@ -39,7 +39,7 @@ public static AttributeDefinitionDraftBuilder toAttributeDefinitionDraftBuilder( .label(attributeDefinition.getLabel()) .isSearchable(attributeDefinition.getIsSearchable()) .inputTip(attributeDefinition.getInputTip()) - .isRequired(attributeDefinition.getIsSearchable()) + .isRequired(attributeDefinition.getIsRequired()) .attributeConstraint(attributeDefinition.getAttributeConstraint()) .inputHint(attributeDefinition.getInputHint()); } diff --git a/src/test/java/com/commercetools/sync/producttypes/utils/AttributeDefinitionUpdateActionUtilsTest.java b/src/test/java/com/commercetools/sync/producttypes/utils/AttributeDefinitionUpdateActionUtilsTest.java index 9088846741..50b2fe8034 100644 --- a/src/test/java/com/commercetools/sync/producttypes/utils/AttributeDefinitionUpdateActionUtilsTest.java +++ b/src/test/java/com/commercetools/sync/producttypes/utils/AttributeDefinitionUpdateActionUtilsTest.java @@ -1,6 +1,7 @@ package com.commercetools.sync.producttypes.utils; import static com.commercetools.api.models.common.LocalizedString.ofEnglish; +import static com.commercetools.sync.producttypes.MockBuilderUtils.createMockAttributeDefinitionDraftBuilder; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static org.assertj.core.api.Assertions.assertThat; @@ -30,7 +31,6 @@ import com.commercetools.api.models.product_type.ProductTypeUpdateAction; import com.commercetools.api.models.product_type.TextInputHint; import com.commercetools.sync.producttypes.MockBuilderUtils; -import com.commercetools.sync.producttypes.helpers.ResourceToDraftConverters; import java.util.List; import java.util.Optional; import org.junit.jupiter.api.Test; @@ -51,7 +51,7 @@ class AttributeDefinitionUpdateActionUtilsTest { void buildChangeLabelAction_WithDifferentValues_ShouldReturnAction() { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder().label(ofEnglish("x")).build(); + createMockAttributeDefinitionDraftBuilder().label(ofEnglish("x")).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder().label(ofEnglish("y")).build(); @@ -73,8 +73,7 @@ void buildChangeLabelAction_WithDifferentValues_ShouldReturnAction() { @Test void buildChangeLabelAction_WithSameValues_ShouldReturnEmptyOptional() { // Preparation - final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder().build(); + final AttributeDefinitionDraft draft = createMockAttributeDefinitionDraftBuilder().build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder().build(); @@ -92,9 +91,7 @@ void buildChangeLabelAction_WithSameValues_ShouldReturnEmptyOptional() { void buildSetInputTipAction_WithDifferentValues_ShouldReturnAction() { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() - .inputTip(ofEnglish("foo")) - .build(); + createMockAttributeDefinitionDraftBuilder().inputTip(ofEnglish("foo")).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder().inputTip(ofEnglish("bar")).build(); @@ -117,9 +114,7 @@ void buildSetInputTipAction_WithDifferentValues_ShouldReturnAction() { void buildSetInputTipAction_WithSameValues_ShouldReturnEmptyOptional() { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() - .inputTip(ofEnglish("foo")) - .build(); + createMockAttributeDefinitionDraftBuilder().inputTip(ofEnglish("foo")).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder().inputTip(ofEnglish("foo")).build(); @@ -137,9 +132,7 @@ void buildSetInputTipAction_WithSameValues_ShouldReturnEmptyOptional() { void buildSetInputTipAction_WithSourceNullValues_ShouldReturnAction() { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() - .inputTip((LocalizedString) null) - .build(); + createMockAttributeDefinitionDraftBuilder().inputTip((LocalizedString) null).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder().inputTip(ofEnglish("foo")).build(); @@ -162,9 +155,7 @@ void buildSetInputTipAction_WithSourceNullValues_ShouldReturnAction() { void buildSetInputTipAction_WithTargetNullValues_ShouldReturnAction() { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() - .inputTip(ofEnglish("foo")) - .build(); + createMockAttributeDefinitionDraftBuilder().inputTip(ofEnglish("foo")).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder() @@ -189,7 +180,7 @@ void buildSetInputTipAction_WithTargetNullValues_ShouldReturnAction() { void buildChangeIsSearchableAction_WithDifferentValues_ShouldReturnAction() { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder().isSearchable(true).build(); + createMockAttributeDefinitionDraftBuilder().isSearchable(true).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder().isSearchable(false).build(); @@ -211,7 +202,7 @@ void buildChangeIsSearchableAction_WithDifferentValues_ShouldReturnAction() { void buildChangeIsSearchableAction_WithSameValues_ShouldReturnEmptyOptional() { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder().isSearchable(true).build(); + createMockAttributeDefinitionDraftBuilder().isSearchable(true).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder().isSearchable(true).build(); @@ -228,7 +219,7 @@ void buildChangeIsSearchableAction_WithSameValues_ShouldReturnEmptyOptional() { void buildChangeIsSearchableAction_WithNullSourceAndNonDefaultTarget_ShouldBuildAction() { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder().isSearchable(null).build(); + createMockAttributeDefinitionDraftBuilder().isSearchable(null).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder().isSearchable(false).build(); @@ -250,7 +241,7 @@ void buildChangeIsSearchableAction_WithNullSourceAndNonDefaultTarget_ShouldBuild void buildChangeIsSearchableAction_WithNullSourceAndDefaultTarget_ShouldNotBuildAction() { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder().isSearchable(null).build(); + createMockAttributeDefinitionDraftBuilder().isSearchable(null).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder().isSearchable(true).build(); @@ -267,9 +258,7 @@ void buildChangeIsSearchableAction_WithNullSourceAndDefaultTarget_ShouldNotBuild void buildChangeInputHintAction_WithDifferentValues_ShouldReturnAction() { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() - .inputHint(TextInputHint.MULTI_LINE) - .build(); + createMockAttributeDefinitionDraftBuilder().inputHint(TextInputHint.MULTI_LINE).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder() @@ -293,9 +282,7 @@ void buildChangeInputHintAction_WithDifferentValues_ShouldReturnAction() { void buildChangeInputHintAction_WithSameValues_ShouldReturnEmptyOptional() { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() - .inputHint(TextInputHint.MULTI_LINE) - .build(); + createMockAttributeDefinitionDraftBuilder().inputHint(TextInputHint.MULTI_LINE).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder() @@ -314,7 +301,7 @@ void buildChangeInputHintAction_WithSameValues_ShouldReturnEmptyOptional() { void buildChangeInputHintAction_WithSourceNullValuesAndNonDefaultTargetValue_ShouldBuildAction() { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder().inputHint(null).build(); + createMockAttributeDefinitionDraftBuilder().inputHint(null).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder() @@ -338,7 +325,7 @@ void buildChangeInputHintAction_WithSourceNullValuesAndNonDefaultTargetValue_Sho void buildChangeInputHintAction_WithSourceNullValuesAndDefaultTargetValue_ShouldNotBuildAction() { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder().inputHint(null).build(); + createMockAttributeDefinitionDraftBuilder().inputHint(null).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder() @@ -358,7 +345,7 @@ void buildChangeAttributeConstraintAction_WithDifferentValues_ShouldBuildAction( throws UnsupportedOperationException { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + createMockAttributeDefinitionDraftBuilder() .attributeConstraint(AttributeConstraintEnum.NONE) .build(); @@ -385,7 +372,7 @@ void buildChangeAttributeConstraintAction_WithSameValues_ShouldReturnEmptyOption throws UnsupportedOperationException { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + createMockAttributeDefinitionDraftBuilder() .attributeConstraint(AttributeConstraintEnum.COMBINATION_UNIQUE) .build(); @@ -408,9 +395,7 @@ void buildChangeAttributeConstraintAction_WithSameValues_ShouldReturnEmptyOption throws UnsupportedOperationException { // Preparation final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() - .attributeConstraint(null) - .build(); + createMockAttributeDefinitionDraftBuilder().attributeConstraint(null).build(); final AttributeDefinition attributeDefinition = MockBuilderUtils.createMockAttributeDefinitionBuilder() @@ -440,7 +425,7 @@ void buildChangeAttributeConstraintAction_WithSameValues_ShouldReturnEmptyOption .build(); final AttributeDefinitionDraft draft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + createMockAttributeDefinitionDraftBuilder() .attributeConstraint(AttributeConstraintEnum.COMBINATION_UNIQUE) .build(); // test @@ -469,7 +454,7 @@ void buildActions_WithNewDifferentValues_ShouldReturnActions() .build(); final AttributeDefinitionDraft newDifferent = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + createMockAttributeDefinitionDraftBuilder() .label(ofEnglish("label2")) .isRequired(true) .attributeConstraint(AttributeConstraintEnum.NONE) @@ -509,8 +494,7 @@ void buildActions_WithNewDifferentValues_ShouldReturnActions() void buildActions_WithSameValues_ShouldReturnEmpty() throws UnsupportedOperationException { final AttributeDefinition old = MockBuilderUtils.createMockAttributeDefinitionBuilder().build(); - final AttributeDefinitionDraft newSame = - ResourceToDraftConverters.toAttributeDefinitionDraftBuilder(old).build(); + final AttributeDefinitionDraft newSame = createMockAttributeDefinitionDraftBuilder().build(); final List result = AttributeDefinitionUpdateActionUtils.buildActions(old, newSame); @@ -525,9 +509,7 @@ void buildActions_WithStringAttributeTypesWithLabelChanges_ShouldBuildChangeLabe MockBuilderUtils.createMockAttributeDefinitionBuilder().label(ofEnglish("label1")).build(); final AttributeDefinitionDraft attributeDefinitionDraft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() - .label(ofEnglish("label2")) - .build(); + createMockAttributeDefinitionDraftBuilder().label(ofEnglish("label2")).build(); final List result = AttributeDefinitionUpdateActionUtils.buildActions( @@ -568,7 +550,7 @@ void buildActions_WithChangedSetOfEnumAttributeTypes_ShouldBuildEnumActions() .build(); final AttributeDefinitionDraft newDraft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + createMockAttributeDefinitionDraftBuilder() .type( attributeTypeBuilder -> attributeTypeBuilder @@ -653,7 +635,7 @@ void buildActions_WithChangedSetOfLocalizedEnumAttributeTypes_ShouldBuildEnumAct .build(); final AttributeDefinitionDraft newDefinition = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + createMockAttributeDefinitionDraftBuilder() .type( attributeTypeBuilder -> attributeTypeBuilder @@ -722,7 +704,7 @@ void buildActions_WithNewPlainEnum_ShouldReturnAddEnumValueAction() { .build(); final AttributeDefinitionDraft attributeDefinitionDraft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + createMockAttributeDefinitionDraftBuilder() .type( attributeTypeBuilder -> attributeTypeBuilder.enumBuilder().values(ENUM_VALUE_A, ENUM_VALUE_B)) @@ -748,7 +730,7 @@ void buildActions_WithoutOldPlainEnum_ShouldReturnRemoveEnumValueAction() { .build(); final AttributeDefinitionDraft attributeDefinitionDraft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + createMockAttributeDefinitionDraftBuilder() .type(attributeTypeBuilder -> attributeTypeBuilder.enumBuilder().values(emptyList())) .build(); @@ -775,7 +757,7 @@ void buildActions_WitDifferentPlainEnumValueLabel_ShouldReturnChangeEnumValueLab AttributePlainEnumValueBuilder.of().key("a").label("label_a_different").build(); final AttributeDefinitionDraft attributeDefinitionDraft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + createMockAttributeDefinitionDraftBuilder() .type( attributeTypeBuilder -> attributeTypeBuilder.enumBuilder().values(enumValueDiffLabel)) @@ -803,7 +785,7 @@ void buildActions_WithNewLocalizedEnum_ShouldReturnAddLocalizedEnumValueAction() .build(); final AttributeDefinitionDraft attributeDefinitionDraft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + createMockAttributeDefinitionDraftBuilder() .type( attributeTypeBuilder -> attributeTypeBuilder @@ -833,7 +815,7 @@ void buildActions_WithoutOldLocalizedEnum_ShouldReturnRemoveLocalizedEnumValueAc .build(); final AttributeDefinitionDraft attributeDefinitionDraft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + createMockAttributeDefinitionDraftBuilder() .type(attributeTypeBuilder -> attributeTypeBuilder.lenumBuilder().values(emptyList())) .build(); @@ -863,7 +845,7 @@ void buildActions_WithoutOldLocalizedEnum_ShouldReturnRemoveLocalizedEnumValueAc AttributeLocalizedEnumValueBuilder.of().key("a").label(ofEnglish("label_a_diff")).build(); final AttributeDefinitionDraft attributeDefinitionDraft = - MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + createMockAttributeDefinitionDraftBuilder() .type( attributeTypeBuilder -> attributeTypeBuilder.lenumBuilder().values(localizedEnumValueDiffLabel)) diff --git a/src/test/java/com/commercetools/sync/producttypes/utils/ProductTypeReferenceResolutionUtilsTest.java b/src/test/java/com/commercetools/sync/producttypes/utils/ProductTypeReferenceResolutionUtilsTest.java index 39457b9d96..a026c1c60d 100644 --- a/src/test/java/com/commercetools/sync/producttypes/utils/ProductTypeReferenceResolutionUtilsTest.java +++ b/src/test/java/com/commercetools/sync/producttypes/utils/ProductTypeReferenceResolutionUtilsTest.java @@ -1,6 +1,8 @@ package com.commercetools.sync.producttypes.utils; import static com.commercetools.api.models.common.LocalizedString.ofEnglish; +import static com.commercetools.sync.producttypes.MockBuilderUtils.createMockAttributeDefinitionDraftBuilder; +import static com.commercetools.sync.producttypes.MockBuilderUtils.createMockProductTypeDraftBuilder; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; @@ -18,7 +20,6 @@ import com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl; import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache; import com.commercetools.sync.producttypes.MockBuilderUtils; -import com.commercetools.sync.producttypes.helpers.ResourceToDraftConverters; import java.util.List; import java.util.UUID; import org.junit.jupiter.api.AfterEach; @@ -113,9 +114,20 @@ void mapToProductDrafts_WithNoReferences_ShouldReturnCorrectProductTypeDrafts() // assertion assertThat(productTypeDrafts) - .containsExactly( - ResourceToDraftConverters.toProductTypeDraft(productTypeFoo), - ResourceToDraftConverters.toProductTypeDraft(productTypeBar)); + .containsExactlyInAnyOrder( + createMockProductTypeDraftBuilder() + .key(productTypeFoo.getKey()) + .attributes(createMockAttributeDefinitionDraftBuilder().build()) + .build(), + createMockProductTypeDraftBuilder() + .key(productTypeBar.getKey()) + .attributes( + createMockAttributeDefinitionDraftBuilder() + .name(numberAttr.getName()) + .label(numberAttr.getLabel()) + .type(numberAttr.getType()) + .build()) + .build()); } @Test diff --git a/src/test/java/com/commercetools/sync/producttypes/utils/ProductTypeTransformUtilsTest.java b/src/test/java/com/commercetools/sync/producttypes/utils/ProductTypeTransformUtilsTest.java index 54b2aed6d4..c35b1d18e1 100644 --- a/src/test/java/com/commercetools/sync/producttypes/utils/ProductTypeTransformUtilsTest.java +++ b/src/test/java/com/commercetools/sync/producttypes/utils/ProductTypeTransformUtilsTest.java @@ -1,6 +1,8 @@ package com.commercetools.sync.producttypes.utils; import static com.commercetools.api.models.common.LocalizedString.ofEnglish; +import static com.commercetools.sync.producttypes.MockBuilderUtils.createMockAttributeDefinitionDraftBuilder; +import static com.commercetools.sync.producttypes.MockBuilderUtils.createMockProductTypeDraftBuilder; import static io.vrap.rmf.base.client.utils.json.JsonUtils.fromJsonString; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; @@ -16,7 +18,9 @@ import com.commercetools.api.models.graph_ql.GraphQLRequest; import com.commercetools.api.models.graph_ql.GraphQLResponse; import com.commercetools.api.models.product_type.AttributeDefinition; +import com.commercetools.api.models.product_type.AttributeDefinitionDraft; import com.commercetools.api.models.product_type.AttributeNestedType; +import com.commercetools.api.models.product_type.AttributeReferenceTypeId; import com.commercetools.api.models.product_type.AttributeSetType; import com.commercetools.api.models.product_type.AttributeTypeBuilder; import com.commercetools.api.models.product_type.ProductType; @@ -26,7 +30,6 @@ import com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl; import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache; import com.commercetools.sync.producttypes.MockBuilderUtils; -import com.commercetools.sync.producttypes.helpers.ResourceToDraftConverters; import io.vrap.rmf.base.client.ApiHttpResponse; import java.util.List; import java.util.UUID; @@ -36,6 +39,7 @@ class ProductTypeTransformUtilsTest { + private static final String MAIN_PRODUCT_TYPE_KEY = "main-product-type"; final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl(); @AfterEach @@ -61,7 +65,8 @@ void mapToProductDrafts_WithProductTypeWithNoAttributeDefs_ShouldReturnProductTy // assertion assertThat(productTypeDrafts) - .containsExactly(ResourceToDraftConverters.toProductTypeDraft(productTypeFoo)); + .containsExactly( + createMockProductTypeDraftBuilder().key("foo").attributes(emptyList()).build()); } @Test @@ -101,10 +106,96 @@ void mapToProductDrafts_WithNoReferences_ShouldReturnCorrectProductTypeDrafts() .join(); // assertion + assertThat(productTypeDrafts) + .containsExactlyInAnyOrder( + createMockProductTypeDraftBuilder() + .key(productTypeFoo.getKey()) + .attributes(createMockAttributeDefinitionDraftBuilder().build()) + .build(), + createMockProductTypeDraftBuilder() + .key(productTypeBar.getKey()) + .attributes( + createMockAttributeDefinitionDraftBuilder() + .name(numberAttr.getName()) + .label(numberAttr.getLabel()) + .type(numberAttr.getType()) + .build()) + .build()); + } + + @Test + void mapToProductDrafts_WithReferencesAndSetOfReferences_ShouldResolveAttributesCorrectly() { + // preparation + final AttributeDefinition attributeProductReference = + MockBuilderUtils.createMockAttributeDefinitionBuilder() + .name("product-reference") + .label(ofEnglish("product-reference")) + .isRequired(false) + .isSearchable(true) + .type( + attributeTypeBuilder -> + attributeTypeBuilder + .referenceBuilder() + .referenceTypeId(AttributeReferenceTypeId.PRODUCT)) + .build(); + final AttributeDefinition colorProductReferenceSetAttribute = + MockBuilderUtils.createMockAttributeDefinitionBuilder() + .name("product-reference-set") + .label(ofEnglish("product-reference-set")) + .isRequired(false) + .isSearchable(true) + .type( + attributeTypeBuilder -> + attributeTypeBuilder + .setBuilder() + .elementType( + typeBuilder -> + typeBuilder + .referenceBuilder() + .referenceTypeId(AttributeReferenceTypeId.PRODUCT))) + .build(); + + final ProductType mainProductType = + MockBuilderUtils.createMockProductTypeBuilder() + .key(MAIN_PRODUCT_TYPE_KEY) + .name(MAIN_PRODUCT_TYPE_KEY) + .description("a main product type") + .attributes(attributeProductReference, colorProductReferenceSetAttribute) + .build(); + + // test + final List productTypeDrafts = + ProductTypeTransformUtils.toProductTypeDrafts( + mock(ProjectApiRoot.class), referenceIdToKeyCache, singletonList(mainProductType)) + .join(); + + // assertion + final AttributeDefinitionDraft productReferenceSetAttrDraft = + createMockAttributeDefinitionDraftBuilder() + .name(colorProductReferenceSetAttribute.getName()) + .label(colorProductReferenceSetAttribute.getLabel()) + .isRequired(false) + .isSearchable(true) + .type(colorProductReferenceSetAttribute.getType()) + .build(); + + final AttributeDefinitionDraft productReferenceAttrDraft = + createMockAttributeDefinitionDraftBuilder() + .name(attributeProductReference.getName()) + .label(attributeProductReference.getLabel()) + .isRequired(false) + .isSearchable(true) + .type(attributeProductReference.getType()) + .build(); + assertThat(productTypeDrafts) .containsExactly( - ResourceToDraftConverters.toProductTypeDraft(productTypeFoo), - ResourceToDraftConverters.toProductTypeDraft(productTypeBar)); + createMockProductTypeDraftBuilder() + .name(MAIN_PRODUCT_TYPE_KEY) + .key(MAIN_PRODUCT_TYPE_KEY) + .description("a main product type") + .attributes(productReferenceAttrDraft, productReferenceSetAttrDraft) + .build()); } @Test diff --git a/src/test/java/com/commercetools/sync/producttypes/utils/ProductTypeUpdateActionUtilsTest.java b/src/test/java/com/commercetools/sync/producttypes/utils/ProductTypeUpdateActionUtilsTest.java index 08744824a1..fa91509f2c 100644 --- a/src/test/java/com/commercetools/sync/producttypes/utils/ProductTypeUpdateActionUtilsTest.java +++ b/src/test/java/com/commercetools/sync/producttypes/utils/ProductTypeUpdateActionUtilsTest.java @@ -18,7 +18,6 @@ import com.commercetools.api.models.product_type.ProductTypeDraftBuilder; import com.commercetools.api.models.product_type.ProductTypeUpdateAction; import com.commercetools.sync.producttypes.MockBuilderUtils; -import com.commercetools.sync.producttypes.helpers.ResourceToDraftConverters; import java.util.List; import java.util.Locale; import java.util.Optional; @@ -45,7 +44,10 @@ static void setup() { final List sameAttributeDefinitionDrafts = singletonList( - ResourceToDraftConverters.toAttributeDefinitionDraftBuilder(attributeDefinition) + MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + .name("attributeName1") + .label(label) + .type(AttributeTypeBuilder::textBuilder) .build()); old = mock(ProductType.class); diff --git a/src/test/java/com/commercetools/sync/producttypes/utils/producttypeactionutils/BuildAttributeDefinitionUpdateActionsTest.java b/src/test/java/com/commercetools/sync/producttypes/utils/producttypeactionutils/BuildAttributeDefinitionUpdateActionsTest.java index eb3558f955..2bc7eb84cf 100644 --- a/src/test/java/com/commercetools/sync/producttypes/utils/producttypeactionutils/BuildAttributeDefinitionUpdateActionsTest.java +++ b/src/test/java/com/commercetools/sync/producttypes/utils/producttypeactionutils/BuildAttributeDefinitionUpdateActionsTest.java @@ -43,7 +43,6 @@ import com.commercetools.sync.producttypes.MockBuilderUtils; import com.commercetools.sync.producttypes.ProductTypeSyncOptions; import com.commercetools.sync.producttypes.ProductTypeSyncOptionsBuilder; -import com.commercetools.sync.producttypes.helpers.ResourceToDraftConverters; import com.commercetools.sync.producttypes.utils.ProductTypeUpdateActionUtils; import java.util.ArrayList; import java.util.List; @@ -702,7 +701,8 @@ void buildAttributesUpdateActions_WithDifferentOrder_ShouldBuildChangeAttributeO .build(); final AttributeDefinitionDraft ofNestedTypeDraft = - ResourceToDraftConverters.toAttributeDefinitionDraftBuilder(ofNestedType) + MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + .name(ofNestedType.getName()) .type( attributeTypeBuilder -> attributeTypeBuilder @@ -711,7 +711,8 @@ void buildAttributesUpdateActions_WithDifferentOrder_ShouldBuildChangeAttributeO .build(); final AttributeDefinitionDraft ofSetOfNestedTypeDraft = - ResourceToDraftConverters.toAttributeDefinitionDraftBuilder(ofSetOfNestedType) + MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + .name(ofSetOfNestedType.getName()) .type( attributeTypeBuilder -> attributeTypeBuilder @@ -725,7 +726,8 @@ void buildAttributesUpdateActions_WithDifferentOrder_ShouldBuildChangeAttributeO .build(); final AttributeDefinitionDraft ofSetOfSetOfNestedTypeDraft = - ResourceToDraftConverters.toAttributeDefinitionDraftBuilder(ofSetOfSetOfNestedType) + MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + .name(ofSetOfSetOfNestedType.getName()) .type( attributeTypeBuilder -> attributeTypeBuilder @@ -824,13 +826,19 @@ void buildAttributesUpdateActions_WithIdenticalNestedAttributeRefs_ShouldNotBuil .build(); final AttributeDefinitionDraft ofNestedTypeDraft = - ResourceToDraftConverters.toAttributeDefinitionDraftBuilder(ofNestedType).build(); + MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + .name(ofNestedType.getName()) + .build(); final AttributeDefinitionDraft ofSetOfNestedTypeDraft = - ResourceToDraftConverters.toAttributeDefinitionDraftBuilder(ofSetOfNestedType).build(); + MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + .name(ofSetOfNestedType.getName()) + .build(); final AttributeDefinitionDraft ofSetOfSetOfNestedTypeDraft = - ResourceToDraftConverters.toAttributeDefinitionDraftBuilder(ofSetOfSetOfNestedType).build(); + MockBuilderUtils.createMockAttributeDefinitionDraftBuilder() + .name(ofSetOfSetOfNestedType.getName()) + .build(); final ProductType oldProductType = MockBuilderUtils.createMockProductTypeBuilder()