Search in sources :

Example 51 with ProductSync

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

the class ProductSyncWithReferencedProductTypesIT method sync_withChangedProductTypeReferenceAsAttribute_shouldUpdateProductReferencingExistingProductType.

@Test
void sync_withChangedProductTypeReferenceAsAttribute_shouldUpdateProductReferencingExistingProductType() {
    // preparation
    final AttributeDraft productTypeReferenceAttribute = AttributeDraft.of("productType-reference", Reference.of(ProductType.referenceTypeId(), productType));
    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();
    CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraftWithProductTypeReference)).toCompletableFuture().join();
    final AttributeDraft newProductTypeReferenceAttribute = AttributeDraft.of("productType-reference", Reference.of(ProductType.referenceTypeId(), productType2.getKey()));
    final ProductVariantDraft newMasterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(newProductTypeReferenceAttribute).build();
    final ProductDraft newProductDraftWithProductTypeReference = 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(newProductDraftWithProductTypeReference)).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("productType-reference", Reference.of(ProductType.referenceTypeId(), productType2.getId()));
    assertThat(actions).containsExactly(SetAttributeInAllVariants.of(expectedAttribute, true));
    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(productType2.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 52 with ProductSync

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

the class ProductSyncWithReferencedProductTypesIT method sync_withProductTypeReferenceSetAsAttribute_shouldCreateProductReferencingExistingCategories.

@Test
void sync_withProductTypeReferenceSetAsAttribute_shouldCreateProductReferencingExistingCategories() {
    // preparation
    final AttributeDraft productTypeReferenceAttribute = AttributeDraft.of("productType-reference", Reference.of(ProductType.referenceTypeId(), productType.getKey()));
    final HashSet<Reference<ProductType>> references = new HashSet<>();
    references.add(Reference.of(ProductType.referenceTypeId(), productType.getKey()));
    references.add(Reference.of(ProductType.referenceTypeId(), productType2.getKey()));
    final AttributeDraft productTypeReferenceSetAttribute = AttributeDraft.of("productType-reference-set", references);
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(productTypeReferenceAttribute, productTypeReferenceSetAttribute).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());
    });
    final Optional<Attribute> createdProductTypeReferenceSetAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(productTypeReferenceSetAttribute.getName());
    assertThat(createdProductTypeReferenceSetAttribute).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(ProductType.referenceTypeId());
            assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(productType.getId());
        }).anySatisfy(reference -> {
            assertThat(reference.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(ProductType.referenceTypeId());
            assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(productType2.getId());
        });
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Reference(io.sphere.sdk.models.Reference) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) ProductByKeyGet(io.sphere.sdk.products.queries.ProductByKeyGet) 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) 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) UpdateAction(io.sphere.sdk.commands.UpdateAction) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) Attribute(io.sphere.sdk.products.attributes.Attribute) BeforeAll(org.junit.jupiter.api.BeforeAll) Assertions.as(org.assertj.core.api.Assertions.as) ProductSync(com.commercetools.sync.products.ProductSync) TriConsumer(com.commercetools.sync.commons.utils.TriConsumer) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) PRODUCT_TYPE_WITH_REFERENCES_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_WITH_REFERENCES_RESOURCE_PATH) CategoryITUtils.deleteAllCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories) REFERENCE_ID_FIELD(com.commercetools.sync.commons.utils.ResourceIdentifierUtils.REFERENCE_ID_FIELD) SetAttributeInAllVariants(io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants) 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) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) 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) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) 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) 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 53 with ProductSync

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

the class ProductSyncWithReferencedProductTypesIT method sync_withNonExistingProductTypeReferenceAsAttribute_ShouldFailCreatingTheProduct.

@Test
void sync_withNonExistingProductTypeReferenceAsAttribute_ShouldFailCreatingTheProduct() {
    // preparation
    final AttributeDraft productTypeReferenceAttribute = AttributeDraft.of("productType-reference", Reference.of(ProductType.referenceTypeId(), "nonExistingKey"));
    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, 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\":\"product-type\",\"id\":\"nonExistingKey\"}' " + "is not valid for field 'productType-reference'");
        return true;
    });
    assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains("The value '{\"typeId\":\"product-type\",\"id\":\"nonExistingKey\"}' " + "is not valid for field 'productType-reference'");
    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) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) ProductSync(com.commercetools.sync.products.ProductSync) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) Test(org.junit.jupiter.api.Test)

Example 54 with ProductSync

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

the class ProductSyncWithReferencedProductTypesIT method sync_withSameProductTypeReferenceAsAttribute_shouldNotSyncAnythingNew.

@Test
void sync_withSameProductTypeReferenceAsAttribute_shouldNotSyncAnythingNew() {
    // preparation
    final AttributeDraft productTypeReferenceAttribute = AttributeDraft.of("productType-reference", Reference.of(ProductType.referenceTypeId(), productType));
    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();
    CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraftWithProductTypeReference)).toCompletableFuture().join();
    final AttributeDraft newProductReferenceAttribute = AttributeDraft.of("productType-reference", Reference.of(ProductType.referenceTypeId(), productType.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(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)

Example 55 with ProductSync

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

the class ProductSyncWithReferencedProductsIT method sync_withProductReferenceAsAttribute_shouldCreateProductReferencingExistingProduct.

@Test
void sync_withProductReferenceAsAttribute_shouldCreateProductReferencingExistingProduct() {
    // preparation
    final AttributeDraft productReferenceAttribute = AttributeDraft.of("product-reference", Reference.of(Product.referenceTypeId(), product.getKey()));
    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, 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());
    });
}
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