use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedCategoriesIT method sync_withSetOfNestedProductReferenceSetAsAttribute_shouldCreateProductReferencingExistingCategories.
@Test
void sync_withSetOfNestedProductReferenceSetAsAttribute_shouldCreateProductReferencingExistingCategories() {
// preparation
final ArrayNode nestedAttributeValue = createArrayNode(createNestedAttributeValueSetOfReferences("category-reference-set", createReferenceObject(testCategory1.getKey(), Category.referenceTypeId()), createReferenceObject(testCategory2.getKey(), Category.referenceTypeId())));
final AttributeDraft categoryReferenceAttribute = AttributeDraft.of("setOfNestedAttribute", createArrayNode(nestedAttributeValue));
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, 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(productDraftWithCategoryReference.getKey())).toCompletableFuture().join();
final Optional<Attribute> createdCategoryReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(categoryReferenceAttribute.getName());
assertThat(createdCategoryReferenceAttribute).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("category-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(Category.referenceTypeId());
assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testCategory1.getId());
}).anySatisfy(reference -> {
assertThat(reference.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Category.referenceTypeId());
assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testCategory2.getId());
});
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedCategoriesIT method sync_withNestedCategoryReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct.
@Test
void sync_withNestedCategoryReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueSetOfReferences("category-reference-set", createReferenceObject(testCategory1.getKey(), Category.referenceTypeId()), createReferenceObject("nonExistingKey", Category.referenceTypeId()));
final AttributeDraft categoryReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(nestedAttributeValue));
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 'nestedAttribute.category-reference-set'");
return true;
});
assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains("The value '{\"typeId\":\"category\",\"id\":\"nonExistingKey\"}' " + "is not valid for field 'nestedAttribute.category-reference-set'");
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedCustomObjectsIT method sync_withSameNestedCustomObjectReferenceAsAttribute_shouldNotSyncAnythingNew.
@Test
void sync_withSameNestedCustomObjectReferenceAsAttribute_shouldNotSyncAnythingNew() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueReferences(CUSTOM_OBJECT_REFERENCE_ATTR_NAME, createReferenceObject(testCustomObject1.getId(), CustomObject.referenceTypeId()));
final AttributeDraft customObjectReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(nestedAttributeValue));
final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(customObjectReferenceAttribute).build();
final ProductDraft productDraftWithCustomObjectReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), masterVariant).key("new-product").build();
CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraftWithCustomObjectReference)).toCompletableFuture().join();
final ObjectNode newNestedAttributeValue = createNestedAttributeValueReferences(CUSTOM_OBJECT_REFERENCE_ATTR_NAME, createReferenceObject(format("%s|%s", testCustomObject1.getContainer(), testCustomObject1.getKey()), CustomObject.referenceTypeId()));
final AttributeDraft newCustomObjectReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(newNestedAttributeValue));
final ProductVariantDraft newMasterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(newCustomObjectReferenceAttribute).build();
final ProductDraft newProductDraftWithCustomObjectReference = 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(newProductDraftWithCustomObjectReference)).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(productDraftWithCustomObjectReference.getKey())).toCompletableFuture().join();
final Optional<Attribute> createdCustomObjectReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(customObjectReferenceAttribute.getName());
assertThat(createdCustomObjectReferenceAttribute).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(CUSTOM_OBJECT_REFERENCE_ATTR_NAME);
assertThat(nestedAttributeValueField.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(CustomObject.referenceTypeId());
assertThat(nestedAttributeValueField.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testCustomObject1.getId());
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedCustomObjectsIT method sync_withNestedCustomObjectReferenceAsAttribute_shouldCreateProductReferencingExistingCustomObject.
@Test
void sync_withNestedCustomObjectReferenceAsAttribute_shouldCreateProductReferencingExistingCustomObject() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueReferences(CUSTOM_OBJECT_REFERENCE_ATTR_NAME, createReferenceObject(format("%s|%s", testCustomObject1.getContainer(), testCustomObject1.getKey()), CustomObject.referenceTypeId()));
final AttributeDraft customObjectReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(nestedAttributeValue));
final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(customObjectReferenceAttribute).build();
final ProductDraft productDraftWithCustomObjectReference = 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(productDraftWithCustomObjectReference)).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(productDraftWithCustomObjectReference.getKey())).toCompletableFuture().join();
final Optional<Attribute> createdCustomObjectReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(customObjectReferenceAttribute.getName());
assertThat(createdCustomObjectReferenceAttribute).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(CUSTOM_OBJECT_REFERENCE_ATTR_NAME);
assertThat(nestedAttributeReferenceValueField.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(CustomObject.referenceTypeId());
assertThat(nestedAttributeReferenceValueField.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testCustomObject1.getId());
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedCustomObjectsIT method sync_withNonExistingNestedCustomObjectReferenceAsAttribute_ShouldFailCreatingTheProduct.
@Test
void sync_withNonExistingNestedCustomObjectReferenceAsAttribute_ShouldFailCreatingTheProduct() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueReferences(CUSTOM_OBJECT_REFERENCE_ATTR_NAME, createReferenceObject("non-existing-container|non-existing-key", CustomObject.referenceTypeId()));
final AttributeDraft customObjectReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(nestedAttributeValue));
final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(customObjectReferenceAttribute).build();
final ProductDraft productDraftWithCustomObjectReference = 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(productDraftWithCustomObjectReference)).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\":\"key-value-document\"," + "\"id\":\"non-existing-container|non-existing-key\"}' " + "is not valid for field 'nestedAttribute.customObject-reference'");
return true;
});
assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains("The value '{\"typeId\":\"key-value-document\"," + "\"id\":\"non-existing-container|non-existing-key\"}' " + "is not valid for field 'nestedAttribute.customObject-reference'");
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
}
Aggregations