Search in sources :

Example 1 with ProductSyncOptions

use of com.commercetools.sync.products.ProductSyncOptions in project commercetools-sync-java by commercetools.

the class ProductSyncIT method sync_withProductTypeReference_ShouldUpdateProducts.

@Test
void sync_withProductTypeReference_ShouldUpdateProducts() {
    // Preparation
    // Create custom options with whitelisting and action filter callback..
    final ProductSyncOptions customSyncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> errorCallback(exception.getMessage(), exception.getCause())).warningCallback((exception, oldResource, newResources) -> warningCallBackMessages.add(exception.getMessage())).beforeUpdateCallback(this::beforeUpdateCallback).syncFilter(SyncFilter.ofWhiteList(ATTRIBUTES)).build();
    final ProductSync customSync = new ProductSync(customSyncOptions);
    // Create 3 existing products in target project with keys (productKey1, productKey2 and
    // productKey3)
    final ProductDraft existingProductDraft = createProductDraft(PRODUCT_KEY_1_RESOURCE_PATH, targetProductType.toReference(), targetTaxCategory.toReference(), targetProductState.toReference(), targetCategoryReferencesWithIds, createRandomCategoryOrderHints(targetCategoryReferencesWithIds));
    CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(existingProductDraft)).toCompletableFuture().join();
    final ProductDraft existingProductDraft2 = createProductDraft(PRODUCT_KEY_2_RESOURCE_PATH, targetProductType.toReference(), targetTaxCategory.toReference(), targetProductState.toReference(), targetCategoryReferencesWithIds, createRandomCategoryOrderHints(targetCategoryReferencesWithIds));
    CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(existingProductDraft2)).toCompletableFuture().join();
    final ProductDraft existingProductDraft3 = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, targetProductType.toReference()).slug(ofEnglish("newSlug3")).key("productKey3").masterVariant(ProductVariantDraftBuilder.of().key("v3").sku("s3").build()).taxCategory(null).state(null).categories(Collections.emptySet()).categoryOrderHints(null).build();
    CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(existingProductDraft3)).toCompletableFuture().join();
    // Create 2 existing products in source project with keys (productKey2 and productKey3)
    final ProductDraft newProductDraft2 = createProductDraft(PRODUCT_KEY_2_RESOURCE_PATH, sourceProductType.toReference(), sourceTaxCategory.toReference(), sourceProductState.toReference(), sourceCategoryReferencesWithIds, createRandomCategoryOrderHints(sourceCategoryReferencesWithIds));
    final Product product2 = CTP_SOURCE_CLIENT.execute(ProductCreateCommand.of(newProductDraft2)).toCompletableFuture().join();
    final ProductDraft newProductDraft3 = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, sourceProductType.toReference()).slug(ofEnglish("newSlug3")).key("productKey3").masterVariant(ProductVariantDraftBuilder.of().key("v3").sku("s3").build()).taxCategory(null).state(null).categories(Collections.emptySet()).categoryOrderHints(null).build();
    final Product product3 = CTP_SOURCE_CLIENT.execute(ProductCreateCommand.of(newProductDraft3)).toCompletableFuture().join();
    // Create existing product with productKey1 in source project that has references to products
    // with keys
    // (productKey2 and productKey3).
    final ObjectNode productReferenceValue1 = getProductReferenceWithId(product2.getId());
    final ObjectNode productReferenceValue2 = getProductReferenceWithId(product3.getId());
    final AttributeDraft productRefAttr = AttributeDraft.of("product-reference", productReferenceValue1);
    final AttributeDraft productSetRefAttr = getReferenceSetAttributeDraft("product-reference-set", productReferenceValue1, productReferenceValue2);
    final List<AttributeDraft> attributeDrafts = existingProductDraft.getMasterVariant().getAttributes();
    attributeDrafts.addAll(Arrays.asList(productRefAttr, productSetRefAttr));
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().key("v1").sku("s1").attributes(attributeDrafts).build();
    final ProductDraft newProductDraftWithProductReference = createProductDraftBuilder(PRODUCT_KEY_1_CHANGED_RESOURCE_PATH, sourceProductType.toReference()).masterVariant(masterVariant).taxCategory(sourceTaxCategory.toReference()).state(sourceProductState.toReference()).categories(Collections.emptySet()).categoryOrderHints(null).build();
    CTP_SOURCE_CLIENT.execute(ProductCreateCommand.of(newProductDraftWithProductReference)).toCompletableFuture().join();
    // Test
    final List<ProductProjection> products = CTP_SOURCE_CLIENT.execute(ProductProjectionQuery.ofStaged()).toCompletableFuture().join().getResults();
    final List<ProductDraft> productDrafts = ProductTransformUtils.toProductDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, products).join();
    final ProductSyncStatistics syncStatistics = customSync.sync(productDrafts).toCompletableFuture().join();
    // Assertion
    assertThat(syncStatistics).hasValues(3, 0, 1, 0);
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    final Product targetProduct2 = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of("productKey2")).toCompletableFuture().join();
    final Product targetProduct3 = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of("productKey3")).toCompletableFuture().join();
    final ObjectNode targetProductReferenceValue2 = getProductReferenceWithId(targetProduct2.getId());
    final ObjectNode targetProductReferenceValue3 = getProductReferenceWithId(targetProduct3.getId());
    final AttributeDraft targetProductRefAttr = AttributeDraft.of("product-reference", targetProductReferenceValue2);
    final AttributeDraft targetProductSetRefAttr = getReferenceSetAttributeDraft("product-reference-set", targetProductReferenceValue2, targetProductReferenceValue3);
    assertThat(updateActions).containsExactlyInAnyOrder(SetAttributeInAllVariants.of(targetProductRefAttr.getName(), targetProductRefAttr.getValue(), true), SetAttributeInAllVariants.of(targetProductSetRefAttr.getName(), targetProductSetRefAttr.getValue(), true), Publish.of());
}
Also used : ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) SUPPLY_CHANNEL_KEY_1(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.SUPPLY_CHANNEL_KEY_1) Reference(io.sphere.sdk.models.Reference) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) PRODUCT_KEY_2_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) StateITUtils.createState(com.commercetools.sync.integration.commons.utils.StateITUtils.createState) ProductITUtils.createPricesCustomType(com.commercetools.sync.integration.commons.utils.ProductITUtils.createPricesCustomType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryITUtils.getCategoryDrafts(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts) PRODUCT_KEY_1_CHANGED_WITH_PRICES_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_CHANGED_WITH_PRICES_RESOURCE_PATH) TaxCategoryITUtils.createTaxCategory(com.commercetools.sync.integration.commons.utils.TaxCategoryITUtils.createTaxCategory) ProductSyncMockUtils.createProductDraftBuilder(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder) UpdateAction(io.sphere.sdk.commands.UpdateAction) Channel(io.sphere.sdk.channels.Channel) Collections.singletonList(java.util.Collections.singletonList) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) AfterAll(org.junit.jupiter.api.AfterAll) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) OLD_CATEGORY_CUSTOM_TYPE_NAME(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME) StateType(io.sphere.sdk.states.StateType) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductITUtils.getDraftWithPriceReferences(com.commercetools.sync.integration.commons.utils.ProductITUtils.getDraftWithPriceReferences) PRODUCT_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH) ENGLISH(java.util.Locale.ENGLISH) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) SetAttributeInAllVariants(io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants) Product(io.sphere.sdk.products.Product) PRODUCT_KEY_1_CHANGED_ATTRIBUTES_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_CHANGED_ATTRIBUTES_RESOURCE_PATH) CategoryITUtils.getReferencesWithIds(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds) State(io.sphere.sdk.states.State) Test(org.junit.jupiter.api.Test) ProductSyncMockUtils.getReferenceSetAttributeDraft(com.commercetools.sync.products.ProductSyncMockUtils.getReferenceSetAttributeDraft) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Publish(io.sphere.sdk.products.commands.updateactions.Publish) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ChannelCreateCommand(io.sphere.sdk.channels.commands.ChannelCreateCommand) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) ChannelDraft(io.sphere.sdk.channels.ChannelDraft) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) ProductByKeyGet(io.sphere.sdk.products.queries.ProductByKeyGet) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) ProductITUtils.deleteAllProducts(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) SyncFilter(com.commercetools.sync.products.SyncFilter) ProductTransformUtils(com.commercetools.sync.products.utils.ProductTransformUtils) Attribute(io.sphere.sdk.products.attributes.Attribute) ATTRIBUTES(com.commercetools.sync.products.ActionGroup.ATTRIBUTES) ProductSync(com.commercetools.sync.products.ProductSync) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) SetAttribute(io.sphere.sdk.products.commands.updateactions.SetAttribute) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Collections.emptyMap(java.util.Collections.emptyMap) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH) PRODUCT_KEY_1_CHANGED_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_CHANGED_RESOURCE_PATH) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ProductSyncMockUtils.getProductReferenceWithId(com.commercetools.sync.products.ProductSyncMockUtils.getProductReferenceWithId) ProductSyncMockUtils.createRandomCategoryOrderHints(com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints) Collections(java.util.Collections) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) Type(io.sphere.sdk.types.Type) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ProductSyncMockUtils.getReferenceSetAttributeDraft(com.commercetools.sync.products.ProductSyncMockUtils.getReferenceSetAttributeDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) Product(io.sphere.sdk.products.Product) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) ProductSync(com.commercetools.sync.products.ProductSync) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Test(org.junit.jupiter.api.Test)

