use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedCustomObjectsIT method sync_withChangedNestedCustomObjectReferenceAsAttribute_shouldUpdateProductReferencingExistingCustomObject.
@Test
void sync_withChangedNestedCustomObjectReferenceAsAttribute_shouldUpdateProductReferencingExistingCustomObject() {
// 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", testCustomObject2.getContainer(), testCustomObject2.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, 1, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
final ObjectNode expectedNestedAttributeValue = createNestedAttributeValueReferences(CUSTOM_OBJECT_REFERENCE_ATTR_NAME, createReferenceObject(testCustomObject2.getId(), CustomObject.referenceTypeId()));
final AttributeDraft expectedCustomObjectReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(expectedNestedAttributeValue));
assertThat(actions).containsExactly(SetAttribute.of(1, expectedCustomObjectReferenceAttribute, true));
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(testCustomObject2.getId());
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedCustomObjectsIT method sync_withNestedCustomObjectReferenceSetAsAttribute_shouldCreateProductReferencingExistingCustomObjects.
@Test
void sync_withNestedCustomObjectReferenceSetAsAttribute_shouldCreateProductReferencingExistingCustomObjects() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueSetOfReferences(CUSTOM_OBJECT_REFERENCE_SET_ATTR_NAME, createReferenceObject(format("%s|%s", testCustomObject1.getContainer(), testCustomObject1.getKey()), CustomObject.referenceTypeId()), createReferenceObject(format("%s|%s", testCustomObject2.getContainer(), testCustomObject2.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 nestedAttributeValueField = attribute.getValueAsJsonNode().get(0).get(ATTRIBUTE_VALUE_FIELD);
assertThat(nestedAttributeNameField.asText()).isEqualTo(CUSTOM_OBJECT_REFERENCE_SET_ATTR_NAME);
assertThat(nestedAttributeValueField).isInstanceOf(ArrayNode.class);
final ArrayNode referenceSet = (ArrayNode) nestedAttributeValueField;
assertThat(referenceSet).hasSize(2).anySatisfy(reference -> {
assertThat(reference.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(CustomObject.referenceTypeId());
assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testCustomObject1.getId());
}).anySatisfy(reference -> {
assertThat(reference.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(CustomObject.referenceTypeId());
assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testCustomObject2.getId());
});
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedCustomObjectsIT method sync_withNestedCustomObjectReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct.
@Test
void sync_withNestedCustomObjectReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueSetOfReferences(CUSTOM_OBJECT_REFERENCE_SET_ATTR_NAME, createReferenceObject(format("%s|%s", testCustomObject1.getContainer(), testCustomObject1.getKey()), CustomObject.referenceTypeId()), 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-set'");
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-set'");
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedProductTypesIT method sync_withChangedNestedProductTypeReferenceAsAttribute_shouldUpdateProductReferencingExistingProductType.
@Test
void sync_withChangedNestedProductTypeReferenceAsAttribute_shouldUpdateProductReferencingExistingProductType() {
// 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(testProductType2.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 newProductDraftWithProductTypeReference = 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(newProductDraftWithProductTypeReference)).toCompletableFuture().join();
// assertion
assertThat(syncStatistics).hasValues(1, 0, 1, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
final ObjectNode expectedNestedAttributeValue = createNestedAttributeValueReferences("productType-reference", createReferenceObject(testProductType2.getId(), ProductType.referenceTypeId()));
final AttributeDraft expectedProductTypeReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(expectedNestedAttributeValue));
assertThat(actions).containsExactly(SetAttribute.of(1, expectedProductTypeReferenceAttribute, true));
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(testProductType2.getId());
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedProductTypesIT method sync_withNestedProductTypeReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct.
@Test
void sync_withNestedProductTypeReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueSetOfReferences("productType-reference-set", createReferenceObject(testProductType1.getKey(), ProductType.referenceTypeId()), createReferenceObject("nonExistingKey", 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, 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 'nestedAttribute.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 'nestedAttribute.productType-reference-set'");
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
}
Aggregations