use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedProductsInAnyOrderIT method sync_withFailToFetchCustomObject_shouldSyncCorrectly.
@SuppressWarnings("unchecked")
@Test
void sync_withFailToFetchCustomObject_shouldSyncCorrectly() {
// preparation
final String productReferenceAttributeName = "product-reference";
final String parentProductKey = "parent-product-key";
final AttributeDraft productReferenceAttribute = AttributeDraft.of(productReferenceAttributeName, Reference.of(Product.referenceTypeId(), parentProductKey));
final ProductDraft childDraft1 = ProductDraftBuilder.of(productType, ofEnglish("foo"), ofEnglish("foo-slug"), ProductVariantDraftBuilder.of().key("foo").sku("foo").attributes(productReferenceAttribute).build()).key(product.getKey()).build();
final ProductDraft parentDraft = ProductDraftBuilder.of(productType, ofEnglish(parentProductKey), ofEnglish(parentProductKey), ProductVariantDraftBuilder.of().sku(parentProductKey).key(parentProductKey).build()).key(parentProductKey).build();
final SphereClient ctpClient = spy(CTP_TARGET_CLIENT);
final BadGatewayException gatewayException = new BadGatewayException("failed to respond.");
when(ctpClient.execute(any(CustomObjectQuery.class))).thenReturn(CompletableFutureUtils.failed(gatewayException)).thenCallRealMethod();
syncOptions = ProductSyncOptionsBuilder.of(ctpClient).errorCallback((syncException, draft, product, updateActions) -> collectErrors(syncException.getMessage(), syncException)).beforeUpdateCallback(this::collectActions).build();
// test
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(childDraft1)).thenCompose(ignoredResult -> productSync.sync(singletonList(parentDraft))).toCompletableFuture().join();
// assertion
assertThat(syncStatistics).hasValues(2, 1, 0, 1, 0);
assertThat(errorCallBackMessages).containsExactly("Failed to fetch ProductDrafts waiting to be resolved with keys '[foo]'.");
assertThat(errorCallBackExceptions).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(BadGatewayException.class);
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
final UnresolvedReferencesService<WaitingToBeResolvedProducts> unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(syncOptions);
final Set<WaitingToBeResolvedProducts> waitingDrafts = unresolvedReferencesService.fetch(asSet(childDraft1.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
assertThat(waitingDrafts).containsExactly(new WaitingToBeResolvedProducts(childDraft1, singleton(parentProductKey)));
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedProductsInAnyOrderIT method sync_withSingleChildDraftSuppliedBeforeMultipleParents_shouldSyncCorrectly.
@Test
void sync_withSingleChildDraftSuppliedBeforeMultipleParents_shouldSyncCorrectly() {
// preparation
final String productReferenceAttributeName = "product-reference-set";
final String parentProductKey = "parent-product-key";
final String parentProductKey2 = "parent-product-key2";
final AttributeDraft productReferenceAttribute = AttributeDraft.of(productReferenceAttributeName, asSet(Reference.of(Product.referenceTypeId(), parentProductKey), Reference.of(Product.referenceTypeId(), parentProductKey2)));
final ProductDraft childDraft = ProductDraftBuilder.of(productType, ofEnglish("foo"), ofEnglish("foo-slug"), ProductVariantDraftBuilder.of().key("foo").sku("foo").attributes(productReferenceAttribute).build()).key(product.getKey()).build();
final ProductDraft parentDraft1 = ProductDraftBuilder.of(productType, ofEnglish("bar"), ofEnglish("bar-slug"), ProductVariantDraftBuilder.of().sku("bar").key("bar").build()).key(parentProductKey).build();
final ProductDraft parentDraft2 = ProductDraftBuilder.of(productType, ofEnglish(parentProductKey2), ofEnglish(parentProductKey2), ProductVariantDraftBuilder.of().sku(parentProductKey2).key(parentProductKey2).build()).key(parentProductKey2).build();
// test
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = productSync.sync(asList(childDraft, parentDraft1, parentDraft2)).toCompletableFuture().join();
final Product syncedParent1 = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(parentDraft1.getKey())).toCompletableFuture().join();
final Product syncedParent2 = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(parentDraft2.getKey())).toCompletableFuture().join();
// assertion
assertThat(syncedParent1).isNotNull();
assertThat(syncedParent2).isNotNull();
assertThat(syncedParent1.getKey()).isEqualTo(parentProductKey);
assertThat(syncedParent2.getKey()).isEqualTo(parentProductKey2);
assertThat(syncStatistics).hasValues(3, 2, 1, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
final ArrayNode expectedAttributeValue = JsonNodeFactory.instance.arrayNode();
expectedAttributeValue.add(createReferenceObject(syncedParent1.getId(), Product.referenceTypeId()));
expectedAttributeValue.add(createReferenceObject(syncedParent2.getId(), Product.referenceTypeId()));
assertThat(actions).containsExactly(SetAttributeInAllVariants.of(productReferenceAttributeName, expectedAttributeValue, true));
final Product syncedChild = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(childDraft.getKey())).toCompletableFuture().join();
final Optional<Attribute> createdProductReferenceAttribute = syncedChild.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceAttribute.getName());
assertThat(createdProductReferenceAttribute).hasValueSatisfying(attribute -> {
assertThat(attribute.getValueAsJsonNode().get(0).get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(0).get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent1.getId());
assertThat(attribute.getValueAsJsonNode().get(1).get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(1).get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent2.getId());
});
assertNoWaitingDrafts(CTP_TARGET_CLIENT);
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedProductsInAnyOrderIT method sync_withSingleChildDraftSuppliedBeforeParent_shouldSyncCorrectly.
@Test
void sync_withSingleChildDraftSuppliedBeforeParent_shouldSyncCorrectly() {
// preparation
final String productReferenceAttributeName = "product-reference";
final String parentProductKey = "parent-product-key";
final AttributeDraft productReferenceAttribute = AttributeDraft.of(productReferenceAttributeName, Reference.of(Product.referenceTypeId(), parentProductKey));
final ProductDraft childDraft = ProductDraftBuilder.of(productType, ofEnglish("foo"), ofEnglish("foo-slug"), ProductVariantDraftBuilder.of().key("foo").sku("foo").attributes(productReferenceAttribute).build()).key(product.getKey()).build();
final ProductDraft parentDraft = ProductDraftBuilder.of(productType, ofEnglish("bar"), ofEnglish("bar-slug"), ProductVariantDraftBuilder.of().sku("bar").key("bar").build()).key(parentProductKey).build();
// test
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = productSync.sync(asList(childDraft, parentDraft)).toCompletableFuture().join();
final Product syncedParent = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(parentDraft.getKey())).toCompletableFuture().join();
// assertion
assertThat(syncedParent).isNotNull();
assertThat(syncedParent.getKey()).isEqualTo(parentProductKey);
assertThat(syncStatistics).hasValues(2, 1, 1, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).containsExactly(SetAttributeInAllVariants.of(productReferenceAttributeName, createReferenceObject(syncedParent.getId(), Product.referenceTypeId()), true));
final Product syncedChild = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(childDraft.getKey())).toCompletableFuture().join();
final Optional<Attribute> createdProductReferenceAttribute = syncedChild.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceAttribute.getName());
assertThat(createdProductReferenceAttribute).hasValueSatisfying(attribute -> {
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(syncedParent.getId());
});
assertNoWaitingDrafts(CTP_TARGET_CLIENT);
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class SyncSingleLocaleIT method setupTest.
/**
* Deletes Products from the target CTP project.
*/
@BeforeEach
void setupTest() {
clearSyncTestCollections();
deleteAllProducts(CTP_TARGET_CLIENT);
productSync = new ProductSync(buildSyncOptions());
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedCategoriesIT method sync_withCategoryReferenceAsAttribute_shouldCreateProductReferencingExistingCategory.
@Test
void sync_withCategoryReferenceAsAttribute_shouldCreateProductReferencingExistingCategory() {
// preparation
final AttributeDraft categoryReferenceAttribute = AttributeDraft.of("category-reference", Reference.of(Category.referenceTypeId(), category.getKey()));
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> createdProductReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(categoryReferenceAttribute.getName());
assertThat(createdProductReferenceAttribute).hasValueSatisfying(attribute -> {
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Category.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(category.getId());
});
}
Aggregations