Search in sources :

Example 46 with ProductSync

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

the class ProductSyncWithReferencedCategoriesIT method sync_withCategoryReferenceSetAsAttribute_shouldCreateProductReferencingExistingCategories.

@Test
void sync_withCategoryReferenceSetAsAttribute_shouldCreateProductReferencingExistingCategories() {
    // preparation
    final AttributeDraft categoryReferenceAttribute = AttributeDraft.of("category-reference", Reference.of(Category.referenceTypeId(), category.getKey()));
    final HashSet<Reference<Category>> references = new HashSet<>();
    references.add(Reference.of(Category.referenceTypeId(), category.getKey()));
    references.add(Reference.of(Category.referenceTypeId(), category2.getKey()));
    final AttributeDraft categoryReferenceSetAttribute = AttributeDraft.of("category-reference-set", references);
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(categoryReferenceAttribute, categoryReferenceSetAttribute).build();
    final ProductDraft productDraftWithCategoryReference = 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(productDraftWithCategoryReference)).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(productDraftWithCategoryReference.getKey())).toCompletableFuture().join();
    final Optional<Attribute> createdProductReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(categoryReferenceAttribute.getName());
    assertThat(createdProductReferenceAttribute).hasValueSatisfying(attribute -> {
        assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Category.referenceTypeId());
        assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(category.getId());
    });
    final Optional<Attribute> createdCategoryReferenceSetAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(categoryReferenceSetAttribute.getName());
    assertThat(createdCategoryReferenceSetAttribute).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(Category.referenceTypeId());
            assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(category.getId());
        }).anySatisfy(reference -> {
            assertThat(reference.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Category.referenceTypeId());
            assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(category2.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) Category(io.sphere.sdk.categories.Category) SetAttributeInAllVariants(io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants) CategoryCreateCommand(io.sphere.sdk.categories.commands.CategoryCreateCommand) 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) 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) 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) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategoryDraftBuilder(io.sphere.sdk.categories.CategoryDraftBuilder) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Attribute(io.sphere.sdk.products.attributes.Attribute) Assertions.as(org.assertj.core.api.Assertions.as) 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) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) 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 47 with ProductSync

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

the class ProductSyncWithReferencedCategoriesIT method sync_withCategoryReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct.

@Test
void sync_withCategoryReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct() {
    // preparation
    final AttributeDraft categoryReferenceAttribute = AttributeDraft.of("category-reference", Reference.of(Category.referenceTypeId(), category.getKey()));
    final HashSet<Reference<Category>> references = new HashSet<>();
    references.add(Reference.of(Category.referenceTypeId(), "nonExistingKey"));
    references.add(Reference.of(Category.referenceTypeId(), category2.getKey()));
    final AttributeDraft categoryReferenceSetAttribute = AttributeDraft.of("category-reference-set", references);
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(categoryReferenceAttribute, categoryReferenceSetAttribute).build();
    final ProductDraft productDraftWithCategoryReference = 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(productDraftWithCategoryReference)).toCompletableFuture().join();
    // assertion
    assertThat(syncStatistics).hasValues(1, 0, 0, 1, 0);
    assertThat(errorCallBackExceptions).hasSize(1).singleElement().matches(error -> {
        assertThat(error).hasCauseExactlyInstanceOf(ErrorResponseException.class);
        final ErrorResponseException errorResponseException = (ErrorResponseException) error.getCause();
        assertThat(errorResponseException.getStatusCode()).isEqualTo(400);
        assertThat(error.getMessage()).contains("The value '{\"typeId\":\"category\",\"id\":\"nonExistingKey\"}' " + "is not valid for field 'category-reference-set'");
        return true;
    });
    assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains("The value '{\"typeId\":\"category\",\"id\":\"nonExistingKey\"}' " + "is not valid for field 'category-reference-set'");
    assertThat(warningCallBackMessages).isEmpty();
    assertThat(actions).isEmpty();
}
Also used : ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductDraft(io.sphere.sdk.products.ProductDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) Reference(io.sphere.sdk.models.Reference) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) ProductSync(com.commercetools.sync.products.ProductSync) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 48 with ProductSync

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

the class ProductSyncWithReferencedCategoriesIT method sync_withChangedCategoryReferenceAsAttribute_shouldUpdateProductReferencingExistingCategory.

