Search in sources :

Example 56 with ProductSync

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

the class ProductSyncWithReferencedProductsIT method sync_withNonExistingProductReferenceAsAttribute_ShouldFailCreatingTheProduct.

@Test
void sync_withNonExistingProductReferenceAsAttribute_ShouldFailCreatingTheProduct() {
    // preparation
    final AttributeDraft productReferenceAttribute = AttributeDraft.of("product-reference", Reference.of(Product.referenceTypeId(), "nonExistingKey"));
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(productReferenceAttribute).build();
    final ProductDraft productDraftWithProductReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), masterVariant).key("new-product").build();
    // test
    final ProductSync productSync = new ProductSync(syncOptions);
    final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(productDraftWithProductReference)).toCompletableFuture().join();
    // assertion
    assertThat(syncStatistics).hasValues(1, 0, 0, 0, 1);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    assertThat(actions).isEmpty();
    final UnresolvedReferencesServiceImpl<WaitingToBeResolvedProducts> unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(syncOptions);
    final Set<WaitingToBeResolvedProducts> waitingToBeResolvedDrafts = unresolvedReferencesService.fetch(asSet(productDraftWithProductReference.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    assertThat(waitingToBeResolvedDrafts).singleElement().matches(waitingToBeResolvedDraft -> {
        assertThat(waitingToBeResolvedDraft.getProductDraft().getKey()).isEqualTo(productDraftWithProductReference.getKey());
        assertThat(waitingToBeResolvedDraft.getMissingReferencedProductKeys()).containsExactly("nonExistingKey");
        return true;
    });
}
Also used : ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) ProductDraft(io.sphere.sdk.products.ProductDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) ProductSync(com.commercetools.sync.products.ProductSync) Test(org.junit.jupiter.api.Test)

Example 57 with ProductSync

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

the class ProductSyncWithReferencedProductsIT method sync_withProductReferenceSetAsAttribute_shouldCreateProductReferencingExistingProducts.

@Test
void sync_withProductReferenceSetAsAttribute_shouldCreateProductReferencingExistingProducts() {
    // preparation
    final AttributeDraft productReferenceAttribute = AttributeDraft.of("product-reference", Reference.of(Product.referenceTypeId(), product.getKey()));
    final HashSet<Reference<Product>> references = new HashSet<>();
    references.add(Reference.of(Product.referenceTypeId(), product.getKey()));
    references.add(Reference.of(Product.referenceTypeId(), product2.getKey()));
    final AttributeDraft productReferenceSetAttribute = AttributeDraft.of("product-reference-set", references);
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(productReferenceAttribute, productReferenceSetAttribute).build();
    final ProductDraft productDraftWithProductReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), masterVariant).key("new-product").build();
    // test
    final ProductSync productSync = new ProductSync(syncOptions);
    final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(productDraftWithProductReference)).toCompletableFuture().join();
    // assertion
    assertThat(syncStatistics).hasValues(1, 1, 0, 0, 0);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    assertThat(actions).isEmpty();
    final Product createdProduct = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(productDraftWithProductReference.getKey())).toCompletableFuture().join();
    final Optional<Attribute> createdProductReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceAttribute.getName());
    assertThat(createdProductReferenceAttribute).hasValueSatisfying(attribute -> {
        assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
        assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(product.getId());
    });
    final Optional<Attribute> createdProductReferenceSetAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceSetAttribute.getName());
    assertThat(createdProductReferenceSetAttribute).hasValueSatisfying(attribute -> {
        assertThat(attribute.getValueAsJsonNode()).isInstanceOf(ArrayNode.class);
        final ArrayNode referenceSet = (ArrayNode) attribute.getValueAsJsonNode();
        assertThat(referenceSet).hasSize(2).anySatisfy(reference -> {
            assertThat(reference.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
            assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(product.getId());
        }).anySatisfy(reference -> {
            assertThat(reference.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
            assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(product2.getId());
        });
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Reference(io.sphere.sdk.models.Reference) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) Collections.singletonList(java.util.Collections.singletonList) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) AfterAll(org.junit.jupiter.api.AfterAll) BeforeAll(org.junit.jupiter.api.BeforeAll) TriConsumer(com.commercetools.sync.commons.utils.TriConsumer) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) Collections.emptyList(java.util.Collections.emptyList) SetAttributeInAllVariants(io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants) Set(java.util.Set) Product(io.sphere.sdk.products.Product) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Test(org.junit.jupiter.api.Test) 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) SphereInternalUtils.asSet(io.sphere.sdk.utils.SphereInternalUtils.asSet) Optional(java.util.Optional) REFERENCE_TYPE_ID_FIELD(com.commercetools.sync.commons.utils.ResourceIdentifierUtils.REFERENCE_TYPE_ID_FIELD) 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) CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) 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) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Attribute(io.sphere.sdk.products.attributes.Attribute) ProductSync(com.commercetools.sync.products.ProductSync) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) REFERENCE_ID_FIELD(com.commercetools.sync.commons.utils.ResourceIdentifierUtils.REFERENCE_ID_FIELD) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) Attribute(io.sphere.sdk.products.attributes.Attribute) Reference(io.sphere.sdk.models.Reference) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) Product(io.sphere.sdk.products.Product) ProductSync(com.commercetools.sync.products.ProductSync) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductDraft(io.sphere.sdk.products.ProductDraft) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 58 with ProductSync

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