Example 2 with ProductSyncOptions

use of com.commercetools.sync.products.ProductSyncOptions in project commercetools-sync-java by commercetools.

the class ProductSyncWithPricesIT method setup.

@BeforeEach
void setup() {
    errorCallBackMessages = new ArrayList<>();
    errorCallBackExceptions = new ArrayList<>();
    warningCallBackMessages = new ArrayList<>();
    final ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).warningCallback((exception, oldResource, newResource) -> warningCallBackMessages.add(exception.getMessage())).build();
    productSync = new ProductSync(syncOptions);
    referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
}
Also used : ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) MoneyImpl(io.sphere.sdk.utils.MoneyImpl) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProductType(io.sphere.sdk.producttypes.ProductType) ProductTypeCreateCommand(io.sphere.sdk.producttypes.commands.ProductTypeCreateCommand) EUR(io.sphere.sdk.models.DefaultCurrencyUnits.EUR) PriceTier(io.sphere.sdk.products.PriceTier) ArrayList(java.util.ArrayList) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) BigDecimal(java.math.BigDecimal) PriceDraftBuilder(io.sphere.sdk.products.PriceDraftBuilder) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) ProductTransformUtils(com.commercetools.sync.products.utils.ProductTransformUtils) BeforeAll(org.junit.jupiter.api.BeforeAll) Arrays.asList(java.util.Arrays.asList) ProductSync(com.commercetools.sync.products.ProductSync) SphereClient(io.sphere.sdk.client.SphereClient) PriceTierBuilder(io.sphere.sdk.products.PriceTierBuilder) DE(com.neovisionaries.i18n.CountryCode.DE) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) Nonnull(javax.annotation.Nonnull) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) ProductTypeDraftBuilder(io.sphere.sdk.producttypes.ProductTypeDraftBuilder) Collections.emptyList(java.util.Collections.emptyList) Price(io.sphere.sdk.products.Price) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) PriceDraft(io.sphere.sdk.products.PriceDraft) ProductITUtils.createPriceDraft(com.commercetools.sync.integration.commons.utils.ProductITUtils.createPriceDraft) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) ProductProjectionByKeyGet(io.sphere.sdk.products.queries.ProductProjectionByKeyGet) ProductProjectionType(io.sphere.sdk.products.ProductProjectionType) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) AssertionsForStatistics(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) ProductSync(com.commercetools.sync.products.ProductSync) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ProductSyncOptions

