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());
});
}
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());
});
});
}
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();
}
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());
});
}
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());
});
}
Aggregations