the class ProductSyncIT method sync_withMissingPriceChannel_shouldCreateProductDistributionPriceChannel.

@Test
void sync_withMissingPriceChannel_shouldCreateProductDistributionPriceChannel() {
    PriceDraftDsl priceDraftWithMissingChannelRef = PriceDraftBuilder.of(MoneyImpl.of("20", "EUR")).channel(ResourceIdentifier.ofKey("missingKey")).build();
    ProductVariantDraftDsl masterVariantDraft = ProductVariantDraftBuilder.of(ProductVariantDraftDsl.of().withKey("v2").withSku("1065833").withPrices(Collections.singletonList(priceDraftWithMissingChannelRef))).build();
    final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).masterVariant(masterVariantDraft).taxCategory(null).state(null).build();
    final TriConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>> warningCallBack = (exception, newResource, oldResource) -> warningCallBackMessages.add(exception.getMessage());
    ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> collectErrors(exception.getMessage(), exception.getCause())).warningCallback(warningCallBack).ensurePriceChannels(true).build();
    final ProductSync productSync = new ProductSync(syncOptions);
    final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(singletonList(productDraft)));
    assertThat(syncStatistics).hasValues(1, 1, 0, 0, 0);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    Product productFromTargetProject = executeBlocking(CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(productDraft.getKey())));
    List<Price> prices = productFromTargetProject.getMasterData().getStaged().getMasterVariant().getPrices();
    assertThat(prices.size()).isEqualTo(1);
    Channel channel = executeBlocking(CTP_TARGET_CLIENT.execute(ChannelByIdGet.of(Objects.requireNonNull(Objects.requireNonNull(prices.get(0).getChannel()).getId()))));
    assertThat(channel.getRoles()).containsOnly(ChannelRole.PRODUCT_DISTRIBUTION);
}
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) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) SyncException(com.commercetools.sync.commons.exceptions.SyncException) MoneyImpl(io.sphere.sdk.utils.MoneyImpl) StateITUtils.createState(com.commercetools.sync.integration.commons.utils.StateITUtils.createState) CategoryITUtils.getCategoryDrafts(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts) TaxCategoryITUtils.createTaxCategory(com.commercetools.sync.integration.commons.utils.TaxCategoryITUtils.createTaxCategory) Channel(io.sphere.sdk.channels.Channel) Collections.singletonList(java.util.Collections.singletonList) AfterAll(org.junit.jupiter.api.AfterAll) BeforeAll(org.junit.jupiter.api.BeforeAll) Arrays.asList(java.util.Arrays.asList) OLD_CATEGORY_CUSTOM_TYPE_NAME(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME) SphereClient(io.sphere.sdk.client.SphereClient) StateType(io.sphere.sdk.states.StateType) TriConsumer(com.commercetools.sync.commons.utils.TriConsumer) ProductProjection(io.sphere.sdk.products.ProductProjection) PRODUCT_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) BadGatewayException(io.sphere.sdk.client.BadGatewayException) SetAttributeInAllVariants(io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants) Price(io.sphere.sdk.products.Price) Set(java.util.Set) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) ProductUpdateCommand(io.sphere.sdk.products.commands.ProductUpdateCommand) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) ProductITUtils.deleteAllProducts(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts) Mockito.spy(org.mockito.Mockito.spy) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) ArrayList(java.util.ArrayList) PriceDraftBuilder(io.sphere.sdk.products.PriceDraftBuilder) CategoryITUtils.replaceCategoryOrderHintCategoryIdsWithKeys(com.commercetools.sync.integration.commons.utils.CategoryITUtils.replaceCategoryOrderHintCategoryIdsWithKeys) ProductSync(com.commercetools.sync.products.ProductSync) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) ChannelByIdGet(io.sphere.sdk.channels.queries.ChannelByIdGet) PRODUCT_KEY_1_CHANGED_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_CHANGED_RESOURCE_PATH) CategoryITUtils.geResourceIdentifiersWithKeys(com.commercetools.sync.integration.commons.utils.CategoryITUtils.geResourceIdentifiersWithKeys) FAILED_TO_BUILD_ATTRIBUTE_UPDATE_ACTION(com.commercetools.sync.products.utils.ProductVariantUpdateActionUtils.FAILED_TO_BUILD_ATTRIBUTE_UPDATE_ACTION) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) RemoveFromCategory(io.sphere.sdk.products.commands.updateactions.RemoveFromCategory) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) PRODUCT_KEY_2_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProductSyncMockUtils.createProductDraftBuilder(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) SetTaxCategory(io.sphere.sdk.products.commands.updateactions.SetTaxCategory) Locale(java.util.Locale) ProductDraft(io.sphere.sdk.products.ProductDraft) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) Product(io.sphere.sdk.products.Product) CategoryITUtils.getReferencesWithIds(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds) State(io.sphere.sdk.states.State) String.format(java.lang.String.format) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) List(java.util.List) Publish(io.sphere.sdk.products.commands.updateactions.Publish) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) Optional(java.util.Optional) ProductVariantDraftDsl(io.sphere.sdk.products.ProductVariantDraftDsl) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) ProductByKeyGet(io.sphere.sdk.products.queries.ProductByKeyGet) CategoryOrderHints(io.sphere.sdk.products.CategoryOrderHints) ProductType(io.sphere.sdk.producttypes.ProductType) ProductQuery(io.sphere.sdk.products.queries.ProductQuery) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) SetAttribute(io.sphere.sdk.products.commands.updateactions.SetAttribute) Nonnull(javax.annotation.Nonnull) ChannelRole(io.sphere.sdk.channels.ChannelRole) PriceDraftDsl(io.sphere.sdk.products.PriceDraftDsl) QueryPredicate(io.sphere.sdk.queries.QueryPredicate) ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA(com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Mockito.when(org.mockito.Mockito.when) Collectors.toList(java.util.stream.Collectors.toList) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) ProductSyncMockUtils.createRandomCategoryOrderHints(com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints) Collections(java.util.Collections) Optional(java.util.Optional) ProductVariantDraftDsl(io.sphere.sdk.products.ProductVariantDraftDsl) Channel(io.sphere.sdk.channels.Channel) Product(io.sphere.sdk.products.Product) ProductSync(com.commercetools.sync.products.ProductSync) SyncException(com.commercetools.sync.commons.exceptions.SyncException) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) PriceDraftDsl(io.sphere.sdk.products.PriceDraftDsl) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) Price(io.sphere.sdk.products.Price) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Test(org.junit.jupiter.api.Test)

