use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncBenchmark method sync_ExistingProducts_ShouldUpdateProducts.
@Test
void sync_ExistingProducts_ShouldUpdateProducts() throws IOException {
final List<ProductDraft> productDrafts = buildProductDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
// Create drafts to target project with different descriptions
CompletableFuture.allOf(productDrafts.stream().map(ProductDraftBuilder::of).map(builder -> builder.description(ofEnglish("oldDescription"))).map(builder -> builder.productType(productType.toReference())).map(ProductDraftBuilder::build).map(draft -> CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(draft))).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new)).join();
// Sync new 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 ~19 seconds)
// double of the highest benchmark
final int threshold = 38000;
assertThat(totalTime).withFailMessage(format(THRESHOLD_EXCEEDED_ERROR, totalTime, threshold)).isLessThan(threshold);
// Assert actual state of CTP project (number of updated products)
final CompletableFuture<Integer> totalNumberOfUpdatedProducts = CTP_TARGET_CLIENT.execute(ProductQuery.of().withPredicates(p -> p.masterData().staged().description().locale(ENGLISH).is("newDescription"))).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
executeBlocking(totalNumberOfUpdatedProducts);
assertThat(totalNumberOfUpdatedProducts).isCompletedWithValue(NUMBER_OF_RESOURCE_UNDER_TEST);
// 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);
// Assert statistics
assertThat(syncStatistics).hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, 0, NUMBER_OF_RESOURCE_UNDER_TEST, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
saveNewResult(PRODUCT_SYNC, UPDATES_ONLY, totalTime);
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncBenchmark method sync_WithSomeExistingProducts_ShouldSyncProducts.
@Test
void sync_WithSomeExistingProducts_ShouldSyncProducts() throws IOException {
final List<ProductDraft> productDrafts = buildProductDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
final int halfNumberOfDrafts = productDrafts.size() / 2;
final List<ProductDraft> firstHalf = productDrafts.subList(0, halfNumberOfDrafts);
// Create first half of drafts to target project with different description
CompletableFuture.allOf(firstHalf.stream().map(ProductDraftBuilder::of).map(builder -> builder.description(ofEnglish("oldDescription"))).map(builder -> builder.productType(productType.toReference())).map(ProductDraftBuilder::build).map(draft -> CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(draft))).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new)).join();
// Sync new 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 ~19 seconds)
// double of the highest benchmark
final int threshold = 38000;
assertThat(totalTime).withFailMessage(format(THRESHOLD_EXCEEDED_ERROR, totalTime, threshold)).isLessThan(threshold);
// Assert actual state of CTP project (number of updated products)
final CompletableFuture<Integer> totalNumberOfUpdatedProducts = CTP_TARGET_CLIENT.execute(ProductQuery.of().withPredicates(p -> p.masterData().staged().description().locale(ENGLISH).is("oldDescription"))).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
executeBlocking(totalNumberOfUpdatedProducts);
assertThat(totalNumberOfUpdatedProducts).isCompletedWithValue(0);
// Assert actual state of CTP project (total number of existing products)
final CompletableFuture<Integer> totalNumberOfProducts = CTP_TARGET_CLIENT.execute(ProductQuery.of()).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
executeBlocking(totalNumberOfProducts);
assertThat(totalNumberOfProducts).isCompletedWithValue(NUMBER_OF_RESOURCE_UNDER_TEST);
// Assert statistics
assertThat(syncStatistics).hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, halfNumberOfDrafts, halfNumberOfDrafts, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
saveNewResult(PRODUCT_SYNC, CREATES_AND_UPDATES, totalTime);
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithPricesIT 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 ProductSyncFilterIT method sync_withChangedProductBlackListingCategories_shouldUpdateProductWithoutCategories.
@Test
void sync_withChangedProductBlackListingCategories_shouldUpdateProductWithoutCategories() {
final ProductDraft productDraft = createProductDraft(PRODUCT_KEY_1_CHANGED_RESOURCE_PATH, referenceOfId(productType.getKey()), null, null, categoryResourceIdentifiersWithKeys, createRandomCategoryOrderHints(categoryResourceIdentifiersWithKeys));
final ProductSyncOptions syncOptions = syncOptionsBuilder.syncFilter(ofBlackList(CATEGORIES)).build();
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(singletonList(productDraft)));
assertThat(syncStatistics).hasValues(1, 0, 1, 0, 0);
assertThat(updateActionsFromSync.stream().noneMatch(updateAction -> updateAction instanceof RemoveFromCategory)).isTrue();
assertThat(updateActionsFromSync.stream().noneMatch(updateAction -> updateAction instanceof AddToCategory)).isTrue();
assertThat(updateActionsFromSync.stream().noneMatch(updateAction -> updateAction instanceof SetCategoryOrderHint)).isTrue();
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withSameSlugInSingleBatch_ShouldNotSyncIt.
@Test
void sync_withSameSlugInSingleBatch_ShouldNotSyncIt() {
// Prepare batches from external source
final ProductDraft productDraft = createProductDraft(PRODUCT_KEY_1_CHANGED_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey()), ResourceIdentifier.ofKey(targetTaxCategory.getKey()), ResourceIdentifier.ofKey(targetProductState.getKey()), categoryResourceIdentifiersWithKeys, categoryOrderHintsWithKeys);
final ProductDraft key3Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(null).state(null).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey3").masterVariant(ProductVariantDraftBuilder.of().key("k3").sku("s3").build()).build();
final ProductDraft key4Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(null).state(null).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey4").masterVariant(ProductVariantDraftBuilder.of().key("k4").sku("s4").build()).build();
final ProductDraft key5Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(null).state(null).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey5").masterVariant(ProductVariantDraftBuilder.of().key("k5").sku("s5").build()).build();
final ProductDraft key6Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(null).state(null).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey6").masterVariant(ProductVariantDraftBuilder.of().key("k6").sku("s6").build()).build();
final List<ProductDraft> batch = new ArrayList<>();
batch.add(productDraft);
batch.add(key3Draft);
batch.add(key4Draft);
batch.add(key5Draft);
batch.add(key6Draft);
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(batch));
assertThat(syncStatistics).hasValues(5, 1, 1, 3, 0);
final String duplicatedSlug = key3Draft.getSlug().get(Locale.ENGLISH);
assertThat(errorCallBackExceptions).hasSize(3);
assertThat(errorCallBackExceptions).allSatisfy(exception -> {
assertThat(exception).isExactlyInstanceOf(ErrorResponseException.class);
final ErrorResponseException errorResponse = ((ErrorResponseException) exception);
final List<DuplicateFieldError> fieldErrors = errorResponse.getErrors().stream().map(sphereError -> {
assertThat(sphereError.getCode()).isEqualTo(DuplicateFieldError.CODE);
return sphereError.as(DuplicateFieldError.class);
}).collect(toList());
assertThat(fieldErrors).hasSize(1);
assertThat(fieldErrors).allSatisfy(error -> {
assertThat(error.getField()).isEqualTo("slug.en");
assertThat(error.getDuplicateValue()).isEqualTo(duplicatedSlug);
});
});
assertThat(errorCallBackMessages).hasSize(3).allSatisfy(errorMessage -> {
assertThat(errorMessage).contains("\"code\" : \"DuplicateField\"");
assertThat(errorMessage).contains("\"field\" : \"slug.en\"");
assertThat(errorMessage).contains(format("\"duplicateValue\" : \"%s\"", duplicatedSlug));
});
assertThat(warningCallBackMessages).isEmpty();
}
Aggregations