-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
389 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
src/integration-test/java/com/commercetools/project/sync/ProductSyncWithDiscountedPrice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.