use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedProductsIT method sync_withSameNestedProductReferenceAsAttribute_shouldNotSyncAnythingNew.
@Test
void sync_withSameNestedProductReferenceAsAttribute_shouldNotSyncAnythingNew() {
// 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(testProduct1.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, 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 -> {
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(testProduct1.getId());
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedProductsIT method sync_withNestedProductReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct.
@Test
void sync_withNestedProductReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueSetOfReferences("product-reference-set", createReferenceObject(testProduct1.getKey(), Product.referenceTypeId()), createReferenceObject("nonExistingKey", 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();
// test
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(productDraftWithProductReference)).toCompletableFuture().join();
// assertion
assertThat(syncStatistics).hasValues(1, 0, 0, 0, 1);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
final UnresolvedReferencesServiceImpl<WaitingToBeResolvedProducts> unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(syncOptions);
final Set<WaitingToBeResolvedProducts> waitingToBeResolvedDrafts = unresolvedReferencesService.fetch(asSet(productDraftWithProductReference.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
assertThat(waitingToBeResolvedDrafts).singleElement().matches(waitingToBeResolvedDraft -> {
assertThat(waitingToBeResolvedDraft.getProductDraft().getKey()).isEqualTo(productDraftWithProductReference.getKey());
assertThat(waitingToBeResolvedDraft.getMissingReferencedProductKeys()).containsExactly("nonExistingKey");
return true;
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedProductsIT method sync_withSetOfNestedProductReferenceSetAsAttribute_shouldCreateProductReferencingExistingProducts.
@Test
void sync_withSetOfNestedProductReferenceSetAsAttribute_shouldCreateProductReferencingExistingProducts() {
// preparation
final ArrayNode nestedAttributeValue = createArrayNode(createNestedAttributeValueSetOfReferences("product-reference-set", createReferenceObject(testProduct1.getKey(), Product.referenceTypeId()), createReferenceObject(testProduct2.getKey(), Product.referenceTypeId())));
final AttributeDraft productReferenceAttribute = AttributeDraft.of("setOfNestedAttribute", 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();
// 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 -> {
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("product-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(Product.referenceTypeId());
assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testProduct1.getId());
}).anySatisfy(reference -> {
assertThat(reference.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testProduct2.getId());
});
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedCategoriesIT method sync_withNonExistingCategoryReferenceAsAttribute_ShouldFailCreatingTheProduct.
@Test
void sync_withNonExistingCategoryReferenceAsAttribute_ShouldFailCreatingTheProduct() {
// preparation
final AttributeDraft categoryReferenceAttribute = AttributeDraft.of("category-reference", Reference.of(Category.referenceTypeId(), "nonExistingKey"));
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();
// 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'");
return true;
});
assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains("The value '{\"typeId\":\"category\",\"id\":\"nonExistingKey\"}' " + "is not valid for field 'category-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_withProductTypeReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct.
@Test
void sync_withProductTypeReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct() {
// 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(), "nonExistingKey"));
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, 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-set'");
return true;
});
assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains("The value '{\"typeId\":\"product-type\",\"id\":\"nonExistingKey\"}' " + "is not valid for field 'productType-reference-set'");
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
}
Aggregations