use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncTest method sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorFetchingExistingKeys_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, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
final ProductTypeService mockProductTypeService = mock(ProductTypeService.class);
when(mockProductTypeService.fetchMatchingProductTypesByKeys(singleton(newProductTypeDraft.getKey()))).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
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)).isEqualTo("Failed to fetch existing product types with keys: '[foo]'.");
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_WithNullAttributeDefinitions_ShouldSyncCorrectly.
@Test
void sync_WithNullAttributeDefinitions_ShouldSyncCorrectly() {
// preparation
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", null);
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final List<UpdateAction> actions = new ArrayList<>();
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(mock(SphereClient.class)).beforeUpdateCallback((generatedActions, draft, productType) -> {
actions.addAll(generatedActions);
return generatedActions;
}).errorCallback((exception, oldResource, newResource, updateActions) -> {
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(actions).containsExactly(ChangeName.of(newProductTypeDraft.getName()), ChangeDescription.of(newProductTypeDraft.getDescription()));
assertThat(productTypeSyncStatistics).hasValues(1, 0, 1, 0, 0);
}
use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncTest method sync_WithEmptyAttributeDefinitions_ShouldSyncCorrectly.
@Test
void sync_WithEmptyAttributeDefinitions_ShouldSyncCorrectly() {
// 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(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);
}
use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeWithNestedAttributeSyncIT method sync_WithUpdates_ShouldReturnProperStatistics.
@Test
void sync_WithUpdates_ShouldReturnProperStatistics() {
// preparation
populateProjectWithNestedAttributes(CTP_TARGET_CLIENT);
final List<ProductType> productTypes = CTP_SOURCE_CLIENT.execute(ProductTypeQuery.of()).toCompletableFuture().join().getResults();
// only update the nested types
final List<ProductTypeDraft> productTypeDrafts = ProductTypeTransformUtils.toProductTypeDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, productTypes).join().stream().map(productType -> {
final List<AttributeDefinitionDraft> attributeDefinitionDrafts = productType.getAttributes().stream().map(attribute -> {
if (attribute.getAttributeType() instanceof NestedAttributeType) {
return AttributeDefinitionDraftBuilder.of(attribute).label(ofEnglish("new-label")).build();
}
return AttributeDefinitionDraftBuilder.of(attribute).build();
}).collect(Collectors.toList());
return ProductTypeDraftBuilder.of(productType).attributes(attributeDefinitionDrafts).build();
}).collect(Collectors.toList());
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(productTypeDrafts).toCompletableFuture().join();
// assertion
assertThat(errorMessages).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(builtUpdateActions).containsExactly(ChangeAttributeDefinitionLabel.of("nestedattr2", ofEnglish("new-label")));
assertThat(productTypeSyncStatistics).hasValues(4, 0, 1, 0, 0);
assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 4 product types were processed in total" + " (0 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 ProductTypeSyncIT method sync_WithoutUpdates_ShouldReturnProperStatistics.
@Test
void sync_WithoutUpdates_ShouldReturnProperStatistics() {
// preparation
final List<ProductType> productTypes = CTP_SOURCE_CLIENT.execute(ProductTypeQuery.of()).toCompletableFuture().join().getResults();
final List<ProductTypeDraft> productTypeDrafts = productTypes.stream().map(ProductTypeDraftBuilder::of).map(ProductTypeDraftBuilder::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, 0, 0, 0);
assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 product types were processed in total" + " (1 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).");
}
Aggregations