Example 59 with ProductSync

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

the class ProductSyncIT method sync_withProductContainingAttributeChanges_shouldSyncProductCorrectly.

@Test
void sync_withProductContainingAttributeChanges_shouldSyncProductCorrectly() {
    // preparation
    final List<UpdateAction<Product>> updateActions = new ArrayList<>();
    final TriConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>> warningCallBack = (exception, newResource, oldResource) -> warningCallBackMessages.add(exception.getMessage());
    final ProductSyncOptions customOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> collectErrors(exception.getMessage(), exception.getCause())).warningCallback(warningCallBack).beforeUpdateCallback((actions, draft, old) -> {
        updateActions.addAll(actions);
        return actions;
    }).build();
    final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_1_RESOURCE_PATH, ProductType.referenceOfId(productType.getKey())).categories(emptyList()).taxCategory(null).state(null).build();
    // Creating the attribute draft with the changes
    final AttributeDraft priceInfoAttrDraft = AttributeDraft.of("priceInfo", JsonNodeFactory.instance.textNode("100/kg"));
    final AttributeDraft angebotAttrDraft = AttributeDraft.of("angebot", JsonNodeFactory.instance.textNode("big discount"));
    final AttributeDraft unknownAttrDraft = AttributeDraft.of("unknown", JsonNodeFactory.instance.textNode("unknown"));
    // Creating the product variant draft with the product reference attribute
    final List<AttributeDraft> attributes = asList(priceInfoAttrDraft, angebotAttrDraft, unknownAttrDraft);
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of(productDraft.getMasterVariant()).attributes(attributes).build();
    final ProductDraft productDraftWithChangedAttributes = ProductDraftBuilder.of(productDraft).masterVariant(masterVariant).build();
    // test
    final ProductSync productSync = new ProductSync(customOptions);
    final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(singletonList(productDraftWithChangedAttributes)));
    // assertion
    assertThat(syncStatistics).hasValues(1, 0, 1, 0, 0);
    final String causeErrorMessage = format(ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA, unknownAttrDraft.getName());
    final String expectedErrorMessage = format(FAILED_TO_BUILD_ATTRIBUTE_UPDATE_ACTION, unknownAttrDraft.getName(), productDraft.getMasterVariant().getKey(), productDraft.getKey(), causeErrorMessage);
    assertThat(errorCallBackExceptions).hasSize(1);
    assertThat(errorCallBackExceptions.get(0).getMessage()).isEqualTo(expectedErrorMessage);
    assertThat(errorCallBackExceptions.get(0).getCause().getMessage()).isEqualTo(causeErrorMessage);
    assertThat(errorCallBackMessages).containsExactly(expectedErrorMessage);
    assertThat(warningCallBackMessages).isEmpty();
    assertThat(updateActions).filteredOn(updateAction -> !(updateAction instanceof SetTaxCategory)).filteredOn(updateAction -> !(updateAction instanceof RemoveFromCategory)).containsExactlyInAnyOrder(SetAttributeInAllVariants.of(priceInfoAttrDraft, true), SetAttribute.of(1, angebotAttrDraft, true), SetAttributeInAllVariants.ofUnsetAttribute("size", true), SetAttributeInAllVariants.ofUnsetAttribute("rinderrasse", true), SetAttributeInAllVariants.ofUnsetAttribute("herkunft", true), SetAttributeInAllVariants.ofUnsetAttribute("teilstueck", true), SetAttributeInAllVariants.ofUnsetAttribute("fuetterung", true), SetAttributeInAllVariants.ofUnsetAttribute("reifung", true), SetAttributeInAllVariants.ofUnsetAttribute("haltbarkeit", true), SetAttributeInAllVariants.ofUnsetAttribute("verpackung", true), SetAttributeInAllVariants.ofUnsetAttribute("anlieferung", true), SetAttributeInAllVariants.ofUnsetAttribute("zubereitung", true), SetAttribute.ofUnsetAttribute(1, "localisedText", true), Publish.of());
}
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) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) SyncException(com.commercetools.sync.commons.exceptions.SyncException) MoneyImpl(io.sphere.sdk.utils.MoneyImpl) StateITUtils.createState(com.commercetools.sync.integration.commons.utils.StateITUtils.createState) CategoryITUtils.getCategoryDrafts(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts) TaxCategoryITUtils.createTaxCategory(com.commercetools.sync.integration.commons.utils.TaxCategoryITUtils.createTaxCategory) Channel(io.sphere.sdk.channels.Channel) Collections.singletonList(java.util.Collections.singletonList) AfterAll(org.junit.jupiter.api.AfterAll) BeforeAll(org.junit.jupiter.api.BeforeAll) Arrays.asList(java.util.Arrays.asList) OLD_CATEGORY_CUSTOM_TYPE_NAME(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME) SphereClient(io.sphere.sdk.client.SphereClient) StateType(io.sphere.sdk.states.StateType) TriConsumer(com.commercetools.sync.commons.utils.TriConsumer) ProductProjection(io.sphere.sdk.products.ProductProjection) PRODUCT_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) BadGatewayException(io.sphere.sdk.client.BadGatewayException) SetAttributeInAllVariants(io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants) Price(io.sphere.sdk.products.Price) Set(java.util.Set) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) ProductUpdateCommand(io.sphere.sdk.products.commands.ProductUpdateCommand) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) ProductITUtils.deleteAllProducts(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts) Mockito.spy(org.mockito.Mockito.spy) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) ArrayList(java.util.ArrayList) PriceDraftBuilder(io.sphere.sdk.products.PriceDraftBuilder) CategoryITUtils.replaceCategoryOrderHintCategoryIdsWithKeys(com.commercetools.sync.integration.commons.utils.CategoryITUtils.replaceCategoryOrderHintCategoryIdsWithKeys) ProductSync(com.commercetools.sync.products.ProductSync) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) ChannelByIdGet(io.sphere.sdk.channels.queries.ChannelByIdGet) PRODUCT_KEY_1_CHANGED_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_CHANGED_RESOURCE_PATH) CategoryITUtils.geResourceIdentifiersWithKeys(com.commercetools.sync.integration.commons.utils.CategoryITUtils.geResourceIdentifiersWithKeys) FAILED_TO_BUILD_ATTRIBUTE_UPDATE_ACTION(com.commercetools.sync.products.utils.ProductVariantUpdateActionUtils.FAILED_TO_BUILD_ATTRIBUTE_UPDATE_ACTION) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) RemoveFromCategory(io.sphere.sdk.products.commands.updateactions.RemoveFromCategory) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) PRODUCT_KEY_2_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProductSyncMockUtils.createProductDraftBuilder(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) SetTaxCategory(io.sphere.sdk.products.commands.updateactions.SetTaxCategory) Locale(java.util.Locale) ProductDraft(io.sphere.sdk.products.ProductDraft) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) Product(io.sphere.sdk.products.Product) CategoryITUtils.getReferencesWithIds(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds) State(io.sphere.sdk.states.State) String.format(java.lang.String.format) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) List(java.util.List) Publish(io.sphere.sdk.products.commands.updateactions.Publish) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) Optional(java.util.Optional) ProductVariantDraftDsl(io.sphere.sdk.products.ProductVariantDraftDsl) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) ProductByKeyGet(io.sphere.sdk.products.queries.ProductByKeyGet) CategoryOrderHints(io.sphere.sdk.products.CategoryOrderHints) ProductType(io.sphere.sdk.producttypes.ProductType) ProductQuery(io.sphere.sdk.products.queries.ProductQuery) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) SetAttribute(io.sphere.sdk.products.commands.updateactions.SetAttribute) Nonnull(javax.annotation.Nonnull) ChannelRole(io.sphere.sdk.channels.ChannelRole) PriceDraftDsl(io.sphere.sdk.products.PriceDraftDsl) QueryPredicate(io.sphere.sdk.queries.QueryPredicate) ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA(com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Mockito.when(org.mockito.Mockito.when) Collectors.toList(java.util.stream.Collectors.toList) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) ProductSyncMockUtils.createRandomCategoryOrderHints(com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints) Collections(java.util.Collections) RemoveFromCategory(io.sphere.sdk.products.commands.updateactions.RemoveFromCategory) Optional(java.util.Optional) UpdateAction(io.sphere.sdk.commands.UpdateAction) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ArrayList(java.util.ArrayList) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) ProductSync(com.commercetools.sync.products.ProductSync) SyncException(com.commercetools.sync.commons.exceptions.SyncException) LocalizedString(io.sphere.sdk.models.LocalizedString) SetTaxCategory(io.sphere.sdk.products.commands.updateactions.SetTaxCategory) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) 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 60 with ProductSync

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

