Skip to content

Commit

Permalink
#363 remove discounted prices from syncing (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
lojzatran authored Jun 9, 2022
1 parent c872bb3 commit 7884fe9
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 19 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Example:
##### Download

```bash
docker pull commercetools/commercetools-project-sync:5.1.2
docker pull commercetools/commercetools-project-sync:5.1.3
```
##### Run

Expand All @@ -215,14 +215,14 @@ docker run \
-e TARGET_PROJECT_KEY=xxxx \
-e TARGET_CLIENT_ID=xxxx \
-e TARGET_CLIENT_SECRET=xxxx \
commercetools/commercetools-project-sync:5.1.2 -s all
commercetools/commercetools-project-sync:5.1.3 -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.1.2 -s all
docker run commercetools/commercetools-project-sync:5.1.3 -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 @@ -232,68 +232,68 @@ commercetools/commercetools-project-sync:5.1.2 -s all

- To run the type sync
```bash
docker run commercetools/commercetools-project-sync:5.1.2 -s types
docker run commercetools/commercetools-project-sync:5.1.3 -s types
```

- To run the productType sync
```bash
docker run commercetools/commercetools-project-sync:5.1.2 -s productTypes
docker run commercetools/commercetools-project-sync:5.1.3 -s productTypes
```

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

- To run the category sync
```bash
docker run commercetools/commercetools-project-sync:5.1.2 -s categories
docker run commercetools/commercetools-project-sync:5.1.3 -s categories
```

- To run the product sync
```bash
docker run commercetools/commercetools-project-sync:5.1.2 -s products
docker run commercetools/commercetools-project-sync:5.1.3 -s products
```

- To run the cartDiscount sync
```bash
docker run commercetools/commercetools-project-sync:5.1.2 -s cartDiscounts
docker run commercetools/commercetools-project-sync:5.1.3 -s cartDiscounts
```

- To run the inventoryEntry sync
```bash
docker run commercetools/commercetools-project-sync:5.1.2 -s inventoryEntries
docker run commercetools/commercetools-project-sync:5.1.3 -s inventoryEntries
```

- To run the customObject sync
```bash
docker run commercetools/commercetools-project-sync:5.1.2 -s customObjects
docker run commercetools/commercetools-project-sync:5.1.3 -s customObjects
```

- To run the customer sync
```bash
docker run commercetools/commercetools-project-sync:5.1.2 -s customers
docker run commercetools/commercetools-project-sync:5.1.3 -s customers
```

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

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

- To run all sync modules using a runner name
```bash
docker run commercetools/commercetools-project-sync:5.1.2 -s all -r myRunnerName
docker run commercetools/commercetools-project-sync:5.1.3 -s all -r myRunnerName
```
5 changes: 5 additions & 0 deletions config/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@
<Bug pattern="EC_UNRELATED_TYPES"/>
</Match>

<!-- Ignoring because of the bug in spotbugs: https://github.com/spotbugs/spotbugs/issues/2040 -->
<Match>
<Bug pattern="THROWS_METHOD_THROWS_CLAUSE_THROWABLE"/>
</Match>

</FindBugsFilter>
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
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 com.neovisionaries.i18n.CountryCode.DE;
import static io.sphere.sdk.models.DefaultCurrencyUnits.EUR;
import static io.sphere.sdk.models.LocalizedString.ofEnglish;
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;

import com.neovisionaries.i18n.CountryCode;
import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.models.Reference;
import io.sphere.sdk.productdiscounts.DiscountedPrice;
import io.sphere.sdk.productdiscounts.ProductDiscount;
import io.sphere.sdk.productdiscounts.ProductDiscountDraft;
import io.sphere.sdk.productdiscounts.ProductDiscountDraftBuilder;
import io.sphere.sdk.productdiscounts.ProductDiscountValue;
import io.sphere.sdk.productdiscounts.commands.ProductDiscountCreateCommand;
import io.sphere.sdk.products.Price;
import io.sphere.sdk.products.PriceDraft;
import io.sphere.sdk.products.PriceDraftBuilder;
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.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.math.BigDecimal;
import java.time.ZonedDateTime;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.money.CurrencyUnit;
import org.javamoney.moneta.FastMoney;
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 ProductSyncWithDiscountedPrice {
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_MASTER_VARIANT_KEY = "main-product-master-variant-key";
private static final String MAIN_PRODUCT_KEY = "product-with-references";
private static final FastMoney TEN_EUR = FastMoney.of(10, EUR);

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

ProductDiscountDraft productDiscountDraft =
ProductDiscountDraftBuilder.of()
.value(ProductDiscountValue.ofExternal())
.name(ofEnglish("testProductDiscount"))
.predicate("1=1")
.sortOrder("0.9")
.isActive(true)
.build();
ProductDiscount productDiscount =
CTP_TARGET_CLIENT
.execute(ProductDiscountCreateCommand.of(productDiscountDraft))
.toCompletableFuture()
.join();
setupProjectData(CTP_SOURCE_CLIENT, null);
setupProjectData(CTP_TARGET_CLIENT, productDiscount.getId());
}

