use of com.commercetools.sync.categories.helpers.CategorySyncStatistics 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.helpers.CategorySyncStatistics in project commercetools-sync-java by commercetools.
the class CategorySyncIT method syncDrafts_withNewCategories_ShouldCreateCategories.
@Test
void syncDrafts_withNewCategories_ShouldCreateCategories() {
createCategories(CTP_SOURCE_CLIENT, getCategoryDraftsWithPrefix(Locale.ENGLISH, "new", null, 3));
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();
final CategorySyncStatistics syncStatistics = categorySync.sync(categoryDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(3, 1, 2, 0, 0);
assertThat(callBackErrorResponses).isEmpty();
assertThat(callBackExceptions).isEmpty();
assertThat(callBackWarningResponses).isEmpty();
}
use of com.commercetools.sync.categories.helpers.CategorySyncStatistics 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.helpers.CategorySyncStatistics in project commercetools-sync-java by commercetools.
the class CategorySyncIT method syncDrafts_fromCategoriesWithoutKeys_ShouldNotUpdateCategories.
@Test
void syncDrafts_fromCategoriesWithoutKeys_ShouldNotUpdateCategories() {
final CategoryDraft oldCategoryDraft1 = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "cat1"), LocalizedString.of(Locale.ENGLISH, "furniture1")).custom(getCustomFieldsDraft()).key("newKey1").build();
final CategoryDraft oldCategoryDraft2 = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "cat2"), LocalizedString.of(Locale.ENGLISH, "furniture2")).custom(getCustomFieldsDraft()).key("newKey2").build();
// Create two categories in the source with Keys.
List<CompletableFuture<Category>> futureCreations = new ArrayList<>();
futureCreations.add(CTP_SOURCE_CLIENT.execute(CategoryCreateCommand.of(oldCategoryDraft1)).toCompletableFuture());
futureCreations.add(CTP_SOURCE_CLIENT.execute(CategoryCreateCommand.of(oldCategoryDraft2)).toCompletableFuture());
CompletableFuture.allOf(futureCreations.toArray(new CompletableFuture[futureCreations.size()])).join();
// Create two categories in the target without Keys.
futureCreations = new ArrayList<>();
final CategoryDraft newCategoryDraft1 = CategoryDraftBuilder.of(oldCategoryDraft1).key(null).build();
final CategoryDraft newCategoryDraft2 = CategoryDraftBuilder.of(oldCategoryDraft2).key(null).build();
futureCreations.add(CTP_TARGET_CLIENT.execute(CategoryCreateCommand.of(newCategoryDraft1)).toCompletableFuture());
futureCreations.add(CTP_TARGET_CLIENT.execute(CategoryCreateCommand.of(newCategoryDraft2)).toCompletableFuture());
CompletableFuture.allOf(futureCreations.toArray(new CompletableFuture[futureCreations.size()])).join();
// ---------
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();
final CategorySyncStatistics syncStatistics = categorySync.sync(categoryDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(2, 0, 0, 2, 0);
assertThat(callBackErrorResponses).hasSize(2).allSatisfy(errorMessage -> {
assertThat(errorMessage).contains("\"code\" : \"DuplicateField\"");
assertThat(errorMessage).contains("\"field\" : \"slug.en\"");
});
assertThat(callBackExceptions).hasSize(2).allSatisfy(exception -> {
assertThat(exception).isExactlyInstanceOf(ErrorResponseException.class);
final ErrorResponseException errorResponse = ((ErrorResponseException) exception);
final List<DuplicateFieldError> fieldErrors = errorResponse.getErrors().stream().map(sphereError -> {
assertThat(sphereError.getCode()).isEqualTo(DuplicateFieldError.CODE);
return sphereError.as(DuplicateFieldError.class);
}).collect(toList());
assertThat(fieldErrors).hasSize(1);
assertThat(fieldErrors).allSatisfy(error -> assertThat(error.getField()).isEqualTo("slug.en"));
});
assertThat(callBackWarningResponses).isEmpty();
}
use of com.commercetools.sync.categories.helpers.CategorySyncStatistics in project commercetools-sync-java by commercetools.
the class CategorySyncTest method sync_WithExistingCategoryButWithNullParentReference_ShouldFailSync.
@Test
void sync_WithExistingCategoryButWithNullParentReference_ShouldFailSync() {
final Category mockCategory = getMockCategory(UUID.randomUUID().toString(), "key");
final CategoryService mockCategoryService = mockCategoryService(singleton(mockCategory), null, mockCategory);
final CategorySync categorySync = new CategorySync(categorySyncOptions, getMockTypeService(), mockCategoryService, mockUnresolvedReferencesService);
final List<CategoryDraft> categoryDrafts = singletonList(getMockCategoryDraft(Locale.ENGLISH, "name", "key", null, "customTypeId", new HashMap<>()));
final CategorySyncStatistics syncStatistics = categorySync.sync(categoryDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(1, 0, 0, 1);
assertThat(errorCallBackMessages).hasSize(1);
assertThat(errorCallBackMessages.get(0)).contains(BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER);
assertThat(errorCallBackExceptions).hasSize(1);
}
Aggregations