use of com.commercetools.sync.producttypes.ProductTypeSync in project commercetools-project-sync by commercetools.
the class ProductTypeSyncer method of.
@Nonnull
public static ProductTypeSyncer of(@Nonnull final SphereClient sourceClient, @Nonnull final SphereClient targetClient, @Nonnull final Clock clock) {
final QuadConsumer<SyncException, Optional<ProductTypeDraft>, Optional<ProductType>, List<UpdateAction<ProductType>>> logErrorCallback = (exception, newResourceDraft, oldResource, updateActions) -> logErrorCallback(LOGGER, "product type", exception, oldResource, updateActions);
final TriConsumer<SyncException, Optional<ProductTypeDraft>, Optional<ProductType>> logWarningCallback = (exception, newResourceDraft, oldResource) -> logWarningCallback(LOGGER, "product type", exception, oldResource);
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(targetClient).errorCallback(logErrorCallback).warningCallback(logWarningCallback).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions);
final CustomObjectService customObjectService = new CustomObjectServiceImpl(targetClient);
return new ProductTypeSyncer(productTypeSync, sourceClient, targetClient, customObjectService, clock);
}
use of com.commercetools.sync.producttypes.ProductTypeSync in project commercetools-sync-java by commercetools.
the class ProductTypeSyncBenchmark method sync_WithSomeExistingProductTypes_ShouldSyncProductTypes.
@Test
void sync_WithSomeExistingProductTypes_ShouldSyncProductTypes() throws IOException {
// preparation
final List<ProductTypeDraft> productTypeDrafts = buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
final int halfNumberOfDrafts = productTypeDrafts.size() / 2;
final List<ProductTypeDraft> firstHalf = productTypeDrafts.subList(0, halfNumberOfDrafts);
// Create first half of drafts to target project with different attribute definition name
CompletableFuture.allOf(firstHalf.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> totalNumberOfProductTypesWithOldName = CTP_TARGET_CLIENT.execute(ProductTypeQuery.of().withPredicates(p -> p.attributes().name().is("attr_name_1_old"))).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
executeBlocking(totalNumberOfProductTypesWithOldName);
assertThat(totalNumberOfProductTypesWithOldName).isCompletedWithValue(0);
// 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, halfNumberOfDrafts, halfNumberOfDrafts, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
saveNewResult(PRODUCT_TYPE_SYNC, CREATES_AND_UPDATES, totalTime);
}
use of com.commercetools.sync.producttypes.ProductTypeSync in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method sync_WithoutAttributeType_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithoutAttributeType_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final AttributeDefinitionDraft attributeDefinitionDraft = AttributeDefinitionDraftBuilder.of(null, "attr_name_1", ofEnglish("attr_label_1"), true).attributeConstraint(AttributeConstraint.NONE).inputTip(ofEnglish("inputTip1")).inputHint(TextInputHint.SINGLE_LINE).isSearchable(false).build();
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, null, PRODUCT_TYPE_DESCRIPTION_1, singletonList(attributeDefinitionDraft));
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.ProductTypeSync in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method sync_WithErrorUpdatingTheProductType_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorUpdatingTheProductType_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
// Invalid attribute definition due to having an invalid name.
final AttributeDefinitionDraftDsl invalidAttrDefinition = AttributeDefinitionDraftBuilder.of(MoneyAttributeType.of(), "*invalidName*", ofEnglish("description"), true).searchable(false).build();
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, PRODUCT_TYPE_NAME_1, PRODUCT_TYPE_DESCRIPTION_1, singletonList(invalidAttrDefinition));
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("InvalidInput");
return true;
});
assertThat(productTypeSyncStatistics).hasValues(1, 0, 0, 1, 0);
}
use of com.commercetools.sync.producttypes.ProductTypeSync in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method sync_WithSetOfEnumsAndSetOfLenumsChanges_ShouldUpdateProductType.
@Test
void sync_WithSetOfEnumsAndSetOfLenumsChanges_ShouldUpdateProductType() {
// preparation
final AttributeDefinitionDraft withSetOfEnumsOld = AttributeDefinitionDraftBuilder.of(SetAttributeType.of(EnumAttributeType.of(asList(EnumValue.of("d", "d"), EnumValue.of("b", "newB"), EnumValue.of("a", "a"), EnumValue.of("c", "c")))), "foo", ofEnglish("foo"), false).build();
final AttributeDefinitionDraft withSetOfSetOfLEnumsOld = AttributeDefinitionDraftBuilder.of(SetAttributeType.of(LocalizedEnumAttributeType.of(asList(LocalizedEnumValue.of("d", ofEnglish("d")), LocalizedEnumValue.of("b", ofEnglish("newB")), LocalizedEnumValue.of("a", ofEnglish("a")), LocalizedEnumValue.of("c", ofEnglish("c"))))), "bar", ofEnglish("bar"), false).build();
final ProductTypeDraft oldDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("withSetOfEnums", "withSetOfEnums", "withSetOfEnums", asList(withSetOfEnumsOld, withSetOfSetOfLEnumsOld));
CTP_TARGET_CLIENT.execute(ProductTypeCreateCommand.of(oldDraft)).toCompletableFuture().join();
final AttributeDefinitionDraft withSetOfEnumsNew = AttributeDefinitionDraftBuilder.of(SetAttributeType.of(EnumAttributeType.of(asList(EnumValue.of("a", "a"), EnumValue.of("b", "b"), EnumValue.of("c", "c")))), "foo", ofEnglish("foo"), false).build();
final AttributeDefinitionDraft withSetOfSetOfLEnumsNew = AttributeDefinitionDraftBuilder.of(SetAttributeType.of(LocalizedEnumAttributeType.of(asList(LocalizedEnumValue.of("a", ofEnglish("a")), LocalizedEnumValue.of("b", ofEnglish("newB")), LocalizedEnumValue.of("c", ofEnglish("c"))))), "bar", ofEnglish("bar"), false).build();
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("withSetOfEnums", "withSetOfEnums", "withSetOfEnums", asList(withSetOfEnumsNew, withSetOfSetOfLEnumsNew));
final ProductTypeSyncOptions productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// tests
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
// assertions
assertThat(productTypeSyncStatistics).hasValues(1, 0, 1, 0, 0);
final Optional<ProductType> oldProductTypeAfter = getProductTypeByKey(CTP_TARGET_CLIENT, "withSetOfEnums");
assertThat(oldProductTypeAfter).hasValueSatisfying(productType -> assertAttributesAreEqual(productType.getAttributes(), asList(withSetOfEnumsNew, withSetOfSetOfLEnumsNew)));
}
Aggregations