@Test
void sync_withChangedCategoryReferenceAsAttribute_shouldUpdateProductReferencingExistingCategory() {
    // preparation
    final AttributeDraft categoryReferenceAttribute = AttributeDraft.of("category-reference", Reference.of(Category.referenceTypeId(), category));
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(categoryReferenceAttribute).build();
    final ProductDraft productDraftWithCategoryReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), masterVariant).key("new-product").build();
    CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraftWithCategoryReference)).toCompletableFuture().join();
    final AttributeDraft newCategoryReferenceAttribute = AttributeDraft.of("category-reference", Reference.of(Category.referenceTypeId(), category2.getKey()));
    final ProductVariantDraft newMasterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(newCategoryReferenceAttribute).build();
    final ProductDraft newProductDraftWithCategoryReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), newMasterVariant).key("new-product").build();
    // test
    final ProductSync productSync = new ProductSync(syncOptions);
    final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(newProductDraftWithCategoryReference)).toCompletableFuture().join();
    // assertion
    assertThat(syncStatistics).hasValues(1, 0, 1, 0, 0);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    final AttributeDraft expectedAttribute = AttributeDraft.of("category-reference", Reference.of(Category.referenceTypeId(), category2.getId()));
    assertThat(actions).containsExactly(SetAttributeInAllVariants.of(expectedAttribute, true));
    final Product createdProduct = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(productDraftWithCategoryReference.getKey())).toCompletableFuture().join();
    final Optional<Attribute> createdProductReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(categoryReferenceAttribute.getName());
    assertThat(createdProductReferenceAttribute).hasValueSatisfying(attribute -> {
        assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Category.referenceTypeId());
        assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(category2.getId());
    });
}
Also used : ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductDraft(io.sphere.sdk.products.ProductDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) Attribute(io.sphere.sdk.products.attributes.Attribute) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) Product(io.sphere.sdk.products.Product) ProductSync(com.commercetools.sync.products.ProductSync) Test(org.junit.jupiter.api.Test)

Example 49 with ProductSync

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

the class ProductSyncWithReferencedCategoriesIT method sync_withSameCategoryReferenceAsAttribute_shouldNotSyncAnythingNew.

@Test
void sync_withSameCategoryReferenceAsAttribute_shouldNotSyncAnythingNew() {
    // preparation
    final AttributeDraft categoryReferenceAttribute = AttributeDraft.of("category-reference", Reference.of(Category.referenceTypeId(), category));
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(categoryReferenceAttribute).build();
    final ProductDraft productDraftWithCategoryReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), masterVariant).key("new-product").build();
    CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraftWithCategoryReference)).toCompletableFuture().join();
    final AttributeDraft newProductReferenceAttribute = AttributeDraft.of("category-reference", Reference.of(Category.referenceTypeId(), category.getKey()));
    final ProductVariantDraft newMasterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(newProductReferenceAttribute).build();
    final ProductDraft newProductDraftWithProductReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), newMasterVariant).key("new-product").build();
    // test
    final ProductSync productSync = new ProductSync(syncOptions);
    final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(newProductDraftWithProductReference)).toCompletableFuture().join();
    // assertion
    assertThat(syncStatistics).hasValues(1, 0, 0, 0, 0);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    assertThat(actions).isEmpty();
    final Product createdProduct = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(productDraftWithCategoryReference.getKey())).toCompletableFuture().join();
    final Optional<Attribute> createdProductReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(categoryReferenceAttribute.getName());
    assertThat(createdProductReferenceAttribute).hasValueSatisfying(attribute -> {
        assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Category.referenceTypeId());
        assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(category.getId());
    });
}
Also used : ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductDraft(io.sphere.sdk.products.ProductDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) Attribute(io.sphere.sdk.products.attributes.Attribute) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) Product(io.sphere.sdk.products.Product) ProductSync(com.commercetools.sync.products.ProductSync) Test(org.junit.jupiter.api.Test)

Example 50 with ProductSync

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

the class ProductSyncWithReferencedProductTypesIT method sync_withProductTypeReferenceAsAttribute_shouldCreateProductReferencingExistingProductType.

@Test
void sync_withProductTypeReferenceAsAttribute_shouldCreateProductReferencingExistingProductType() {
    // preparation
    final AttributeDraft productTypeReferenceAttribute = AttributeDraft.of("productType-reference", Reference.of(ProductType.referenceTypeId(), productType.getKey()));
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(productTypeReferenceAttribute).build();
    final ProductDraft productDraftWithProductTypeReference = 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(productDraftWithProductTypeReference)).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(productDraftWithProductTypeReference.getKey())).toCompletableFuture().join();
    final Optional<Attribute> createdProductReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(productTypeReferenceAttribute.getName());
    assertThat(createdProductReferenceAttribute).hasValueSatisfying(attribute -> {
        assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(ProductType.referenceTypeId());
        assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(productType.getId());
    });
}
Also used : ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductDraft(io.sphere.sdk.products.ProductDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) Attribute(io.sphere.sdk.products.attributes.Attribute) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) Product(io.sphere.sdk.products.Product) 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