use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedProductsInAnyOrderIT method sync_withMultipleHierarchyProductReferenceAsAttribute_shouldCreateProductReferencingExistingProduct.
@Test
void sync_withMultipleHierarchyProductReferenceAsAttribute_shouldCreateProductReferencingExistingProduct() {
// preparation
final String parentKey = "product-parent";
final String parentKey1 = "product-parent-1";
final String parentKey2 = "product-parent-2";
final String parentKey3 = "product-parent-3";
final String parentKey4 = "product-parent-4";
final String productAttributeReferenceName = "product-reference";
final String productAttributeReferenceSetName = "product-reference-set";
final AttributeDraft productReferenceAttribute = AttributeDraft.of(productAttributeReferenceName, Reference.of(Product.referenceTypeId(), parentKey));
final ArrayNode refSet = JsonNodeFactory.instance.arrayNode();
final ObjectNode parentKey1RefObject = createReferenceObject(parentKey1, Product.referenceTypeId());
final ObjectNode parentKey2RefObject = createReferenceObject(parentKey2, Product.referenceTypeId());
final ObjectNode parentKey3RefObject = createReferenceObject(parentKey3, Product.referenceTypeId());
final ObjectNode parentKey4RefObject = createReferenceObject(parentKey4, Product.referenceTypeId());
refSet.add(parentKey1RefObject);
refSet.add(parentKey2RefObject);
refSet.add(parentKey3RefObject);
refSet.add(parentKey4RefObject);
final AttributeDraft productReferenceSetAttribute = AttributeDraft.of(productAttributeReferenceSetName, refSet);
final ProductDraft childDraft = ProductDraftBuilder.of(productType, ofEnglish("foo"), ofEnglish("foo-slug"), emptyList()).key("foo").masterVariant(ProductVariantDraftBuilder.of().key("foo").sku("foo").attributes(productReferenceAttribute, productReferenceSetAttribute).build()).build();
final ProductDraft childDraft1 = ProductDraftBuilder.of(productType, ofEnglish("childDraft1"), ofEnglish("childDraft1"), ProductVariantDraftBuilder.of().key("childDraft1").sku("childDraft1").attributes(productReferenceAttribute).build()).key("childDraft1").build();
final ProductDraft parentDraft = ProductDraftBuilder.of(productType, ofEnglish(parentKey), ofEnglish(parentKey), ProductVariantDraftBuilder.of().sku(parentKey).key(parentKey).build()).key(parentKey).build();
final ProductDraft parentDraft1 = ProductDraftBuilder.of(productType, ofEnglish(parentKey1), ofEnglish(parentKey1), ProductVariantDraftBuilder.of().key(parentKey1).sku(parentKey1).build()).key(parentKey1).build();
final ProductDraft parentDraft2 = ProductDraftBuilder.of(productType, ofEnglish(parentKey2), ofEnglish(parentKey2), ProductVariantDraftBuilder.of().key(parentKey2).sku(parentKey2).attributes(AttributeDraft.of(productAttributeReferenceName, parentKey1RefObject)).build()).key(parentKey2).build();
final ProductDraft parentDraft3 = ProductDraftBuilder.of(productType, ofEnglish(parentKey3), ofEnglish(parentKey3), ProductVariantDraftBuilder.of().key(parentKey3).sku(parentKey3).attributes(AttributeDraft.of(productAttributeReferenceName, parentKey2RefObject)).build()).key(parentKey3).build();
final ProductDraft parentDraft4 = ProductDraftBuilder.of(productType, ofEnglish(parentKey4), ofEnglish(parentKey4), ProductVariantDraftBuilder.of().key(parentKey4).sku(parentKey4).attributes(AttributeDraft.of(productAttributeReferenceName, parentKey3RefObject)).build()).key(parentKey4).build();
// test
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = productSync.sync(asList(childDraft, parentDraft, parentDraft4, childDraft1, parentDraft3, parentDraft2, parentDraft1)).toCompletableFuture().join();
// assertion
final Product syncedChild1 = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(childDraft1.getKey())).toCompletableFuture().join();
final Product syncedParent = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(parentDraft.getKey())).toCompletableFuture().join();
final Product syncedParent1 = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(parentDraft1.getKey())).toCompletableFuture().join();
final Product syncedParent2 = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(parentDraft2.getKey())).toCompletableFuture().join();
final Product syncedParent3 = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(parentDraft3.getKey())).toCompletableFuture().join();
final Product syncedParent4 = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(parentDraft4.getKey())).toCompletableFuture().join();
final ArrayNode expectedAttributeValue = JsonNodeFactory.instance.arrayNode();
expectedAttributeValue.add(createReferenceObject(syncedParent1.getId(), Product.referenceTypeId()));
expectedAttributeValue.add(createReferenceObject(syncedParent2.getId(), Product.referenceTypeId()));
expectedAttributeValue.add(createReferenceObject(syncedParent3.getId(), Product.referenceTypeId()));
expectedAttributeValue.add(createReferenceObject(syncedParent4.getId(), Product.referenceTypeId()));
assertThat(syncStatistics).hasValues(7, 6, 1, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
final Product existingProduct = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(childDraft.getKey())).toCompletableFuture().join();
final Optional<Attribute> existingProductReferenceSetAttribute = existingProduct.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceSetAttribute.getName());
assertThat(existingProductReferenceSetAttribute).hasValueSatisfying(attribute -> {
assertThat(attribute.getValueAsJsonNode().get(0).get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(0).get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent1.getId());
assertThat(attribute.getValueAsJsonNode().get(1).get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(1).get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent2.getId());
assertThat(attribute.getValueAsJsonNode().get(2).get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(2).get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent3.getId());
assertThat(attribute.getValueAsJsonNode().get(3).get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(3).get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent4.getId());
});
final Optional<Attribute> existingProductReferenceAttribute = existingProduct.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceAttribute.getName());
assertThat(existingProductReferenceAttribute).hasValueSatisfying(attribute -> {
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent.getId());
});
final Optional<Attribute> syncedProduct4Attribute = syncedParent4.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceAttribute.getName());
assertThat(syncedProduct4Attribute).hasValueSatisfying(attribute -> {
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent3.getId());
});
final Optional<Attribute> syncedProduct3Attribute = syncedParent3.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceAttribute.getName());
assertThat(syncedProduct3Attribute).hasValueSatisfying(attribute -> {
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent2.getId());
});
final Optional<Attribute> syncedProduct2Attribute = syncedParent2.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceAttribute.getName());
assertThat(syncedProduct2Attribute).hasValueSatisfying(attribute -> {
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent1.getId());
});
final Optional<Attribute> syncedChild1Attribute = syncedChild1.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceAttribute.getName());
assertThat(syncedChild1Attribute).hasValueSatisfying(attribute -> {
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent.getId());
});
assertNoWaitingDrafts(CTP_TARGET_CLIENT);
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedProductsInAnyOrderIT method sync_withMultipleChildDraftsSuppliedBeforeParent_shouldSyncCorrectly.
@Test
void sync_withMultipleChildDraftsSuppliedBeforeParent_shouldSyncCorrectly() {
// preparation
final String productReferenceAttributeName = "product-reference";
final String parentProductKey = "parent-product-key";
final AttributeDraft productReferenceAttribute = AttributeDraft.of(productReferenceAttributeName, Reference.of(Product.referenceTypeId(), parentProductKey));
final ProductDraft childDraft1 = ProductDraftBuilder.of(productType, ofEnglish("foo"), ofEnglish("foo-slug"), ProductVariantDraftBuilder.of().key("foo").sku("foo").attributes(productReferenceAttribute).build()).key(product.getKey()).build();
final ProductDraft childDraft2 = ProductDraftBuilder.of(productType, ofEnglish("foo-2"), ofEnglish("foo-slug-2"), ProductVariantDraftBuilder.of().key("foo-2").sku("foo-2").attributes(productReferenceAttribute).build()).key("foo-2").build();
final ProductDraft parentDraft = ProductDraftBuilder.of(productType, ofEnglish("bar"), ofEnglish("bar-slug"), ProductVariantDraftBuilder.of().sku("bar").key("bar").build()).key(parentProductKey).build();
// test
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = productSync.sync(asList(childDraft1, childDraft2, parentDraft)).toCompletableFuture().join();
final Product syncedParent = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(parentDraft.getKey())).toCompletableFuture().join();
// assertion
assertThat(syncedParent).isNotNull();
assertThat(syncedParent.getKey()).isEqualTo(parentProductKey);
assertThat(syncStatistics).hasValues(3, 2, 1, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).containsExactly(SetAttributeInAllVariants.of(productReferenceAttributeName, createReferenceObject(syncedParent.getId(), Product.referenceTypeId()), true));
final Product syncedChild1 = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(childDraft1.getKey())).toCompletableFuture().join();
final Product syncedChild2 = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(childDraft2.getKey())).toCompletableFuture().join();
final Optional<Attribute> syncedProductReferenceAttribute1 = syncedChild1.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceAttribute.getName());
final Optional<Attribute> createdProductReferenceAttribute2 = syncedChild2.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceAttribute.getName());
assertThat(syncedProductReferenceAttribute1).hasValueSatisfying(attribute -> {
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent.getId());
});
assertThat(createdProductReferenceAttribute2).hasValueSatisfying(attribute -> {
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent.getId());
});
assertNoWaitingDrafts(CTP_TARGET_CLIENT);
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedProductTypesIT method sync_withSameNestedProductTypeReferenceAsAttribute_shouldNotSyncAnythingNew.
@Test
void sync_withSameNestedProductTypeReferenceAsAttribute_shouldNotSyncAnythingNew() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueReferences("productType-reference", createReferenceObject(testProductType1.getId(), ProductType.referenceTypeId()));
final AttributeDraft productTypeReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(nestedAttributeValue));
final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(productTypeReferenceAttribute).build();
final ProductDraft productDraftWithProductTypeReference = ProductDraftBuilder.of(testProductType1, ofEnglish("productName"), ofEnglish("productSlug"), masterVariant).key("new-product").build();
CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraftWithProductTypeReference)).toCompletableFuture().join();
final ObjectNode newNestedAttributeValue = createNestedAttributeValueReferences("productType-reference", createReferenceObject(testProductType1.getKey(), ProductType.referenceTypeId()));
final AttributeDraft newProductReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(newNestedAttributeValue));
final ProductVariantDraft newMasterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(newProductReferenceAttribute).build();
final ProductDraft newProductDraftWithProductReference = ProductDraftBuilder.of(testProductType1, 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> createdProductTypeReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(productTypeReferenceAttribute.getName());
assertThat(createdProductTypeReferenceAttribute).hasValueSatisfying(attribute -> {
final JsonNode nestedAttributeNameField = attribute.getValueAsJsonNode().get(0).get(ATTRIBUTE_NAME_FIELD);
final JsonNode nestedAttributeValueField = attribute.getValueAsJsonNode().get(0).get(ATTRIBUTE_VALUE_FIELD);
assertThat(nestedAttributeNameField.asText()).isEqualTo("productType-reference");
assertThat(nestedAttributeValueField.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(ProductType.referenceTypeId());
assertThat(nestedAttributeValueField.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testProductType1.getId());
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedProductTypesIT method sync_withNestedProductTypeReferenceAsAttribute_shouldCreateProductReferencingExistingProductType.
@Test
void sync_withNestedProductTypeReferenceAsAttribute_shouldCreateProductReferencingExistingProductType() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueReferences("productType-reference", createReferenceObject(testProductType1.getKey(), ProductType.referenceTypeId()));
final AttributeDraft productTypeReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(nestedAttributeValue));
final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(productTypeReferenceAttribute).build();
final ProductDraft productDraftWithProductTypeReference = ProductDraftBuilder.of(testProductType1, 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> createdProductTypeReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(productTypeReferenceAttribute.getName());
assertThat(createdProductTypeReferenceAttribute).hasValueSatisfying(attribute -> {
final JsonNode nestedAttributeNameField = attribute.getValueAsJsonNode().get(0).get(ATTRIBUTE_NAME_FIELD);
final JsonNode nestedAttributeReferenceValueField = attribute.getValueAsJsonNode().get(0).get(ATTRIBUTE_VALUE_FIELD);
assertThat(nestedAttributeNameField.asText()).isEqualTo("productType-reference");
assertThat(nestedAttributeReferenceValueField.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(ProductType.referenceTypeId());
assertThat(nestedAttributeReferenceValueField.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testProductType1.getId());
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedProductTypesIT method sync_withSetOfNestedProductReferenceSetAsAttribute_shouldCreateProductReferencingExistingProductType.
@Test
void sync_withSetOfNestedProductReferenceSetAsAttribute_shouldCreateProductReferencingExistingProductType() {
// preparation
final ArrayNode nestedAttributeValue = createArrayNode(createNestedAttributeValueSetOfReferences("productType-reference-set", createReferenceObject(testProductType1.getKey(), ProductType.referenceTypeId()), createReferenceObject(testProductType2.getKey(), ProductType.referenceTypeId())));
final AttributeDraft productTypeReferenceAttribute = AttributeDraft.of("setOfNestedAttribute", createArrayNode(nestedAttributeValue));
final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(productTypeReferenceAttribute).build();
final ProductDraft productDraftWithProductTypeReference = ProductDraftBuilder.of(testProductType1, 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> createdProductTypeReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(productTypeReferenceAttribute.getName());
assertThat(createdProductTypeReferenceAttribute).hasValueSatisfying(attribute -> {
final JsonNode setOfNestedAttributeNameField = attribute.getValueAsJsonNode().get(0).get(0).get(ATTRIBUTE_NAME_FIELD);
final JsonNode setOfNestedAttributeValueField = attribute.getValueAsJsonNode().get(0).get(0).get(ATTRIBUTE_VALUE_FIELD);
assertThat(setOfNestedAttributeNameField.asText()).isEqualTo("productType-reference-set");
assertThat(setOfNestedAttributeValueField).isInstanceOf(ArrayNode.class);
final ArrayNode referenceSet = (ArrayNode) setOfNestedAttributeValueField;
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(testProductType1.getId());
}).anySatisfy(reference -> {
assertThat(reference.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(ProductType.referenceTypeId());
assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testProductType2.getId());
});
});
}
Aggregations