use of com.commercetools.sync.products.ProductSyncOptions in project commercetools-sync-java by commercetools.

the class ProductReferenceResolverIT method setupTest.

/**
 * Deletes Products and Types from target CTP projects, then it populates target CTP project with
 * product test data.
 */
@BeforeEach
void setupTest() {
    deleteAllProducts(CTP_TARGET_CLIENT);
    deleteAllProducts(CTP_SOURCE_CLIENT);
    errorCallBackMessages = new ArrayList<>();
    errorCallBackExceptions = new ArrayList<>();
    warningCallBackMessages = new ArrayList<>();
    final ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).warningCallback((exception, oldResource, newResource) -> warningCallBackMessages.add(exception.getMessage())).build();
    productSync = new ProductSync(syncOptions);
    referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
}
Also used : ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) Reference(io.sphere.sdk.models.Reference) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) StateITUtils.createState(com.commercetools.sync.integration.commons.utils.StateITUtils.createState) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryITUtils.getCategoryDrafts(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts) TaxCategoryITUtils.createTaxCategory(com.commercetools.sync.integration.commons.utils.TaxCategoryITUtils.createTaxCategory) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) AfterAll(org.junit.jupiter.api.AfterAll) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) OLD_CATEGORY_CUSTOM_TYPE_NAME(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME) StateType(io.sphere.sdk.states.StateType) PRODUCT_TYPE_WITH_REFERENCES_FOR_VARIANT_ATTRIBUTES_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_WITH_REFERENCES_FOR_VARIANT_ATTRIBUTES_RESOURCE_PATH) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) PRODUCT_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Customer(io.sphere.sdk.customers.Customer) CategoryITUtils.getReferencesWithIds(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds) State(io.sphere.sdk.states.State) PRODUCT_TYPE_NO_KEY_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_NO_KEY_RESOURCE_PATH) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) List(java.util.List) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) ProductITUtils.deleteAllProducts(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts) PRODUCT_KEY_1_NO_ATTRIBUTES_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_NO_ATTRIBUTES_RESOURCE_PATH) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ProductTransformUtils(com.commercetools.sync.products.utils.ProductTransformUtils) ProductSync(com.commercetools.sync.products.ProductSync) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) Nonnull(javax.annotation.Nonnull) CustomerITUtils.createSampleCustomerJaneDoe(com.commercetools.sync.integration.commons.utils.CustomerITUtils.createSampleCustomerJaneDoe) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ProductSyncMockUtils.createRandomCategoryOrderHints(com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints) BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER(com.commercetools.sync.commons.helpers.BaseReferenceResolver.BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) ProductSync(com.commercetools.sync.products.ProductSync) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ProductSyncOptions

