Skip to content

Commit

Permalink
#447 Master variant reassignment (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
lojzatran committed Mar 22, 2023
1 parent 30157c1 commit be5bcf3
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 18 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ Example:
##### Download

```bash
docker pull commercetools/commercetools-project-sync:5.3.0
docker pull commercetools/commercetools-project-sync:5.3.1
```
##### Run

Expand All @@ -219,14 +219,14 @@ docker run \
-e TARGET_PROJECT_KEY=xxxx \
-e TARGET_CLIENT_ID=xxxx \
-e TARGET_CLIENT_SECRET=xxxx \
commercetools/commercetools-project-sync:5.3.0 -s all
commercetools/commercetools-project-sync:5.3.1 -s all
```


### Examples
- To run the all sync modules from a source project to a target project
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s all
docker run commercetools/commercetools-project-sync:5.3.1 -s all
```
This will run the following sync modules in the given order:
1. `Type` Sync and `ProductType` Sync and `States` Sync and `TaxCategory` Sync and `CustomObject` Sync in parallel.
Expand All @@ -236,68 +236,68 @@ commercetools/commercetools-project-sync:5.3.0 -s all

- To run the type sync
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s types
docker run commercetools/commercetools-project-sync:5.3.1 -s types
```

- To run the productType sync
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s productTypes
docker run commercetools/commercetools-project-sync:5.3.1 -s productTypes
```

- To run the states sync
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s states
docker run commercetools/commercetools-project-sync:5.3.1 -s states
```
- To run the taxCategory sync
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s taxCategories
docker run commercetools/commercetools-project-sync:5.3.1 -s taxCategories
```

- To run the category sync
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s categories
docker run commercetools/commercetools-project-sync:5.3.1 -s categories
```

- To run the product sync
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s products
docker run commercetools/commercetools-project-sync:5.3.1 -s products
```

- To run the cartDiscount sync
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s cartDiscounts
docker run commercetools/commercetools-project-sync:5.3.1 -s cartDiscounts
```

- To run the inventoryEntry sync
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s inventoryEntries
docker run commercetools/commercetools-project-sync:5.3.1 -s inventoryEntries
```

- To run the customObject sync
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s customObjects
docker run commercetools/commercetools-project-sync:5.3.1 -s customObjects
```

- To run the customer sync
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s customers
docker run commercetools/commercetools-project-sync:5.3.1 -s customers
```

- To run the shoppingList sync
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s shoppingLists
docker run commercetools/commercetools-project-sync:5.3.1 -s shoppingLists
```
- To run both products and shoppingList sync
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s products shoppingLists
docker run commercetools/commercetools-project-sync:5.3.1 -s products shoppingLists
```

