use of com.commercetools.sync.categories.CategorySync in project commercetools-sync-java by commercetools.
the class CategorySyncIT method syncDrafts_withExistingShuffledCategoriesWithChangingCategoryHierarchy_ShouldUpdateCategories.
@Test
void syncDrafts_withExistingShuffledCategoriesWithChangingCategoryHierarchy_ShouldUpdateCategories() {
// -----------------Test Setup------------------------------------
// Delete all categories in target project
deleteAllCategories(CTP_TARGET_CLIENT);
// Create a total of 130 categories in the target project
final List<Category> subFamily = createChildren(5, null, "root", CTP_TARGET_CLIENT);
for (final Category child : subFamily) {
final List<Category> subsubFamily = createChildren(5, child, child.getName().get(Locale.ENGLISH), CTP_TARGET_CLIENT);
for (final Category subChild : subsubFamily) {
createChildren(4, subChild, subChild.getName().get(Locale.ENGLISH), CTP_TARGET_CLIENT);
}
}
// ---------------------------------------------------------------
// Create a total of 130 categories in the source project
final List<Category> sourceSubFamily = createChildren(5, null, "root", CTP_SOURCE_CLIENT);
for (final Category child : sourceSubFamily) {
final List<Category> subsubFamily = createChildren(5, sourceSubFamily.get(0), child.getName().get(Locale.ENGLISH), CTP_SOURCE_CLIENT);
for (final Category subChild : subsubFamily) {
createChildren(4, sourceSubFamily.get(0), subChild.getName().get(Locale.ENGLISH), CTP_SOURCE_CLIENT);
}
}
// ---------------------------------------------------------------
// Fetch categories from source project
final List<Category> categories = CTP_SOURCE_CLIENT.execute(CategoryQuery.of().withLimit(QueryExecutionUtils.DEFAULT_PAGE_SIZE)).toCompletableFuture().join().getResults();
final List<CategoryDraft> categoryDrafts = CategoryTransformUtils.toCategoryDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, categories).join();
Collections.shuffle(categoryDrafts);
CategorySync categorySyncWith13BatcheSize = new CategorySync(buildCategorySyncOptions(13));
final CategorySyncStatistics syncStatistics = categorySyncWith13BatcheSize.sync(categoryDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(130, 0, 120, 0, 0);
assertThat(callBackErrorResponses).isEmpty();
assertThat(callBackExceptions).isEmpty();
assertThat(callBackWarningResponses).isEmpty();
}
use of com.commercetools.sync.categories.CategorySync in project commercetools-sync-java by commercetools.
the class CategorySyncIT method syncDrafts_withANonExistingNewParent_ShouldUpdateCategories.
@Test
void syncDrafts_withANonExistingNewParent_ShouldUpdateCategories() {
// -----------------Test Setup------------------------------------
// Delete all categories in target project
deleteAllCategories(CTP_TARGET_CLIENT);
String parentKey = "parent";
// Create a total of 2 categories in the target project.
final CategoryDraft parentDraft = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "parent"), LocalizedString.of(Locale.ENGLISH, "parent")).key(parentKey).custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CATEGORY_CUSTOM_TYPE_KEY, createCustomFieldsJsonMap())).build();
CTP_TARGET_CLIENT.execute(CategoryCreateCommand.of(parentDraft)).toCompletableFuture().join();
final CategoryDraft childDraft = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "child"), LocalizedString.of(Locale.ENGLISH, "child")).key("child").parent(ResourceIdentifier.ofKey(parentKey)).custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CATEGORY_CUSTOM_TYPE_KEY, createCustomFieldsJsonMap())).build();
CTP_TARGET_CLIENT.execute(CategoryCreateCommand.of(childDraft)).toCompletableFuture().join();
// ------------------------------------------------------------------------------------------------------------
// Create a total of 2 categories in the source project
String newParentKey = "new-parent";
final CategoryDraft sourceParentDraft = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "new-parent"), LocalizedString.of(Locale.ENGLISH, "new-parent")).key(newParentKey).custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CATEGORY_CUSTOM_TYPE_KEY, createCustomFieldsJsonMap())).build();
CTP_SOURCE_CLIENT.execute(CategoryCreateCommand.of(sourceParentDraft)).toCompletableFuture().join();
final CategoryDraft sourceChildDraft = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "child-new-name"), LocalizedString.of(Locale.ENGLISH, "child")).key("child").parent(ResourceIdentifier.ofKey(newParentKey)).custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CATEGORY_CUSTOM_TYPE_KEY, createCustomFieldsJsonMap())).build();
CTP_SOURCE_CLIENT.execute(CategoryCreateCommand.of(sourceChildDraft)).toCompletableFuture().join();
// ---------------------------------------------------------------
// Fetch categories from source project
final List<Category> categories = CTP_SOURCE_CLIENT.execute(CategoryQuery.of().withSort(sorting -> sorting.createdAt().sort().asc())).toCompletableFuture().join().getResults();
final List<CategoryDraft> categoryDrafts = CategoryTransformUtils.toCategoryDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, categories).join();
// To simulate the new parent coming in a later draft
Collections.reverse(categoryDrafts);
CategorySync categorySyncWith1BatchSize = new CategorySync(buildCategorySyncOptions(13));
final CategorySyncStatistics syncStatistics = categorySyncWith1BatchSize.sync(categoryDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(2, 1, 1, 0, 0);
assertThat(callBackErrorResponses).isEmpty();
assertThat(callBackExceptions).isEmpty();
assertThat(callBackWarningResponses).isEmpty();
}
use of com.commercetools.sync.categories.CategorySync in project commercetools-project-sync by commercetools.
the class CategorySyncer method of.
@Nonnull
public static CategorySyncer of(@Nonnull final SphereClient sourceClient, @Nonnull final SphereClient targetClient, @Nonnull final Clock clock) {
final QuadConsumer<SyncException, Optional<CategoryDraft>, Optional<Category>, List<UpdateAction<Category>>> logErrorCallback = (exception, newResourceDraft, oldResource, updateActions) -> logErrorCallback(LOGGER, "category", exception, oldResource, updateActions);
final TriConsumer<SyncException, Optional<CategoryDraft>, Optional<Category>> logWarningCallback = (exception, newResourceDraft, oldResource) -> logWarningCallback(LOGGER, "category", exception, oldResource);
final CategorySyncOptions syncOptions = CategorySyncOptionsBuilder.of(targetClient).errorCallback(logErrorCallback).warningCallback(logWarningCallback).build();
final CategorySync categorySync = new CategorySync(syncOptions);
final CustomObjectService customObjectService = new CustomObjectServiceImpl(targetClient);
return new CategorySyncer(categorySync, sourceClient, targetClient, customObjectService, clock);
}
use of com.commercetools.sync.categories.CategorySync in project commercetools-sync-java by commercetools.
the class CategorySyncIT method syncDrafts_withNewShuffledBatchOfCategories_ShouldCreateCategories.
@Test
void syncDrafts_withNewShuffledBatchOfCategories_ShouldCreateCategories() {
// -----------------Test Setup------------------------------------
// Delete all categories in target project
deleteAllCategories(CTP_TARGET_CLIENT);
// Create a total of 130 categories in the source project
final List<Category> subFamily = createChildren(5, null, "root", CTP_SOURCE_CLIENT);
for (final Category child : subFamily) {
final List<Category> subsubFamily = createChildren(5, child, child.getName().get(Locale.ENGLISH), CTP_SOURCE_CLIENT);
for (final Category subChild : subsubFamily) {
createChildren(4, subChild, subChild.getName().get(Locale.ENGLISH), CTP_SOURCE_CLIENT);
}
}
// ---------------------------------------------------------------
// Fetch categories from source project
final List<Category> categories = CTP_SOURCE_CLIENT.execute(CategoryQuery.of().withLimit(QueryExecutionUtils.DEFAULT_PAGE_SIZE)).toCompletableFuture().join().getResults();
final List<CategoryDraft> categoryDrafts = CategoryTransformUtils.toCategoryDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, categories).join();
// Make sure there is no hierarchical order
Collections.shuffle(categoryDrafts);
CategorySync categorySyncWith13BatchSize = new CategorySync(buildCategorySyncOptions(13));
final CategorySyncStatistics syncStatistics = categorySyncWith13BatchSize.sync(categoryDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(130, 130, 0, 0, 0);
assertThat(callBackErrorResponses).isEmpty();
assertThat(callBackExceptions).isEmpty();
assertThat(callBackWarningResponses).isEmpty();
}
use of com.commercetools.sync.categories.CategorySync in project commercetools-sync-java by commercetools.
the class CategorySyncIT method syncDrafts_withExistingCategoriesThatChangeParents_ShouldUpdateCategories.
@Test
void syncDrafts_withExistingCategoriesThatChangeParents_ShouldUpdateCategories() {
// -----------------Test Setup------------------------------------
// Delete all categories in target project
deleteAllCategories(CTP_TARGET_CLIENT);
// Create a total of 3 categories in the target project (2 roots and 1 child to the first root)
final List<Category> subFamily = createChildren(2, null, "root", CTP_TARGET_CLIENT);
final Category firstRoot = subFamily.get(0);
createChildren(1, firstRoot, "child", CTP_TARGET_CLIENT);
// ---------------------------------------------------------------
// Create a total of 2 categories in the source project (2 roots and 1 child to the second root)
final List<Category> sourceSubFamily = createChildren(2, null, "root", CTP_SOURCE_CLIENT);
final Category secondRoot = sourceSubFamily.get(1);
createChildren(1, secondRoot, "child", CTP_SOURCE_CLIENT);
// ---------------------------------------------------------------
// Fetch categories from source project
final List<Category> categories = CTP_SOURCE_CLIENT.execute(CategoryQuery.of()).toCompletableFuture().join().getResults();
final List<CategoryDraft> categoryDrafts = CategoryTransformUtils.toCategoryDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, categories).join();
Collections.shuffle(categoryDrafts);
CategorySync categorySyncWith1BatchSize = new CategorySync(buildCategorySyncOptions(1));
final CategorySyncStatistics syncStatistics = categorySyncWith1BatchSize.sync(categoryDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(3, 0, 1, 0, 0);
assertThat(callBackErrorResponses).isEmpty();
assertThat(callBackExceptions).isEmpty();
assertThat(callBackWarningResponses).isEmpty();
}
Aggregations