use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method sync_WithUpdates_ShouldReturnProperStatistics.
@Test
void sync_WithUpdates_ShouldReturnProperStatistics() {
// preparation
final List<ProductType> productTypes = CTP_SOURCE_CLIENT.execute(ProductTypeQuery.of()).toCompletableFuture().join().getResults();
final List<ProductTypeDraft> productTypeDrafts = productTypes.stream().map(productType -> {
final List<AttributeDefinitionDraft> attributeDefinitionDrafts = productType.getAttributes().stream().map(attribute -> AttributeDefinitionDraftBuilder.of(attribute).build()).collect(Collectors.toList());
return ProductTypeDraftBuilder.of(productType.getKey(), "newName", productType.getDescription(), attributeDefinitionDrafts).build();
}).collect(Collectors.toList());
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final ProductTypeSyncOptions productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(productTypeDrafts).toCompletableFuture().join();
// assertion
assertThat(errorMessages).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(productTypeSyncStatistics).hasValues(2, 1, 1, 0, 0);
assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 product types were processed in total" + " (1 created, 1 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
}
use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeWithNestedAttributeSyncIT method sync_WithoutUpdates_ShouldReturnProperStatistics.
@Test
void sync_WithoutUpdates_ShouldReturnProperStatistics() {
// preparation
populateProjectWithNestedAttributes(CTP_TARGET_CLIENT);
final List<ProductType> productTypes = CTP_SOURCE_CLIENT.execute(ProductTypeQuery.of()).toCompletableFuture().join().getResults();
final List<ProductTypeDraft> productTypeDrafts = ProductTypeTransformUtils.toProductTypeDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, productTypes).join();
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(productTypeDrafts).toCompletableFuture().join();
// assertion
assertThat(errorMessages).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(builtUpdateActions).isEmpty();
assertThat(productTypeSyncStatistics).hasValues(4, 0, 0, 0, 0);
assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 4 product types were processed in total" + " (0 created, 0 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
}
use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeWithNestedAttributeSyncIT method sync_WithEmptyTargetProject_ShouldReturnProperStatistics.
@Test
void sync_WithEmptyTargetProject_ShouldReturnProperStatistics() {
// preparation
final List<ProductType> productTypes = CTP_SOURCE_CLIENT.execute(ProductTypeQuery.of()).toCompletableFuture().join().getResults();
final List<ProductTypeDraft> productTypeDrafts = ProductTypeTransformUtils.toProductTypeDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, productTypes).join();
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(productTypeDrafts).toCompletableFuture().join();
// assertion
assertThat(errorMessages).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(builtUpdateActions).isEmpty();
assertThat(productTypeSyncStatistics).hasValues(4, 4, 0, 0, 0);
assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 4 product types were processed in total" + " (4 created, 0 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
}
use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncTest method sync_WithErrorCachingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorCachingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final AttributeDefinitionDraft nestedTypeAttrDefDraft = AttributeDefinitionDraftBuilder.of(NestedAttributeType.of(referenceOfId("x")), "validNested", ofEnglish("koko"), true).build();
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", singletonList(nestedTypeAttrDefDraft));
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final SphereClient sphereClient = mock(SphereClient.class);
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(sphereClient).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
final ProductTypeService mockProductTypeService = new ProductTypeServiceImpl(syncOptions);
when(sphereClient.execute(any(ResourceKeyIdGraphQlRequest.class))).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
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)).isEqualTo("Failed to build a cache of keys to ids.");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(SphereException.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 ProductTypeSyncTest method sync_WithNullInAttributeDefinitions_ShouldSyncCorrectly.
@Test
void sync_WithNullInAttributeDefinitions_ShouldSyncCorrectly() {
// preparation
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", singletonList(null));
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(newProductTypeDraft.getKey());
when(mockProductTypeService.fetchMatchingProductTypesByKeys(singleton(newProductTypeDraft.getKey()))).thenReturn(CompletableFuture.completedFuture(singleton(existingProductType)));
when(mockProductTypeService.fetchMatchingProductTypesByKeys(emptySet())).thenReturn(CompletableFuture.completedFuture(Collections.emptySet()));
when(mockProductTypeService.updateProductType(any(), any())).thenReturn(CompletableFuture.completedFuture(existingProductType));
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).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(productTypeSyncStatistics).hasValues(1, 0, 1, 0, 0);
}
Aggregations