- To run type, productType and shoppingList sync
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s types productTypes shoppingLists
docker run commercetools/commercetools-project-sync:5.3.1 -s types productTypes shoppingLists
```

- To run all sync modules using a runner name
```bash
docker run commercetools/commercetools-project-sync:5.3.0 -s all -r myRunnerName
docker run commercetools/commercetools-project-sync:5.3.1 -s all -r myRunnerName
```
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ext {
assertjVersion = '3.24.2'
pmdVersion = '6.55.0'
jacocoVersion = '0.8.8'
commercetoolsSyncJava = '9.2.1'
commercetoolsSyncJava = '9.2.3'
apacheCliVersion = '1.5.0'
jupiterApiVersion = '5.9.2'
asyncHttpClientVersion = '2.12.3'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package com.commercetools.project.sync;

import static com.commercetools.project.sync.util.IntegrationTestUtils.cleanUpProjects;
import static com.commercetools.project.sync.util.IntegrationTestUtils.createITSyncerFactory;
import static com.commercetools.project.sync.util.SphereClientUtils.CTP_SOURCE_CLIENT;
import static com.commercetools.project.sync.util.SphereClientUtils.CTP_TARGET_CLIENT;
import static io.sphere.sdk.models.LocalizedString.ofEnglish;
import static org.assertj.core.api.Assertions.assertThat;

import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.models.LocalizedString;
import io.sphere.sdk.products.Product;
import io.sphere.sdk.products.ProductDraft;
import io.sphere.sdk.products.ProductDraftBuilder;
import io.sphere.sdk.products.ProductVariant;
import io.sphere.sdk.products.ProductVariantDraft;
import io.sphere.sdk.products.ProductVariantDraftBuilder;
import io.sphere.sdk.products.attributes.AttributeConstraint;
import io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder;
import io.sphere.sdk.products.attributes.AttributeDraft;
import io.sphere.sdk.products.attributes.StringAttributeType;
import io.sphere.sdk.products.commands.ProductCreateCommand;
import io.sphere.sdk.products.queries.ProductQuery;
import io.sphere.sdk.producttypes.ProductType;
import io.sphere.sdk.producttypes.ProductTypeDraft;
import io.sphere.sdk.producttypes.ProductTypeDraftBuilder;
import io.sphere.sdk.producttypes.commands.ProductTypeCreateCommand;
import io.sphere.sdk.queries.PagedQueryResult;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import uk.org.lidalia.slf4jext.Level;
import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;

// This will suppress MoreThanOneLogger warnings in this class
@SuppressWarnings("PMD.MoreThanOneLogger")
class ProductSyncWithMasterVariantSwitchIT {
private static final TestLogger cliRunnerTestLogger =
TestLoggerFactory.getTestLogger(CliRunner.class);

private static final String MAIN_PRODUCT_TYPE_KEY = "main-product-type";
private static final String MAIN_PRODUCT_KEY = "product-with-references";

@BeforeEach
void setup() {
cliRunnerTestLogger.clearAll();

setupProjectData(CTP_SOURCE_CLIENT, "Source");
setupProjectData(CTP_TARGET_CLIENT, "Target");
}

static void setupProjectData(
@Nonnull final SphereClient sphereClient, @Nonnull final String variantSuffix) {
final ProductTypeDraft productTypeDraft =
ProductTypeDraftBuilder.of(
MAIN_PRODUCT_TYPE_KEY,
MAIN_PRODUCT_TYPE_KEY,
"a productType for t-shirts",
Collections.singletonList(
AttributeDefinitionDraftBuilder.of(
StringAttributeType.of(),
"test",
LocalizedString.ofEnglish("test"),
false)
.attributeConstraint(AttributeConstraint.SAME_FOR_ALL)
.build()))
.build();

final ProductType productType =
sphereClient
.execute(ProductTypeCreateCommand.of(productTypeDraft))
.toCompletableFuture()
.join();

final ProductVariantDraft masterVariant =
ProductVariantDraftBuilder.of()
.key("key" + variantSuffix)
.sku("sku" + variantSuffix)
.attributes(AttributeDraft.of("test", "test" + variantSuffix))
.build();

final ProductVariantDraft variant1 =
ProductVariantDraftBuilder.of()
.key("key" + variantSuffix + "Variant1")
.sku("sku" + variantSuffix + "Variant1")
.attributes(AttributeDraft.of("test", "test" + variantSuffix))
.build();

final ProductDraft draft =
ProductDraftBuilder.of(
productType,
ofEnglish(MAIN_PRODUCT_KEY),
ofEnglish(MAIN_PRODUCT_KEY),
List.of(masterVariant, variant1))
.key(MAIN_PRODUCT_KEY)
.build();

sphereClient.execute(ProductCreateCommand.of(draft)).toCompletableFuture().join();
}

@AfterAll
static void tearDownSuite() {
cleanUpProjects(CTP_SOURCE_CLIENT, CTP_TARGET_CLIENT);
}

@Test
void run_WhenTargetProductDifferentSkusAndKeys_ShouldSyncTargetCorrectly() {
// test
CliRunner.of()
.run(new String[] {"-s", "products", "-r", "runnerName", "-f"}, createITSyncerFactory());

// assertions
assertThat(cliRunnerTestLogger.getAllLoggingEvents())
.allMatch(loggingEvent -> !Level.ERROR.equals(loggingEvent.getLevel()));

final PagedQueryResult<Product> productQueryResult =
CTP_TARGET_CLIENT.execute(ProductQuery.of()).toCompletableFuture().join();

assertThat(productQueryResult.getResults())
.hasSize(1)
.singleElement()
.satisfies(
product -> {
final ProductVariant targetMasterVariant =
product.getMasterData().getStaged().getMasterVariant();
final ProductVariant targetVariant1 =
product.getMasterData().getStaged().getVariants().get(0);
assertThat(targetMasterVariant.getSku()).isEqualTo("skuSource");
assertThat(targetMasterVariant.getKey()).isEqualTo("keySource");
assertThat(targetMasterVariant.getAttributes().get(0).getValueAsString())
.isEqualTo("testSource");

assertThat(targetVariant1.getSku()).isEqualTo("skuSourceVariant1");
assertThat(targetVariant1.getKey()).isEqualTo("keySourceVariant1");
assertThat(targetVariant1.getAttributes().get(0).getValueAsString())
.isEqualTo("testSource");
});
}
}

0 comments on commit be5bcf3

Please sign in to comment.