the class ProductSyncIT method sync_withSingleBatchSyncing_ShouldSync.

@Test
void sync_withSingleBatchSyncing_ShouldSync() {
    // Prepare batches from external source
    final ProductDraft productDraft = createProductDraft(PRODUCT_KEY_1_CHANGED_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey()), ResourceIdentifier.ofKey(targetTaxCategory.getKey()), ResourceIdentifier.ofKey(targetProductState.getKey()), categoryResourceIdentifiersWithKeys, categoryOrderHintsWithKeys);
    final ProductDraft key3Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(ResourceIdentifier.ofKey(targetTaxCategory.getKey())).state(ResourceIdentifier.ofKey(targetProductState.getKey())).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey3").slug(LocalizedString.of(Locale.ENGLISH, "slug3")).masterVariant(ProductVariantDraftBuilder.of().key("mv3").sku("sku3").build()).build();
    final ProductDraft key4Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(ResourceIdentifier.ofKey(targetTaxCategory.getKey())).state(ResourceIdentifier.ofKey(targetProductState.getKey())).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey4").slug(LocalizedString.of(Locale.ENGLISH, "slug4")).masterVariant(ProductVariantDraftBuilder.of().key("mv4").sku("sku4").build()).build();
    final ProductDraft key5Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(ResourceIdentifier.ofKey(targetTaxCategory.getKey())).state(ResourceIdentifier.ofKey(targetProductState.getKey())).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey5").slug(LocalizedString.of(Locale.ENGLISH, "slug5")).masterVariant(ProductVariantDraftBuilder.of().key("mv5").sku("sku5").build()).build();
    final ProductDraft key6Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(ResourceIdentifier.ofKey(targetTaxCategory.getKey())).state(ResourceIdentifier.ofKey(targetProductState.getKey())).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey6").slug(LocalizedString.of(Locale.ENGLISH, "slug6")).masterVariant(ProductVariantDraftBuilder.of().key("mv6").sku("sku6").build()).build();
    final List<ProductDraft> batch = new ArrayList<>();
    batch.add(productDraft);
    batch.add(key3Draft);
    batch.add(key4Draft);
    batch.add(key5Draft);
    batch.add(key6Draft);
    final ProductSync productSync = new ProductSync(syncOptions);
    final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(batch));
    assertThat(syncStatistics).hasValues(5, 4, 1, 0, 0);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
}
Also used : ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProductSync(com.commercetools.sync.products.ProductSync) Test(org.junit.jupiter.api.Test)

