Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate ITs for product syncer #505

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e11f298
Migrate SyncerFactory
Oct 9, 2023
25fc48d
Migrate client utils and rename
Oct 9, 2023
ae84cbc
Migrate CliRunner
Oct 9, 2023
90b0816
Migrate unittests for SyncerFactory
Oct 17, 2023
2e303ee
Migrate unittests for CLIRunner
Oct 17, 2023
6dc8005
Cleanup test code
Oct 17, 2023
420ccc0
DEVX-247 migrate category syncr
lojzatran Oct 19, 2023
ba5fc2c
DEVX-247 fix spotbugs
lojzatran Oct 19, 2023
0a5b757
DEVX-247 rename variable
lojzatran Oct 19, 2023
8b07dfe
DEVX-247 rename variable
lojzatran Oct 19, 2023
c3c3f33
Merge branch 'sync-java-migration-v10' into DEVX-247_migrate_category…
Oct 23, 2023
7639767
Formatting
Oct 23, 2023
e01cb13
Migrate product syncer with unittests
Oct 23, 2023
3ffc9cf
DEVX-247 add todo
lojzatran Oct 23, 2023
db72242
DEVX-247 remove redundat variable
lojzatran Oct 23, 2023
204ba8e
Merge branch 'sync-java-migration-v10' into DEVX-247_migrate_category…
Oct 24, 2023
56936ce
Merge branch 'DEVX-247_migrate_category_syncer' into DEVX-243_migrate…
Oct 24, 2023
415a9a1
Merge branch 'sync-java-migration-v10' into DEVX-243_migrate-product-…
Oct 24, 2023
82b4515
Migrate IT for productSync with discounted price
Oct 24, 2023
8dc4546
Migrate IT for productsync with mastervariant switch
Oct 24, 2023
c325130
Merge branch 'sync-java-migration-v10' into DEVX-243_migrate-product-…
Oct 25, 2023
ce26a52
Use utility method to mock graphql request
Oct 25, 2023
387aed3
Merge branch 'DEVX-243_migrate-product-syncer' into DEVX-245_Migrate-…
Oct 25, 2023
d95c7c0
Migrate ITs for productsync with nested references and adjust testutils
Oct 25, 2023
8c5490b
Migrate ITs for productsync with reference resolution
Oct 25, 2023
e8e59be
Migrate ITs for productsync with references
Oct 26, 2023
12d8ec4
Remove unused return value
Oct 26, 2023
cec344b
Fix NPE in test
Oct 26, 2023
691a844
Merge branch 'DEVX-243_migrate-product-syncer' into DEVX-245_Migrate-…
Oct 26, 2023
a16365a
Rename class. Remove suppress warning
Oct 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package com.commercetools.project.sync;

import static com.commercetools.api.models.common.LocalizedString.ofEnglish;
import static com.commercetools.project.sync.util.CtpClientUtils.CTP_SOURCE_CLIENT;
import static com.commercetools.project.sync.util.CtpClientUtils.CTP_TARGET_CLIENT;
import static com.commercetools.project.sync.util.IntegrationTestUtils.cleanUpProjects;
import static com.commercetools.project.sync.util.IntegrationTestUtils.createITSyncerFactory;
import static org.assertj.core.api.Assertions.assertThat;

import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.models.common.CentPrecisionMoneyBuilder;
import com.commercetools.api.models.common.DiscountedPriceDraft;
import com.commercetools.api.models.common.DiscountedPriceDraftBuilder;
import com.commercetools.api.models.common.Price;
import com.commercetools.api.models.common.PriceDraft;
import com.commercetools.api.models.common.PriceDraftBuilder;
import com.commercetools.api.models.common.TypedMoney;
import com.commercetools.api.models.product.ProductDraft;
import com.commercetools.api.models.product.ProductDraftBuilder;
import com.commercetools.api.models.product.ProductPagedQueryResponse;
import com.commercetools.api.models.product.ProductVariant;
import com.commercetools.api.models.product.ProductVariantDraft;
import com.commercetools.api.models.product.ProductVariantDraftBuilder;
import com.commercetools.api.models.product_discount.ProductDiscount;
import com.commercetools.api.models.product_discount.ProductDiscountDraft;
import com.commercetools.api.models.product_discount.ProductDiscountDraftBuilder;
import com.commercetools.api.models.product_discount.ProductDiscountValueExternalDraftBuilder;
import com.commercetools.api.models.product_type.ProductType;
import com.commercetools.api.models.product_type.ProductTypeDraft;
import com.commercetools.api.models.product_type.ProductTypeDraftBuilder;
import java.time.ZonedDateTime;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
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;

