use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedProductsIT method sync_withChangedNestedProductReferenceAsAttribute_shouldUpdateProductReferencingExistingProduct.
@Test
void sync_withChangedNestedProductReferenceAsAttribute_shouldUpdateProductReferencingExistingProduct() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueReferences("product-reference", createReferenceObject(testProduct1.getId(), Product.referenceTypeId()));
final AttributeDraft productReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(nestedAttributeValue));
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();
CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraftWithProductReference)).toCompletableFuture().join();
final ObjectNode newNestedAttributeValue = createNestedAttributeValueReferences("product-reference", createReferenceObject(testProduct2.getKey(), Product.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(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, 1, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
final ObjectNode expectedNestedAttributeValue = createNestedAttributeValueReferences("product-reference", createReferenceObject(testProduct2.getId(), Product.referenceTypeId()));
final AttributeDraft expectedProductReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(expectedNestedAttributeValue));
assertThat(actions).containsExactly(SetAttribute.of(1, expectedProductReferenceAttribute, true));
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 -> {
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("product-reference");
assertThat(nestedAttributeValueField.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(nestedAttributeValueField.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testProduct2.getId());
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedProductsIT method sync_withNestedAttributeWithTextAttribute_shouldCreateProduct.
@Test
void sync_withNestedAttributeWithTextAttribute_shouldCreateProduct() {
// preparation
final ArrayNode nestedAttributeValue = JsonNodeFactory.instance.arrayNode();
final ObjectNode nestedProductTypeAttribute = JsonNodeFactory.instance.objectNode();
nestedAttributeValue.add(nestedProductTypeAttribute);
nestedProductTypeAttribute.put(ATTRIBUTE_NAME_FIELD, "text-attr");
nestedProductTypeAttribute.put(ATTRIBUTE_VALUE_FIELD, "text-attr-value");
final AttributeDraft productReferenceAttribute = AttributeDraft.of("nestedAttribute", nestedAttributeValue);
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(0).get(ATTRIBUTE_VALUE_FIELD).asText()).isEqualTo("text-attr-value");
assertThat(attribute.getValueAsJsonNode().get(0).get(ATTRIBUTE_NAME_FIELD).asText()).isEqualTo("text-attr");
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withEmptySetAttribute_ShouldCreateProductWithAnEmptySetAttribute.
@Test
void sync_withEmptySetAttribute_ShouldCreateProductWithAnEmptySetAttribute() {
// Preparation
// Create custom options with whitelisting and action filter callback..
final ProductSyncOptions customSyncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, newResource, oldResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).warningCallback(((exception, newResource, oldResource) -> warningCallBackMessages.add(exception.getMessage()))).beforeUpdateCallback(this::beforeUpdateCallback).syncFilter(SyncFilter.ofWhiteList(ATTRIBUTES)).build();
final ProductSync customSync = new ProductSync(customSyncOptions);
// Create a product that will be referenced by another product in the target project
final ProductDraft productDraftToBeReferenced = ProductDraftBuilder.of(targetProductType.toReference(), ofEnglish("root"), ofEnglish("root"), emptyList()).build();
final Product productToBeReferenced = CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraftToBeReferenced)).toCompletableFuture().join();
// Create a product "bar" that references a product on the target project
final ObjectNode productReferenceValue1 = getProductReferenceWithId(productToBeReferenced.getId());
final AttributeDraft productSetRefAttr = getReferenceSetAttributeDraft("product-reference-set", productReferenceValue1);
final ProductVariantDraft variantWithProductReferences = ProductVariantDraftBuilder.of().key("bar").sku("bar").attributes(singletonList(productSetRefAttr)).build();
final ProductDraft existingProductDraft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, targetProductType.toReference()).masterVariant(variantWithProductReferences).taxCategory(targetTaxCategory.toReference()).state(targetProductState.toReference()).categories(Collections.emptySet()).categoryOrderHints(null).build();
CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(existingProductDraft)).toCompletableFuture().join();
// Create a product "bar" that has an empty references set on the source project (this is
// expected to update)
final ProductVariantDraft variantBarWithEmptyReferenceSet = ProductVariantDraftBuilder.of().key("bar").sku("bar").attributes(singletonList(getReferenceSetAttributeDraft(productSetRefAttr.getName()))).build();
final ProductDraft newProductDraftWithProductReference = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, sourceProductType.toReference()).masterVariant(variantBarWithEmptyReferenceSet).taxCategory(sourceTaxCategory.toReference()).state(sourceProductState.toReference()).categories(Collections.emptySet()).categoryOrderHints(null).build();
CTP_SOURCE_CLIENT.execute(ProductCreateCommand.of(newProductDraftWithProductReference)).toCompletableFuture().join();
// Create a product "foo" that has an empty references set on the source project (this is
// expected to create)
final ProductVariantDraft variantFooWithEmptyReferenceSet = ProductVariantDraftBuilder.of().key("foo").sku("foo").attributes(singletonList(getReferenceSetAttributeDraft(productSetRefAttr.getName()))).build();
final ProductDraft sourceProductDraft = createProductDraftBuilder(PRODUCT_KEY_1_CHANGED_RESOURCE_PATH, sourceProductType.toReference()).taxCategory(sourceTaxCategory).state(sourceProductState).categories(sourceCategoryReferencesWithIds).categoryOrderHints(createRandomCategoryOrderHints(sourceCategoryReferencesWithIds)).masterVariant(variantFooWithEmptyReferenceSet).build();
CTP_SOURCE_CLIENT.execute(ProductCreateCommand.of(sourceProductDraft)).toCompletableFuture().join();
// Test
final List<ProductProjection> products = CTP_SOURCE_CLIENT.execute(ProductProjectionQuery.ofStaged()).toCompletableFuture().join().getResults();
final List<ProductDraft> productDrafts = ProductTransformUtils.toProductDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, products).join();
final ProductSyncStatistics syncStatistics = customSync.sync(productDrafts).toCompletableFuture().join();
// Assertion
assertThat(syncStatistics).hasValues(2, 1, 1, 0);
assertThat(errorCallBackMessages).isEmpty();
assertThat(errorCallBackExceptions).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
assertThat(updateActions).containsExactly(SetAttributeInAllVariants.of(productSetRefAttr.getName(), JsonNodeFactory.instance.arrayNode(), true), Publish.of());
final Product targetProduct = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(sourceProductDraft.getKey())).toCompletableFuture().join();
final Attribute targetAttribute = targetProduct.getMasterData().getStaged().getMasterVariant().getAttribute(productSetRefAttr.getName());
assertThat(targetAttribute).isNotNull();
assertThat(targetAttribute.getValueAsJsonNode()).isEmpty();
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method setupTest.
/**
* Deletes Products from the source and target CTP projects, clears the callback collections then
* it instantiates a new {@link ProductSync} instance.
*/
@BeforeEach
void setupTest() {
clearSyncTestCollections();
deleteAllProducts(CTP_TARGET_CLIENT);
deleteAllProducts(CTP_SOURCE_CLIENT);
final ProductSyncOptions syncOptions = buildSyncOptions();
productSync = new ProductSync(syncOptions);
referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withChangedAttributes_ShouldUpdateProducts.
@Test
void sync_withChangedAttributes_ShouldUpdateProducts() {
// Preparation
// Create custom options with whitelisting and action filter callback..
final ProductSyncOptions customSyncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> errorCallback(exception.getMessage(), exception.getCause())).warningCallback((exception, oldResource, newResources) -> warningCallBackMessages.add(exception.getMessage())).beforeUpdateCallback(this::beforeUpdateCallback).syncFilter(SyncFilter.ofWhiteList(ATTRIBUTES)).build();
final ProductSync customSync = new ProductSync(customSyncOptions);
// Create existing products in target project with keys (productKey1)
final ProductDraft existingProductDraft = createProductDraft(PRODUCT_KEY_1_RESOURCE_PATH, targetProductType.toReference(), targetTaxCategory.toReference(), targetProductState.toReference(), targetCategoryReferencesWithIds, createRandomCategoryOrderHints(targetCategoryReferencesWithIds));
CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(existingProductDraft)).toCompletableFuture().join();
// Create existing product with productKey1 in source project with changed attributes
final ProductDraft newProductDraftWithProductReference = createProductDraftBuilder(PRODUCT_KEY_1_CHANGED_ATTRIBUTES_RESOURCE_PATH, sourceProductType.toReference()).taxCategory(sourceTaxCategory.toReference()).state(sourceProductState.toReference()).categories(Collections.emptySet()).categoryOrderHints(null).build();
CTP_SOURCE_CLIENT.execute(ProductCreateCommand.of(newProductDraftWithProductReference)).toCompletableFuture().join();
// Test
final List<ProductProjection> products = CTP_SOURCE_CLIENT.execute(ProductProjectionQuery.ofStaged()).toCompletableFuture().join().getResults();
final List<ProductDraft> productDrafts = ProductTransformUtils.toProductDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, products).join();
final ProductSyncStatistics syncStatistics = customSync.sync(productDrafts).toCompletableFuture().join();
// Assertion
assertThat(syncStatistics).hasValues(1, 0, 1, 0);
assertThat(errorCallBackMessages).isEmpty();
assertThat(errorCallBackExceptions).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
final AttributeDraft priceInfoAttrDraft = AttributeDraft.of("priceInfo", JsonNodeFactory.instance.textNode("100/kg"));
final AttributeDraft angebotAttrDraft = AttributeDraft.of("angebot", JsonNodeFactory.instance.textNode("big discount"));
assertThat(updateActions).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());
}
Aggregations