use of com.commercetools.sync.products.ProductSyncOptions in project commercetools-sync-java by commercetools.

the class ProductSyncWithUnexpandedReferencesIT method setup.

@BeforeEach
void setup() {
    errorCallBackMessages = new ArrayList<>();
    errorCallBackExceptions = new ArrayList<>();
    warningCallBackMessages = new ArrayList<>();
    final ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).warningCallback((exception, oldResource, newResource) -> warningCallBackMessages.add(exception.getMessage())).build();
    productSync = new ProductSync(syncOptions);
    referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
}
Also used : TypeDraft(io.sphere.sdk.types.TypeDraft) ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProductTypeCreateCommand(io.sphere.sdk.producttypes.commands.ProductTypeCreateCommand) Collections.singletonList(java.util.Collections.singletonList) FieldDefinition(io.sphere.sdk.types.FieldDefinition) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) AfterAll(org.junit.jupiter.api.AfterAll) BigDecimal(java.math.BigDecimal) TaxRateDraft(io.sphere.sdk.taxcategories.TaxRateDraft) Collections.singleton(java.util.Collections.singleton) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) StateCreateCommand(io.sphere.sdk.states.commands.StateCreateCommand) BeforeAll(org.junit.jupiter.api.BeforeAll) Arrays.asList(java.util.Arrays.asList) CustomerGroupCreateCommand(io.sphere.sdk.customergroups.commands.CustomerGroupCreateCommand) StateType(io.sphere.sdk.states.StateType) DE(com.neovisionaries.i18n.CountryCode.DE) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) TaxCategoryDraftBuilder(io.sphere.sdk.taxcategories.TaxCategoryDraftBuilder) AssetDraft(io.sphere.sdk.models.AssetDraft) AssetDraftBuilder(io.sphere.sdk.models.AssetDraftBuilder) ProductTypeDraftBuilder(io.sphere.sdk.producttypes.ProductTypeDraftBuilder) Collections.emptyList(java.util.Collections.emptyList) CategoryCreateCommand(io.sphere.sdk.categories.commands.CategoryCreateCommand) CustomerGroupDraftBuilder(io.sphere.sdk.customergroups.CustomerGroupDraftBuilder) State(io.sphere.sdk.states.State) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) TextInputHint(io.sphere.sdk.models.TextInputHint) ChannelCreateCommand(io.sphere.sdk.channels.commands.ChannelCreateCommand) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) StateDraft(io.sphere.sdk.states.StateDraft) AssertionsForStatistics(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics) ChannelDraft(io.sphere.sdk.channels.ChannelDraft) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) ResourceTypeIdsSetBuilder(io.sphere.sdk.types.ResourceTypeIdsSetBuilder) TaxCategoryDraft(io.sphere.sdk.taxcategories.TaxCategoryDraft) ProductType(io.sphere.sdk.producttypes.ProductType) EUR(io.sphere.sdk.models.DefaultCurrencyUnits.EUR) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategoryDraftBuilder(io.sphere.sdk.categories.CategoryDraftBuilder) ArrayList(java.util.ArrayList) PriceDraftBuilder(io.sphere.sdk.products.PriceDraftBuilder) ProductTransformUtils(com.commercetools.sync.products.utils.ProductTransformUtils) ProductSync(com.commercetools.sync.products.ProductSync) TypeDraftBuilder(io.sphere.sdk.types.TypeDraftBuilder) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) StateDraftBuilder(io.sphere.sdk.states.StateDraftBuilder) StringFieldType(io.sphere.sdk.types.StringFieldType) CountryCode(com.neovisionaries.i18n.CountryCode) ChannelRole(io.sphere.sdk.channels.ChannelRole) Collections.emptyMap(java.util.Collections.emptyMap) CustomerGroup(io.sphere.sdk.customergroups.CustomerGroup) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) AssetSourceBuilder(io.sphere.sdk.models.AssetSourceBuilder) TypeCreateCommand(io.sphere.sdk.types.commands.TypeCreateCommand) ChannelDraftBuilder(io.sphere.sdk.channels.ChannelDraftBuilder) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) PriceDraft(io.sphere.sdk.products.PriceDraft) ProductITUtils.createPriceDraft(com.commercetools.sync.integration.commons.utils.ProductITUtils.createPriceDraft) CustomerGroupDraft(io.sphere.sdk.customergroups.CustomerGroupDraft) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) TaxRateDraftBuilder(io.sphere.sdk.taxcategories.TaxRateDraftBuilder) TaxCategoryCreateCommand(io.sphere.sdk.taxcategories.commands.TaxCategoryCreateCommand) Collections(java.util.Collections) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) Type(io.sphere.sdk.types.Type) ProductSync(com.commercetools.sync.products.ProductSync) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with ProductSyncOptions

