use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncTest method sync_WithErrorsOnSyncing_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorsOnSyncing_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", emptyList());
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
final ProductTypeService mockProductTypeService = mock(ProductTypeServiceImpl.class);
final ProductType existingProductType = mock(ProductType.class);
when(existingProductType.getKey()).thenReturn(null);
when(mockProductTypeService.fetchMatchingProductTypesByKeys(singleton(newProductTypeDraft.getKey()))).thenReturn(CompletableFuture.completedFuture(singleton(existingProductType)));
when(mockProductTypeService.fetchMatchingProductTypesByKeys(emptySet())).thenReturn(CompletableFuture.completedFuture(Collections.emptySet()));
when(mockProductTypeService.cacheKeysToIds(anySet())).thenReturn(CompletableFuture.completedFuture(emptyMap()));
final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions, mockProductTypeService);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to process the productTypeDraft with key:'foo'." + " Reason: java.lang.NullPointerException");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(NullPointerException.class);
assertThat(productTypeSyncStatistics).hasValues(1, 0, 0, 1, 0);
}
use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncBenchmark method sync_NewProductTypes_ShouldCreateProductTypes.
@Test
void sync_NewProductTypes_ShouldCreateProductTypes() throws IOException {
// preparation
final List<ProductTypeDraft> productTypeDrafts = buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// benchmark
final long beforeSyncTime = System.currentTimeMillis();
final ProductTypeSyncStatistics syncStatistics = executeBlocking(productTypeSync.sync(productTypeDrafts));
final long totalTime = System.currentTimeMillis() - beforeSyncTime;
// assert on threshold (based on history of benchmarks; highest was ~12 seconds)
// double of the highest benchmark
final int threshold = 24000;
assertThat(totalTime).withFailMessage(format(THRESHOLD_EXCEEDED_ERROR, totalTime, threshold)).isLessThan(threshold);
// Assert actual state of CTP project (total number of existing product types)
final CompletableFuture<Integer> totalNumberOfProductTypes = CTP_TARGET_CLIENT.execute(ProductTypeQuery.of()).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
executeBlocking(totalNumberOfProductTypes);
assertThat(totalNumberOfProductTypes).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_TYPE_SYNC, CREATES_ONLY, totalTime);
}
use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncBenchmark method sync_ExistingProductTypes_ShouldUpdateProductTypes.
@Test
void sync_ExistingProductTypes_ShouldUpdateProductTypes() throws IOException {
// preparation
final List<ProductTypeDraft> productTypeDrafts = buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
// Create drafts to target project with different attribute definition name
CompletableFuture.allOf(productTypeDrafts.stream().map(ProductTypeDraftBuilder::of).map(ProductTypeSyncBenchmark::applyAttributeDefinitionNameChange).map(ProductTypeDraftBuilder::build).map(draft -> CTP_TARGET_CLIENT.execute(ProductTypeCreateCommand.of(draft))).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new)).join();
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// benchmark
final long beforeSyncTime = System.currentTimeMillis();
final ProductTypeSyncStatistics syncStatistics = executeBlocking(productTypeSync.sync(productTypeDrafts));
final long totalTime = System.currentTimeMillis() - beforeSyncTime;
// assert on threshold (based on history of benchmarks; highest was ~13 seconds)
// double of the highest benchmark
final int threshold = 26000;
assertThat(totalTime).withFailMessage(format(THRESHOLD_EXCEEDED_ERROR, totalTime, threshold)).isLessThan(threshold);
// Assert actual state of CTP project (number of updated product types)
final CompletableFuture<Integer> totalNumberOfUpdatedProductTypes = CTP_TARGET_CLIENT.execute(ProductTypeQuery.of().withPredicates(p -> p.attributes().name().is("attr_name_1"))).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
executeBlocking(totalNumberOfUpdatedProductTypes);
assertThat(totalNumberOfUpdatedProductTypes).isCompletedWithValue(NUMBER_OF_RESOURCE_UNDER_TEST);
// Assert actual state of CTP project (total number of existing product types)
final CompletableFuture<Integer> totalNumberOfProductTypes = CTP_TARGET_CLIENT.execute(ProductTypeQuery.of()).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
executeBlocking(totalNumberOfProductTypes);
assertThat(totalNumberOfProductTypes).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_TYPE_SYNC, UPDATES_ONLY, totalTime);
}
use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method sync_WithoutName_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithoutName_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
// Draft without "name" throws a commercetools exception because "name" is a required value
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, null, PRODUCT_TYPE_DESCRIPTION_1, asList(ATTRIBUTE_DEFINITION_DRAFT_1, ATTRIBUTE_DEFINITION_DRAFT_2));
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to update product type with key: 'key_1'.");
assertThat(exceptions).hasSize(1).singleElement().matches(throwable -> {
assertThat(throwable).isExactlyInstanceOf(CompletionException.class);
assertThat(throwable).hasCauseExactlyInstanceOf(ErrorResponseException.class);
assertThat(throwable).hasMessageContaining("Missing required value");
return true;
});
assertThat(productTypeSyncStatistics).hasValues(1, 0, 0, 1, 0);
}
use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method syncDrafts_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate.
@Test
void syncDrafts_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate() {
// Preparation
final SphereClient spyClient = buildClientWithConcurrentModificationUpdateAndNotFoundFetchOnRetry();
final ProductTypeDraft productTypeDraft = ProductTypeDraftBuilder.of("key", "foo", "description", emptyList()).build();
CTP_TARGET_CLIENT.execute(ProductTypeCreateCommand.of(productTypeDraft)).toCompletableFuture().join();
final String newProductTypeName = "bar";
final ProductTypeDraft updatedDraft = ProductTypeDraftBuilder.of(productTypeDraft).name(newProductTypeName).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> errors = new ArrayList<>();
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
errors.add(exception.getCause());
}).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions);
// Test
final ProductTypeSyncStatistics statistics = productTypeSync.sync(singletonList(updatedDraft)).toCompletableFuture().join();
// Assertion
assertThat(statistics).hasValues(1, 0, 0, 1, 0);
assertThat(errorMessages).hasSize(1);
assertThat(errors).hasSize(1);
assertThat(errorMessages.get(0)).contains(format("Failed to update product type with key: '%s'. Reason: Not found when attempting to fetch while " + "retrying after concurrency modification.", productTypeDraft.getKey()));
}
Aggregations