|
27 | 27 | import com.commercetools.sync.products.ProductSyncOptionsBuilder; |
28 | 28 | import com.commercetools.sync.products.helpers.ProductSyncStatistics; |
29 | 29 | import com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl; |
| 30 | +import com.fasterxml.jackson.core.type.TypeReference; |
30 | 31 | import com.fasterxml.jackson.databind.JsonNode; |
31 | 32 | import com.fasterxml.jackson.databind.node.ObjectNode; |
32 | 33 | import io.vrap.rmf.base.client.utils.json.JsonUtils; |
@@ -188,6 +189,129 @@ void sync_withProductReferenceAsAttribute_shouldCreateProductReferencingExisting |
188 | 189 | }); |
189 | 190 | } |
190 | 191 |
|
| 192 | + @Test |
| 193 | + void sync_withProductReferencingItselfAsAttribute_shouldCreateProductReferencingItself() { |
| 194 | + // preparation |
| 195 | + final String sameNewProductKey = "new-product-key"; |
| 196 | + final Attribute productReferenceAttribute = |
| 197 | + AttributeBuilder.of() |
| 198 | + .name("product-reference") |
| 199 | + .value(createReferenceObject(sameNewProductKey, ProductReference.PRODUCT)) |
| 200 | + .build(); |
| 201 | + final Set<Reference> references = |
| 202 | + Set.of( |
| 203 | + createReferenceObject(sameNewProductKey, ProductReference.PRODUCT), |
| 204 | + createReferenceObject(product.getKey(), ProductReference.PRODUCT)); |
| 205 | + |
| 206 | + final Attribute productReferenceSetAttribute = |
| 207 | + AttributeBuilder.of().name("product-reference-set").value(references).build(); |
| 208 | + |
| 209 | + final ProductVariantDraft masterVariant = |
| 210 | + ProductVariantDraftBuilder.of() |
| 211 | + .sku("sku") |
| 212 | + .key("new-product-master-variant") |
| 213 | + .attributes(productReferenceAttribute, productReferenceSetAttribute) |
| 214 | + .build(); |
| 215 | + |
| 216 | + final ProductDraft productDraftWithProductReference = |
| 217 | + ProductDraftBuilder.of() |
| 218 | + .productType(productType.toResourceIdentifier()) |
| 219 | + .name(ofEnglish("productName")) |
| 220 | + .slug(ofEnglish("productSlug")) |
| 221 | + .masterVariant(masterVariant) |
| 222 | + .key(sameNewProductKey) |
| 223 | + .build(); |
| 224 | + |
| 225 | + // test |
| 226 | + final ProductSync productSync = new ProductSync(syncOptions); |
| 227 | + final ProductSyncStatistics syncStatistics = |
| 228 | + productSync |
| 229 | + .sync(singletonList(productDraftWithProductReference)) |
| 230 | + .toCompletableFuture() |
| 231 | + .join(); |
| 232 | + |
| 233 | + // assertion |
| 234 | + assertThat(syncStatistics).hasValues(1, 1, 1, 0, 0); |
| 235 | + assertThat(errorCallBackExceptions).isEmpty(); |
| 236 | + assertThat(errorCallBackMessages).isEmpty(); |
| 237 | + assertThat(warningCallBackMessages).isEmpty(); |
| 238 | + final Product createdProduct = |
| 239 | + CTP_TARGET_CLIENT |
| 240 | + .products() |
| 241 | + .withKey(productDraftWithProductReference.getKey()) |
| 242 | + .get() |
| 243 | + .execute() |
| 244 | + .toCompletableFuture() |
| 245 | + .join() |
| 246 | + .getBody(); |
| 247 | + |
| 248 | + final JsonNode referenceObjectSameProduct = |
| 249 | + JsonUtils.toJsonNode( |
| 250 | + createReferenceObject(createdProduct.getId(), ProductReference.PRODUCT)); |
| 251 | + final JsonNode referenceObjectOtherProduct = |
| 252 | + JsonUtils.toJsonNode(createReferenceObject(product.getId(), ProductReference.PRODUCT)); |
| 253 | + assertThat(actions).hasSize(2); |
| 254 | + assertThat(actions.get(0)) |
| 255 | + .satisfies( |
| 256 | + productUpdateAction -> { |
| 257 | + assertThat(productUpdateAction) |
| 258 | + .isInstanceOf(ProductSetAttributeInAllVariantsAction.class); |
| 259 | + ProductSetAttributeInAllVariantsAction castedAction = |
| 260 | + (ProductSetAttributeInAllVariantsAction) productUpdateAction; |
| 261 | + assertThat(castedAction.getName()).isEqualTo("product-reference"); |
| 262 | + assertThat(castedAction.getStaged()).isTrue(); |
| 263 | + assertThat(castedAction.getValue()).isEqualTo(referenceObjectSameProduct); |
| 264 | + }); |
| 265 | + assertThat(actions.get(1)) |
| 266 | + .satisfies( |
| 267 | + productUpdateAction -> { |
| 268 | + assertThat(productUpdateAction) |
| 269 | + .isInstanceOf(ProductSetAttributeInAllVariantsAction.class); |
| 270 | + ProductSetAttributeInAllVariantsAction castedAction = |
| 271 | + (ProductSetAttributeInAllVariantsAction) productUpdateAction; |
| 272 | + assertThat(castedAction.getName()).isEqualTo("product-reference-set"); |
| 273 | + assertThat(castedAction.getStaged()).isTrue(); |
| 274 | + List<JsonNode> valueAsList = |
| 275 | + JsonUtils.fromJsonNode( |
| 276 | + (JsonNode) castedAction.getValue(), new TypeReference<>() {}); |
| 277 | + assertThat(valueAsList) |
| 278 | + .containsExactlyInAnyOrder( |
| 279 | + referenceObjectSameProduct, referenceObjectOtherProduct); |
| 280 | + }); |
| 281 | + |
| 282 | + final Optional<Attribute> createdProductReferenceAttribute = |
| 283 | + createdProduct |
| 284 | + .getMasterData() |
| 285 | + .getStaged() |
| 286 | + .getMasterVariant() |
| 287 | + .findAttribute(productReferenceAttribute.getName()); |
| 288 | + |
| 289 | + assertThat(createdProductReferenceAttribute) |
| 290 | + .hasValueSatisfying( |
| 291 | + attribute -> { |
| 292 | + final ProductReference productReference = (ProductReference) attribute.getValue(); |
| 293 | + assertThat(productReference.getTypeId()).isEqualTo(ReferenceTypeId.PRODUCT); |
| 294 | + assertThat(productReference.getId()).isEqualTo(createdProduct.getId()); |
| 295 | + }); |
| 296 | + |
| 297 | + final Optional<Attribute> createdProductReferenceSetAttribute = |
| 298 | + createdProduct |
| 299 | + .getMasterData() |
| 300 | + .getStaged() |
| 301 | + .getMasterVariant() |
| 302 | + .findAttribute(productReferenceSetAttribute.getName()); |
| 303 | + |
| 304 | + assertThat(createdProductReferenceSetAttribute) |
| 305 | + .hasValueSatisfying( |
| 306 | + attribute -> { |
| 307 | + final List<Reference> referenceList = AttributeAccessor.asSetReference(attribute); |
| 308 | + assertThat(referenceList) |
| 309 | + .containsExactlyInAnyOrder( |
| 310 | + ProductReferenceBuilder.of().id(createdProduct.getId()).build(), |
| 311 | + ProductReferenceBuilder.of().id(product.getId()).build()); |
| 312 | + }); |
| 313 | + } |
| 314 | + |
191 | 315 | @Test |
192 | 316 | void sync_withSameProductReferenceAsAttribute_shouldNotSyncAnythingNew() { |
193 | 317 | // preparation |
@@ -552,6 +676,7 @@ void sync_withProductReferenceSetContainingANonExistingReference_shouldFailCreat |
552 | 676 | final Set<Reference> references = |
553 | 677 | Set.of( |
554 | 678 | createReferenceObject("nonExistingKey", ProductReference.PRODUCT), |
| 679 | + createReferenceObject("new-product", ProductReference.PRODUCT), |
555 | 680 | createReferenceObject(product2.getKey(), ProductReference.PRODUCT)); |
556 | 681 |
|
557 | 682 | final Attribute productReferenceSetAttribute = |
@@ -609,5 +734,61 @@ void sync_withProductReferenceSetContainingANonExistingReference_shouldFailCreat |
609 | 734 | .containsExactly("nonExistingKey"); |
610 | 735 | return true; |
611 | 736 | }); |
| 737 | + |
| 738 | + final Product missingProductCreated = |
| 739 | + CTP_TARGET_CLIENT |
| 740 | + .products() |
| 741 | + .create( |
| 742 | + ProductDraftBuilder.of() |
| 743 | + .productType(productType.toResourceIdentifier()) |
| 744 | + .name(ofEnglish("noExistingName")) |
| 745 | + .slug(ofEnglish("noExistingSlug")) |
| 746 | + .key("nonExistingKey") |
| 747 | + .build()) |
| 748 | + .executeBlocking() |
| 749 | + .getBody(); |
| 750 | + // rebuild syncOptions to clear old statistics |
| 751 | + syncOptions = buildSyncOptions(); |
| 752 | + final ProductSync productSync2ndRun = new ProductSync(syncOptions); |
| 753 | + final ProductSyncStatistics syncStatistics2ndRun = |
| 754 | + productSync2ndRun |
| 755 | + .sync(singletonList(productDraftWithProductReference)) |
| 756 | + .toCompletableFuture() |
| 757 | + .join(); |
| 758 | + |
| 759 | + // assertion |
| 760 | + assertThat(syncStatistics2ndRun).hasValues(1, 1, 1, 0, 0); |
| 761 | + assertThat(errorCallBackExceptions).isEmpty(); |
| 762 | + assertThat(errorCallBackMessages).isEmpty(); |
| 763 | + assertThat(warningCallBackMessages).isEmpty(); |
| 764 | + assertThat(actions).isNotEmpty(); |
| 765 | + |
| 766 | + final Product createdProduct = |
| 767 | + CTP_TARGET_CLIENT |
| 768 | + .products() |
| 769 | + .withKey(productDraftWithProductReference.getKey()) |
| 770 | + .get() |
| 771 | + .execute() |
| 772 | + .toCompletableFuture() |
| 773 | + .join() |
| 774 | + .getBody(); |
| 775 | + |
| 776 | + final Optional<Attribute> createdProductReferenceSetAttribute = |
| 777 | + createdProduct |
| 778 | + .getMasterData() |
| 779 | + .getStaged() |
| 780 | + .getMasterVariant() |
| 781 | + .findAttribute(productReferenceSetAttribute.getName()); |
| 782 | + |
| 783 | + assertThat(createdProductReferenceSetAttribute) |
| 784 | + .hasValueSatisfying( |
| 785 | + attribute -> { |
| 786 | + final List<Reference> referenceList = AttributeAccessor.asSetReference(attribute); |
| 787 | + assertThat(referenceList) |
| 788 | + .containsExactlyInAnyOrder( |
| 789 | + ProductReferenceBuilder.of().id(createdProduct.getId()).build(), |
| 790 | + ProductReferenceBuilder.of().id(missingProductCreated.getId()).build(), |
| 791 | + ProductReferenceBuilder.of().id(product2.getId()).build()); |
| 792 | + }); |
612 | 793 | } |
613 | 794 | } |
0 commit comments