Aggregations

ProductSync (com.commercetools.sync.products.ProductSync)89 ProductDraft (io.sphere.sdk.products.ProductDraft)85 ProductSyncStatistics (com.commercetools.sync.products.helpers.ProductSyncStatistics)84 Test (org.junit.jupiter.api.Test)83 AttributeDraft (io.sphere.sdk.products.attributes.AttributeDraft)66 ProductVariantDraft (io.sphere.sdk.products.ProductVariantDraft)64 Product (io.sphere.sdk.products.Product)56 Attribute (io.sphere.sdk.products.attributes.Attribute)41 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)35 ProductSyncOptions (com.commercetools.sync.products.ProductSyncOptions)33 SetAttribute (io.sphere.sdk.products.commands.updateactions.SetAttribute)33 BeforeEach (org.junit.jupiter.api.BeforeEach)33 ArrayList (java.util.ArrayList)32 ProductProjection (io.sphere.sdk.products.ProductProjection)30 ProductSyncOptionsBuilder (com.commercetools.sync.products.ProductSyncOptionsBuilder)29 List (java.util.List)29 ProductITUtils.deleteProductSyncTestData (com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData)28 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)28 ProductDraftBuilder (io.sphere.sdk.products.ProductDraftBuilder)28 ProductVariantDraftBuilder (io.sphere.sdk.products.ProductVariantDraftBuilder)28