|
| 1 | +package com.commercetools.project.sync; |
| 2 | + |
| 3 | +import static com.commercetools.api.models.common.LocalizedString.ofEnglish; |
| 4 | +import static com.commercetools.project.sync.util.CtpClientUtils.CTP_SOURCE_CLIENT; |
| 5 | +import static com.commercetools.project.sync.util.CtpClientUtils.CTP_TARGET_CLIENT; |
| 6 | +import static com.commercetools.project.sync.util.IntegrationTestUtils.cleanUpProjects; |
| 7 | +import static com.commercetools.project.sync.util.IntegrationTestUtils.createITSyncerFactory; |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import com.commercetools.api.client.ProjectApiRoot; |
| 11 | +import com.commercetools.api.models.common.CentPrecisionMoneyBuilder; |
| 12 | +import com.commercetools.api.models.common.TypedMoney; |
| 13 | +import com.commercetools.api.models.product.Attribute; |
| 14 | +import com.commercetools.api.models.product.Product; |
| 15 | +import com.commercetools.api.models.product.ProductDraft; |
| 16 | +import com.commercetools.api.models.product.ProductDraftBuilder; |
| 17 | +import com.commercetools.api.models.product.ProductPagedQueryResponse; |
| 18 | +import com.commercetools.api.models.product.ProductVariant; |
| 19 | +import com.commercetools.api.models.product.ProductVariantDraft; |
| 20 | +import com.commercetools.api.models.product.ProductVariantDraftBuilder; |
| 21 | +import com.commercetools.api.models.product_type.AttributeConstraintEnum; |
| 22 | +import com.commercetools.api.models.product_type.AttributeTypeBuilder; |
| 23 | +import com.commercetools.api.models.product_type.ProductType; |
| 24 | +import com.commercetools.api.models.product_type.ProductTypeDraft; |
| 25 | +import com.commercetools.api.models.product_type.ProductTypeDraftBuilder; |
| 26 | +import com.github.valfirst.slf4jtest.TestLogger; |
| 27 | +import com.github.valfirst.slf4jtest.TestLoggerFactory; |
| 28 | +import io.vrap.rmf.base.client.ApiHttpResponse; |
| 29 | +import javax.annotation.Nonnull; |
| 30 | +import org.junit.jupiter.api.AfterAll; |
| 31 | +import org.junit.jupiter.api.BeforeEach; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import uk.org.lidalia.slf4jext.Level; |
| 34 | + |
| 35 | +class ProductSyncWithStringAttributeIT { |
| 36 | + |
| 37 | + private static final TestLogger cliRunnerTestLogger = |
| 38 | + TestLoggerFactory.getTestLogger(CliRunner.class); |
| 39 | + |
| 40 | + private static final String MAIN_PRODUCT_TYPE_KEY = "main-product-type"; |
| 41 | + private static final String MAIN_PRODUCT_MASTER_VARIANT_KEY = "main-product-master-variant-key"; |
| 42 | + private static final String MAIN_PRODUCT_KEY = "product-with-attribute-as-string"; |
| 43 | + private static final TypedMoney TEN_EUR = |
| 44 | + CentPrecisionMoneyBuilder.of() |
| 45 | + .centAmount(1000L) |
| 46 | + .currencyCode("EUR") |
| 47 | + .fractionDigits(2) |
| 48 | + .build(); |
| 49 | + private Product sourceProduct; |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + void setup() { |
| 53 | + cliRunnerTestLogger.clearAll(); |
| 54 | + sourceProduct = setupProjectData(CTP_SOURCE_CLIENT); |
| 55 | + setupProjectData(CTP_TARGET_CLIENT); |
| 56 | + } |
| 57 | + |
| 58 | + static Product setupProjectData(@Nonnull final ProjectApiRoot ctpClient) { |
| 59 | + final ProductTypeDraft productTypeDraft = |
| 60 | + ProductTypeDraftBuilder.of() |
| 61 | + .key(MAIN_PRODUCT_TYPE_KEY) |
| 62 | + .name(MAIN_PRODUCT_TYPE_KEY) |
| 63 | + .description("a productType for t-shirts") |
| 64 | + .addAttributes( |
| 65 | + attributeDefinitionDraftBuilder -> |
| 66 | + attributeDefinitionDraftBuilder |
| 67 | + .type(AttributeTypeBuilder::textBuilder) |
| 68 | + .isRequired(false) |
| 69 | + .attributeConstraint(AttributeConstraintEnum.SAME_FOR_ALL) |
| 70 | + .name("modelo") |
| 71 | + .label( |
| 72 | + localizedStringBuilder -> |
| 73 | + localizedStringBuilder.addValue("en", "modelo")) |
| 74 | + .build()) |
| 75 | + .build(); |
| 76 | + |
| 77 | + final ProductType productType = |
| 78 | + ctpClient.productTypes().post(productTypeDraft).executeBlocking().getBody(); |
| 79 | + |
| 80 | + final ProductVariantDraft masterVariant = |
| 81 | + ProductVariantDraftBuilder.of() |
| 82 | + .key(MAIN_PRODUCT_MASTER_VARIANT_KEY) |
| 83 | + .sku(MAIN_PRODUCT_MASTER_VARIANT_KEY) |
| 84 | + .build(); |
| 85 | + |
| 86 | + final ProductDraft draft = |
| 87 | + ProductDraftBuilder.of() |
| 88 | + .productType(productType.toResourceIdentifier()) |
| 89 | + .name(ofEnglish(MAIN_PRODUCT_KEY)) |
| 90 | + .slug(ofEnglish(MAIN_PRODUCT_KEY)) |
| 91 | + .masterVariant(masterVariant) |
| 92 | + .key(MAIN_PRODUCT_KEY) |
| 93 | + .build(); |
| 94 | + |
| 95 | + return ctpClient.products().post(draft).execute().thenApply(ApiHttpResponse::getBody).join(); |
| 96 | + } |
| 97 | + |
| 98 | + @AfterAll |
| 99 | + static void tearDownSuite() { |
| 100 | + cleanUpProjects(CTP_SOURCE_CLIENT, CTP_TARGET_CLIENT); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void run_WhenTargetProductHasStringAttributeThatLooksLikeDate_ShouldNotParseIt() { |
| 105 | + sourceProduct = |
| 106 | + CTP_SOURCE_CLIENT |
| 107 | + .products() |
| 108 | + .withId(sourceProduct.getId()) |
| 109 | + .post( |
| 110 | + productUpdateBuilder -> |
| 111 | + productUpdateBuilder |
| 112 | + .version(sourceProduct.getVersion()) |
| 113 | + .withActions( |
| 114 | + productUpdateActionBuilder -> |
| 115 | + productUpdateActionBuilder |
| 116 | + .setAttributeInAllVariantsBuilder() |
| 117 | + .name("modelo") |
| 118 | + .value("2281-22-90"))) |
| 119 | + .execute() |
| 120 | + .thenApply(ApiHttpResponse::getBody) |
| 121 | + .join(); |
| 122 | + |
| 123 | + // test |
| 124 | + CliRunner.of() |
| 125 | + .run(new String[] {"-s", "products", "-r", "runnerName", "-f"}, createITSyncerFactory()); |
| 126 | + |
| 127 | + // assertions |
| 128 | + assertThat(cliRunnerTestLogger.getAllLoggingEvents()) |
| 129 | + .allMatch(loggingEvent -> !Level.ERROR.equals(loggingEvent.getLevel())); |
| 130 | + |
| 131 | + final ProductPagedQueryResponse productPagedQueryResponse = |
| 132 | + CTP_TARGET_CLIENT.products().get().execute().toCompletableFuture().join().getBody(); |
| 133 | + |
| 134 | + assertThat(productPagedQueryResponse.getResults()) |
| 135 | + .hasSize(1) |
| 136 | + .singleElement() |
| 137 | + .satisfies( |
| 138 | + product -> { |
| 139 | + final ProductVariant stagedMasterVariant = |
| 140 | + product.getMasterData().getStaged().getMasterVariant(); |
| 141 | + Attribute modeloAttribute = |
| 142 | + stagedMasterVariant.getAttributes().stream() |
| 143 | + .filter(attribute -> attribute.getName().equals("modelo")) |
| 144 | + .findFirst() |
| 145 | + .orElse(null); |
| 146 | + assertThat(modeloAttribute.getValue()).isEqualTo("2281-22-90"); |
| 147 | + }); |
| 148 | + } |
| 149 | +} |
0 commit comments