Search in sources :

Example 6 with CategorySync

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();
}
Also used : Category(io.sphere.sdk.categories.Category) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategorySync(com.commercetools.sync.categories.CategorySync) CategorySyncStatistics(com.commercetools.sync.categories.helpers.CategorySyncStatistics) Test(org.junit.jupiter.api.Test)

Example 7 with CategorySync

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();
}
Also used : Category(io.sphere.sdk.categories.Category) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) LocalizedString(io.sphere.sdk.models.LocalizedString) CategorySync(com.commercetools.sync.categories.CategorySync) CategorySyncStatistics(com.commercetools.sync.categories.helpers.CategorySyncStatistics) Test(org.junit.jupiter.api.Test)

Example 8 with CategorySync

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);
}
Also used : CategorySyncStatistics(com.commercetools.sync.categories.helpers.CategorySyncStatistics) SyncException(com.commercetools.sync.commons.exceptions.SyncException) LoggerFactory(org.slf4j.LoggerFactory) UpdateAction(io.sphere.sdk.commands.UpdateAction) QuadConsumer(com.commercetools.sync.commons.utils.QuadConsumer) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) SyncUtils.logWarningCallback(com.commercetools.project.sync.util.SyncUtils.logWarningCallback) SphereClient(io.sphere.sdk.client.SphereClient) CategorySync(com.commercetools.sync.categories.CategorySync) TriConsumer(com.commercetools.sync.commons.utils.TriConsumer) Nonnull(javax.annotation.Nonnull) SyncUtils.logErrorCallback(com.commercetools.project.sync.util.SyncUtils.logErrorCallback) Logger(org.slf4j.Logger) Category(io.sphere.sdk.categories.Category) CustomObjectService(com.commercetools.project.sync.service.CustomObjectService) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Syncer(com.commercetools.project.sync.Syncer) CategoryTransformUtils.toCategoryDrafts(com.commercetools.sync.categories.utils.CategoryTransformUtils.toCategoryDrafts) Clock(java.time.Clock) Optional(java.util.Optional) CustomObjectServiceImpl(com.commercetools.project.sync.service.impl.CustomObjectServiceImpl) CustomObjectService(com.commercetools.project.sync.service.CustomObjectService) Category(io.sphere.sdk.categories.Category) Optional(java.util.Optional) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) CustomObjectServiceImpl(com.commercetools.project.sync.service.impl.CustomObjectServiceImpl) List(java.util.List) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CategorySync(com.commercetools.sync.categories.CategorySync) Nonnull(javax.annotation.Nonnull)

Example 9 with CategorySync

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();
}
Also used : Category(io.sphere.sdk.categories.Category) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategorySync(com.commercetools.sync.categories.CategorySync) CategorySyncStatistics(com.commercetools.sync.categories.helpers.CategorySyncStatistics) Test(org.junit.jupiter.api.Test)

Example 10 with CategorySync

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();
}
Also used : Category(io.sphere.sdk.categories.Category) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategorySync(com.commercetools.sync.categories.CategorySync) CategorySyncStatistics(com.commercetools.sync.categories.helpers.CategorySyncStatistics) Test(org.junit.jupiter.api.Test)

Aggregations

CategorySync (com.commercetools.sync.categories.CategorySync)10 CategorySyncStatistics (com.commercetools.sync.categories.helpers.CategorySyncStatistics)9 Category (io.sphere.sdk.categories.Category)9 CategoryDraft (io.sphere.sdk.categories.CategoryDraft)9 Test (org.junit.jupiter.api.Test)8 CategorySyncOptions (com.commercetools.sync.categories.CategorySyncOptions)5 CategorySyncOptionsBuilder (com.commercetools.sync.categories.CategorySyncOptionsBuilder)5 CategoryQuery (io.sphere.sdk.categories.queries.CategoryQuery)5 SphereClient (io.sphere.sdk.client.SphereClient)5 LocalizedString (io.sphere.sdk.models.LocalizedString)5 List (java.util.List)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)4 TYPE_DOES_NOT_EXIST (com.commercetools.sync.commons.helpers.CustomReferenceResolver.TYPE_DOES_NOT_EXIST)4 OLD_CATEGORY_CUSTOM_TYPE_KEY (com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY)4 OLD_CATEGORY_CUSTOM_TYPE_NAME (com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME)4 CategoryITUtils.createCategoriesCustomType (com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType)4 CategoryITUtils.deleteAllCategories (com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories)4 CategoryITUtils.getCustomFieldsDraft (com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCustomFieldsDraft)4 BOOLEAN_CUSTOM_FIELD_NAME (com.commercetools.sync.integration.commons.utils.ITUtils.BOOLEAN_CUSTOM_FIELD_NAME)4