use of com.commercetools.sync.products.ProductSyncOptions in project commercetools-sync-java by commercetools.

the class ProductServiceImplIT method fetchProduct_WithBadGatewayException_ShouldFail.

@Test
void fetchProduct_WithBadGatewayException_ShouldFail() {
    // preparation
    // Mock sphere client to return BadGatewayException on any request.
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    when(spyClient.execute(any(ProductProjectionQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
    final ProductSyncOptions spyOptions = ProductSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).warningCallback((exception, oldResource, newResource) -> warningCallBackMessages.add(exception.getMessage())).build();
    final ProductService spyProductService = new ProductServiceImpl(spyOptions);
    final String productKey = product.getKey();
    // test and assertion
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(spyProductService.fetchProduct(productKey)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
Also used : ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Reference(io.sphere.sdk.models.Reference) PRODUCT_KEY_2_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryITUtils.getCategoryDrafts(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts) ProductSyncMockUtils.createProductDraftBuilder(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder) UpdateAction(io.sphere.sdk.commands.UpdateAction) StringUtils(org.apache.commons.lang3.StringUtils) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) AfterAll(org.junit.jupiter.api.AfterAll) ChangeName(io.sphere.sdk.products.commands.updateactions.ChangeName) Collections.singleton(java.util.Collections.singleton) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) OLD_CATEGORY_CUSTOM_TYPE_NAME(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) PRODUCT_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Product(io.sphere.sdk.products.Product) CategoryITUtils.getReferencesWithIds(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ChangeSlug(io.sphere.sdk.products.commands.updateactions.ChangeSlug) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) List(java.util.List) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ProductProjectionType(io.sphere.sdk.products.ProductProjectionType) Optional(java.util.Optional) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) AddExternalImage(io.sphere.sdk.products.commands.updateactions.AddExternalImage) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) ProductType(io.sphere.sdk.producttypes.ProductType) ProductITUtils.deleteAllProducts(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) ImageDimensions(io.sphere.sdk.products.ImageDimensions) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Image(io.sphere.sdk.products.Image) Collections.emptySet(java.util.Collections.emptySet) QueryPredicate(io.sphere.sdk.queries.QueryPredicate) SetKey(io.sphere.sdk.products.commands.updateactions.SetKey) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) ProductSyncMockUtils.createRandomCategoryOrderHints(com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints) ProductService(com.commercetools.sync.services.ProductService) Collections(java.util.Collections) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) ProductService(com.commercetools.sync.services.ProductService) SphereClient(io.sphere.sdk.client.SphereClient) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) LocalizedString(io.sphere.sdk.models.LocalizedString) ExecutionException(java.util.concurrent.ExecutionException) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) Test(org.junit.jupiter.api.Test)

Aggregations

ProductSyncOptions (com.commercetools.sync.products.ProductSyncOptions)75 BeforeEach (org.junit.jupiter.api.BeforeEach)46 Test (org.junit.jupiter.api.Test)42 ProductDraft (io.sphere.sdk.products.ProductDraft)41 ArrayList (java.util.ArrayList)36 List (java.util.List)36 UpdateAction (io.sphere.sdk.commands.UpdateAction)34 Product (io.sphere.sdk.products.Product)34 ProductSyncOptionsBuilder (com.commercetools.sync.products.ProductSyncOptionsBuilder)33 ProductProjection (io.sphere.sdk.products.ProductProjection)32 Collections.emptyList (java.util.Collections.emptyList)32 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)32 SphereClient (io.sphere.sdk.client.SphereClient)31 String.format (java.lang.String.format)25 HashMap (java.util.HashMap)25 Collections.singletonList (java.util.Collections.singletonList)22 Map (java.util.Map)22 VariantReferenceResolver (com.commercetools.sync.products.helpers.VariantReferenceResolver)20 Category (io.sphere.sdk.categories.Category)20 Reference (io.sphere.sdk.models.Reference)20