static void setupProjectData(@Nonnull final SphereClient sphereClient, String productDiscountId) {
final ProductTypeDraft productTypeDraft =
ProductTypeDraftBuilder.of(
MAIN_PRODUCT_TYPE_KEY,
MAIN_PRODUCT_TYPE_KEY,
"a productType for t-shirts",
emptyList())
.build();

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

final PriceDraft priceDraft =
PriceDraftBuilder.of(
getPriceDraft(BigDecimal.valueOf(222), EUR, DE, null, null, productDiscountId))
.build();

final ProductVariantDraft masterVariant =
ProductVariantDraftBuilder.of()
.key(MAIN_PRODUCT_MASTER_VARIANT_KEY)
.sku(MAIN_PRODUCT_MASTER_VARIANT_KEY)
.prices(priceDraft)
.build();

final ProductDraft draft =
ProductDraftBuilder.of(
productType,
ofEnglish(MAIN_PRODUCT_KEY),
ofEnglish(MAIN_PRODUCT_KEY),
masterVariant)
.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_WhenTargetProductHasDiscountedPrice_ShouldNotRemoveIt() {
// 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 stagedMasterVariant =
product.getMasterData().getStaged().getMasterVariant();
assertThat(stagedMasterVariant.getPrices())
.satisfies(
prices -> {
Price price = prices.get(0);
assertThat(price.getDiscounted()).isNotNull();
assertThat(price.getDiscounted().getValue()).isEqualTo(TEN_EUR);
});
});
}

@Nonnull
public static PriceDraft getPriceDraft(
@Nonnull final BigDecimal value,
@Nonnull final CurrencyUnit currencyUnits,
@Nullable final CountryCode countryCode,
@Nullable final ZonedDateTime validFrom,
@Nullable final ZonedDateTime validUntil,
@Nullable final String productDiscountReferenceId) {
DiscountedPrice discounted = null;
if (productDiscountReferenceId != null) {
discounted =
DiscountedPrice.of(TEN_EUR, Reference.of("product-discount", productDiscountReferenceId));
}
return PriceDraftBuilder.of(Price.of(value, currencyUnits))
.country(countryCode)
.validFrom(validFrom)
.validUntil(validUntil)
.discounted(discounted)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import io.sphere.sdk.inventory.commands.InventoryEntryDeleteCommand;
import io.sphere.sdk.inventory.queries.InventoryEntryQuery;
import io.sphere.sdk.models.Versioned;
import io.sphere.sdk.productdiscounts.commands.ProductDiscountDeleteCommand;
import io.sphere.sdk.productdiscounts.queries.ProductDiscountQuery;
import io.sphere.sdk.products.Product;
import io.sphere.sdk.products.ProductVariant;
import io.sphere.sdk.products.commands.ProductDeleteCommand;
Expand Down Expand Up @@ -172,8 +174,15 @@ private static void deleteProjectData(@Nonnull final SphereClient client) {
final CompletableFuture<Void> deleteCustomObject =
queryAndExecute(
client, CustomObjectQuery.ofJsonNode(), CustomObjectDeleteCommand::ofJsonNode);
final CompletableFuture<Void> deleteProductDiscount =
queryAndExecute(client, ProductDiscountQuery.of(), ProductDiscountDeleteCommand::of);

CompletableFuture.allOf(deleteProduct, deleteInventory, deleteCartDiscount, deleteCustomObject)
CompletableFuture.allOf(
deleteProduct,
deleteInventory,
deleteCartDiscount,
deleteCustomObject,
deleteProductDiscount)
.join();

queryAndExecute(
Expand Down
Loading

0 comments on commit 7884fe9

Please sign in to comment.