Skip to content

Commit

Permalink
Merge pull request #1123 from commercetools/DEVX-291_Sync-producttype…
Browse files Browse the repository at this point in the history
…s-with-reference-set

Sync producttypes with attributes correctly
  • Loading branch information
salander85 authored Dec 5, 2023
2 parents c340210 + dae798a commit 2100201
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> T readObjectFromResource(final String resourcePath, final Class<T> objectType) {
final InputStream resourceAsStream =
Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<ProductType> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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);
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Loading

0 comments on commit 2100201

Please sign in to comment.