class ProductSyncWithDiscountedPriceIT {
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 TypedMoney TEN_EUR =
CentPrecisionMoneyBuilder.of()
.centAmount(1000L)
.currencyCode("EUR")
.fractionDigits(2)
.build();

@BeforeEach
void setup() {
cliRunnerTestLogger.clearAll();
ProductDiscountDraft productDiscountDraft =
ProductDiscountDraftBuilder.of()
.value(ProductDiscountValueExternalDraftBuilder.of().build())
.name(ofEnglish("testProductDiscount"))
.predicate("1=1")
.sortOrder("0.9")
.isActive(true)
.build();
ProductDiscount productDiscount =
CTP_TARGET_CLIENT.productDiscounts().post(productDiscountDraft).executeBlocking().getBody();
setupProjectData(CTP_SOURCE_CLIENT, null);
setupProjectData(CTP_TARGET_CLIENT, productDiscount.getId());
}

static void setupProjectData(
@Nonnull final ProjectApiRoot ctpClient, final String productDiscountId) {
final ProductTypeDraft productTypeDraft =
ProductTypeDraftBuilder.of()
.key(MAIN_PRODUCT_TYPE_KEY)
.name(MAIN_PRODUCT_TYPE_KEY)
.description("a productType for t-shirts")
.build();

final ProductType productType =
ctpClient.productTypes().post(productTypeDraft).executeBlocking().getBody();

final PriceDraft priceDraft =
PriceDraftBuilder.of(getPriceDraft(22200L, "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(productType.toResourceIdentifier())
.name(ofEnglish(MAIN_PRODUCT_KEY))
.slug(ofEnglish(MAIN_PRODUCT_KEY))
.masterVariant(masterVariant)
.key(MAIN_PRODUCT_KEY)
.build();

ctpClient.products().post(draft).executeBlocking();
}

@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 ProductPagedQueryResponse productPagedQueryResponse =
CTP_TARGET_CLIENT.products().get().execute().toCompletableFuture().join().getBody();

assertThat(productPagedQueryResponse.getResults())
.hasSize(1)
.singleElement()
.satisfies(
product -> {
final ProductVariant stagedMasterVariant =
product.getMasterData().getStaged().getMasterVariant();
assertThat(stagedMasterVariant.getPrices())
.satisfies(
prices -> {
final Price price = prices.get(0);
assertThat(price.getDiscounted()).isNotNull();
assertThat(price.getDiscounted().getValue()).isEqualTo(TEN_EUR);
});
});
}

@Nonnull
public static PriceDraft getPriceDraft(
@Nonnull final Long value,
@Nonnull final String currencyCode,
@Nullable final String countryCode,
@Nullable final ZonedDateTime validFrom,
@Nullable final ZonedDateTime validUntil,
@Nullable final String productDiscountReferenceId) {
DiscountedPriceDraft discounted = null;
if (productDiscountReferenceId != null) {
discounted =
DiscountedPriceDraftBuilder.of()
.value(TEN_EUR)
.discount(
productDiscountReferenceBuilder ->
productDiscountReferenceBuilder.id(productDiscountReferenceId))
.build();
}
return PriceDraftBuilder.of()
.value(moneyBuilder -> moneyBuilder.centAmount(value).currencyCode(currencyCode))
.country(countryCode)
.validFrom(validFrom)
.validUntil(validUntil)
.discounted(discounted)
.build();
}
}
Loading