use of com.commercetools.sync.commons.utils.ResourceIdentifierUtils.REFERENCE_ID_FIELD in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedProductTypesIT method sync_withNestedProductTypeReferenceSetAsAttribute_shouldCreateProductReferencingExistingProductType.
@Test
void sync_withNestedProductTypeReferenceSetAsAttribute_shouldCreateProductReferencingExistingProductType() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueSetOfReferences("productType-reference-set", createReferenceObject(testProductType1.getKey(), ProductType.referenceTypeId()), createReferenceObject(testProductType2.getKey(), 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, 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> 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-set");
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(ProductType.referenceTypeId());
assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testProductType1.getId());
}).anySatisfy(reference -> {
assertThat(reference.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(ProductType.referenceTypeId());
assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testProductType2.getId());
});
});
}
use of com.commercetools.sync.commons.utils.ResourceIdentifierUtils.REFERENCE_ID_FIELD in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedProductsIT method sync_withNestedProductReferenceSetAsAttribute_shouldCreateProductReferencingExistingProducts.
@Test
void sync_withNestedProductReferenceSetAsAttribute_shouldCreateProductReferencingExistingProducts() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueSetOfReferences("product-reference-set", createReferenceObject(testProduct1.getKey(), Product.referenceTypeId()), createReferenceObject(testProduct2.getKey(), 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, 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 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-set");
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(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());
});
});
}
Aggregations