use of com.commercetools.sync.products.ProductSync in project commercetools-project-sync by commercetools.
the class ProductSyncer method of.
@Nonnull
public static ProductSyncer of(@Nonnull final SphereClient sourceClient, @Nonnull final SphereClient targetClient, @Nonnull final Clock clock, @Nullable final ProductSyncCustomRequest productSyncCustomRequest) {
final QuadConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>, List<UpdateAction<Product>>> logErrorCallback = (exception, newResourceDraft, oldResource, updateActions) -> {
final String resourceKey = oldResource.map(WithKey::getKey).orElse(IDENTIFIER_NOT_PRESENT);
logErrorCallback(LOGGER, "product", exception, resourceKey, updateActions);
};
final TriConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>> logWarningCallback = (exception, newResourceDraft, oldResource) -> logWarningCallback(LOGGER, "product", exception, oldResource);
final ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(targetClient).errorCallback(logErrorCallback).warningCallback(logWarningCallback).build();
final ProductSync productSync = new ProductSync(syncOptions);
final CustomObjectService customObjectService = new CustomObjectServiceImpl(targetClient);
return new ProductSyncer(productSync, sourceClient, targetClient, customObjectService, clock, productSyncCustomRequest);
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncBenchmark method sync_NewProducts_ShouldCreateProducts.
@Test
void sync_NewProducts_ShouldCreateProducts() throws IOException {
final List<ProductDraft> productDrafts = buildProductDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
// Sync drafts
final ProductSync productSync = new ProductSync(syncOptions);
final long beforeSyncTime = System.currentTimeMillis();
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(productDrafts));
final long totalTime = System.currentTimeMillis() - beforeSyncTime;
// assert on threshold (based on history of benchmarks, highest was ~16 seconds)
// double of the highest benchmark
final int threshold = 32000;
assertThat(totalTime).withFailMessage(format(THRESHOLD_EXCEEDED_ERROR, totalTime, threshold)).isLessThan(threshold);
// Assert actual state of CTP project (total number of existing products)
final CompletableFuture<Integer> totalNumberOfProducts = CTP_TARGET_CLIENT.execute(ProductProjectionQuery.ofStaged()).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
executeBlocking(totalNumberOfProducts);
assertThat(totalNumberOfProducts).isCompletedWithValue(NUMBER_OF_RESOURCE_UNDER_TEST);
assertThat(syncStatistics).hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, NUMBER_OF_RESOURCE_UNDER_TEST, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
saveNewResult(PRODUCT_SYNC, CREATES_ONLY, totalTime);
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedProductsIT method sync_withSameProductReferenceAsAttribute_shouldNotSyncAnythingNew.
@Test
void sync_withSameProductReferenceAsAttribute_shouldNotSyncAnythingNew() {
// preparation
final AttributeDraft productReferenceAttribute = AttributeDraft.of("product-reference", Reference.of(Product.referenceTypeId(), product));
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();
CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraftWithProductReference)).toCompletableFuture().join();
final AttributeDraft newProductReferenceAttribute = AttributeDraft.of("product-reference", Reference.of(Product.referenceTypeId(), product.getKey()));
final ProductVariantDraft newMasterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(newProductReferenceAttribute).build();
final ProductDraft newProductDraftWithProductReference = 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(newProductDraftWithProductReference)).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(productDraftWithProductReference.getKey())).toCompletableFuture().join();
final Optional<Attribute> createdProductReferenceAttribute = createdProduct.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(product.getId());
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedProductsIT method sync_withChangedProductReferenceAsAttribute_shouldUpdateProductReferencingExistingProduct.
@Test
void sync_withChangedProductReferenceAsAttribute_shouldUpdateProductReferencingExistingProduct() {
// preparation
final AttributeDraft productReferenceAttribute = AttributeDraft.of("product-reference", Reference.of(Product.referenceTypeId(), product));
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();
CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraftWithProductReference)).toCompletableFuture().join();
final AttributeDraft newProductReferenceAttribute = AttributeDraft.of("product-reference", Reference.of(Product.referenceTypeId(), product2.getKey()));
final ProductVariantDraft newMasterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(newProductReferenceAttribute).build();
final ProductDraft newProductDraftWithProductReference = 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(newProductDraftWithProductReference)).toCompletableFuture().join();
// assertion
assertThat(syncStatistics).hasValues(1, 0, 1, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
final AttributeDraft expectedAttribute = AttributeDraft.of("product-reference", Reference.of(Product.referenceTypeId(), product2.getId()));
assertThat(actions).containsExactly(SetAttributeInAllVariants.of(expectedAttribute, true));
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 -> {
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(product2.getId());
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedProductsInAnyOrderIT method sync_withMissingParent_shouldSyncCorrectly.
@Test
void sync_withMissingParent_shouldSyncCorrectly() {
// preparation
final String productReferenceAttributeName = "product-reference";
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 childDraft1 = ProductDraftBuilder.of(productType, ofEnglish("foo"), ofEnglish("foo-slug"), ProductVariantDraftBuilder.of().key("foo").sku("foo").attributes(productReferenceAttribute).build()).key(product.getKey()).build();
final ProductDraft childDraft2 = ProductDraftBuilder.of(productType, ofEnglish("foo-2"), ofEnglish("foo-slug-2"), ProductVariantDraftBuilder.of().key("foo-2").sku("foo-2").attributes(productReferenceAttribute).build()).key("foo-2").build();
final ProductDraft parentDraft = 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(childDraft1, childDraft2, parentDraft)).toCompletableFuture().join();
final Product syncedParent = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(parentProductKey)).toCompletableFuture().join();
// assertion
assertThat(syncedParent).isNull();
assertThat(syncStatistics).hasValues(3, 1, 0, 0, 2);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
final UnresolvedReferencesService<WaitingToBeResolvedProducts> unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(syncOptions);
final Set<WaitingToBeResolvedProducts> waitingDrafts = unresolvedReferencesService.fetch(asSet(childDraft1.getKey(), childDraft2.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
assertThat(waitingDrafts).containsExactlyInAnyOrder(new WaitingToBeResolvedProducts(childDraft1, singleton(parentProductKey)), new WaitingToBeResolvedProducts(childDraft2, singleton(parentProductKey)));
}
Aggregations