use of com.commercetools.sync.categories.helpers.CategorySyncStatistics in project commercetools-sync-java by commercetools.
the class CategorySyncIT method syncDrafts_WithMultipleBatchSyncing_ShouldSync.
@Test
void syncDrafts_WithMultipleBatchSyncing_ShouldSync() {
// Existing array of [1, 2, 3, oldCategoryKey]
final CategoryDraft oldCategoryDraft1 = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "cat1"), LocalizedString.of(Locale.ENGLISH, "furniture1")).key("cat1").custom(getCustomFieldsDraft()).build();
CTP_TARGET_CLIENT.execute(CategoryCreateCommand.of(oldCategoryDraft1)).toCompletableFuture().join();
final CategoryDraft oldCategoryDraft2 = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "cat2"), LocalizedString.of(Locale.ENGLISH, "furniture2")).key("cat2").custom(getCustomFieldsDraft()).build();
CTP_TARGET_CLIENT.execute(CategoryCreateCommand.of(oldCategoryDraft2)).toCompletableFuture().join();
final CategoryDraft oldCategoryDraft3 = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "cat3"), LocalizedString.of(Locale.ENGLISH, "furniture3")).key("cat3").custom(getCustomFieldsDraft()).build();
CTP_TARGET_CLIENT.execute(CategoryCreateCommand.of(oldCategoryDraft3)).toCompletableFuture().join();
// _-----_-----_-----_-----_-----_PREPARE BATCHES FROM EXTERNAL
// SOURCE-----_-----_-----_-----_-----_-----
// _-----_-----_-----_-----_-----_-----_-----_-----_-----_-----_-----_-----_-----_-----_-----_-----_-----
// Category draft coming from external source.
final CategoryDraft categoryDraft1 = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "oldCategoryKey"), LocalizedString.of(Locale.ENGLISH, "modern-furniture")).key(oldCategoryKey).parent(ResourceIdentifier.ofKey("cat7")).custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CATEGORY_CUSTOM_TYPE_KEY, createCustomFieldsJsonMap())).build();
final List<CategoryDraft> batch1 = new ArrayList<>();
batch1.add(categoryDraft1);
final CategoryDraft categoryDraft2 = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "cat7"), LocalizedString.of(Locale.ENGLISH, "modern-furniture1")).key("cat7").custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CATEGORY_CUSTOM_TYPE_KEY, createCustomFieldsJsonMap())).build();
final List<CategoryDraft> batch2 = new ArrayList<>();
batch2.add(categoryDraft2);
final CategoryDraft categoryDraft3 = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "cat6"), LocalizedString.of(Locale.ENGLISH, "modern-furniture2")).key("cat6").parent(ResourceIdentifier.ofKey("cat5")).custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CATEGORY_CUSTOM_TYPE_KEY, createCustomFieldsJsonMap())).build();
final List<CategoryDraft> batch3 = new ArrayList<>();
batch3.add(categoryDraft3);
final CategoryDraft categoryDraft4 = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "cat5"), LocalizedString.of(Locale.ENGLISH, "modern-furniture3")).key("cat5").custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CATEGORY_CUSTOM_TYPE_KEY, createCustomFieldsJsonMap())).build();
final List<CategoryDraft> batch4 = new ArrayList<>();
batch4.add(categoryDraft4);
final CategorySyncStatistics syncStatistics = categorySync.sync(batch1).thenCompose(result -> categorySync.sync(batch2)).thenCompose(result -> categorySync.sync(batch3)).thenCompose(result -> categorySync.sync(batch4)).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(4, 3, 1, 0, 0);
}
use of com.commercetools.sync.categories.helpers.CategorySyncStatistics in project commercetools-sync-java by commercetools.
the class CategorySyncIT method syncDrafts_WithValidCustomFieldsChange_ShouldSyncIt.
@Test
void syncDrafts_WithValidCustomFieldsChange_ShouldSyncIt() {
final List<CategoryDraft> newCategoryDrafts = new ArrayList<>();
final Map<String, JsonNode> customFieldsJsons = new HashMap<>();
customFieldsJsons.put(BOOLEAN_CUSTOM_FIELD_NAME, JsonNodeFactory.instance.booleanNode(false));
customFieldsJsons.put(LOCALISED_STRING_CUSTOM_FIELD_NAME, JsonNodeFactory.instance.objectNode().put("de", "rot").put("en", "red").put("it", "rosso"));
final CategoryDraft categoryDraft1 = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "Modern Furniture"), LocalizedString.of(Locale.ENGLISH, "modern-furniture")).key(oldCategoryKey).custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CATEGORY_CUSTOM_TYPE_KEY, customFieldsJsons)).build();
newCategoryDrafts.add(categoryDraft1);
final CategorySyncStatistics syncStatistics = categorySync.sync(newCategoryDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(1, 0, 1, 0, 0);
}
use of com.commercetools.sync.categories.helpers.CategorySyncStatistics in project commercetools-sync-java by commercetools.
the class CategorySyncIT method syncDrafts_WithConcurrentModificationException_ShouldRetryToUpdateNewCategoryWithSuccess.
@Test
void syncDrafts_WithConcurrentModificationException_ShouldRetryToUpdateNewCategoryWithSuccess() {
// Preparation
final SphereClient spyClient = buildClientWithConcurrentModificationUpdate();
final LocalizedString newCategoryName = LocalizedString.of(Locale.ENGLISH, "Modern Furniture");
final CategoryDraft categoryDraft = CategoryDraftBuilder.of(newCategoryName, LocalizedString.of(Locale.ENGLISH, "modern-furniture")).key(oldCategoryKey).custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CATEGORY_CUSTOM_TYPE_KEY, createCustomFieldsJsonMap())).build();
final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(spyClient).build();
final CategorySync categorySync = new CategorySync(categorySyncOptions);
// Test
final CategorySyncStatistics statistics = categorySync.sync(Collections.singletonList(categoryDraft)).toCompletableFuture().join();
// Assertion
assertThat(statistics).hasValues(1, 0, 1, 0);
// Assert CTP state.
final PagedQueryResult<Category> queryResult = CTP_TARGET_CLIENT.execute(CategoryQuery.of().plusPredicates(categoryQueryModel -> categoryQueryModel.key().is(categoryDraft.getKey()))).toCompletableFuture().join();
assertThat(queryResult.head()).hasValueSatisfying(category -> assertThat(category.getName()).isEqualTo(newCategoryName));
}
use of com.commercetools.sync.categories.helpers.CategorySyncStatistics in project commercetools-sync-java by commercetools.
the class CategorySyncIT method syncDraft_withARemovedCustomType_ShouldUpdateCategory.
@Test
void syncDraft_withARemovedCustomType_ShouldUpdateCategory() {
// Category draft coming from external source.
final CategoryDraft categoryDraft = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "furniture"), LocalizedString.of(Locale.ENGLISH, "furniture")).key(oldCategoryKey).build();
final CategorySyncStatistics syncStatistics = categorySync.sync(Collections.singletonList(categoryDraft)).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(1, 0, 1, 0, 0);
}
use of com.commercetools.sync.categories.helpers.CategorySyncStatistics in project commercetools-sync-java by commercetools.
the class CategorySyncIT method syncDrafts_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate.
@Test
void syncDrafts_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate() {
// Preparation
final SphereClient spyClient = buildClientWithConcurrentModificationUpdateAndFailedFetchOnRetry();
final CategoryDraft categoryDraft = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "Modern Furniture"), LocalizedString.of(Locale.ENGLISH, "modern-furniture")).key(oldCategoryKey).custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CATEGORY_CUSTOM_TYPE_KEY, createCustomFieldsJsonMap())).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> errors = new ArrayList<>();
final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
errors.add(exception.getCause());
}).build();
final CategorySync categorySync = new CategorySync(categorySyncOptions);
final CategorySyncStatistics statistics = categorySync.sync(Collections.singletonList(categoryDraft)).toCompletableFuture().join();
// Test and assertion
assertThat(statistics).hasValues(1, 0, 0, 1);
assertThat(errorMessages).hasSize(1);
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getCause()).isExactlyInstanceOf(BadGatewayException.class);
assertThat(errorMessages.get(0)).contains("Reason: Failed to fetch from CTP while retrying after concurrency modification.");
}
Aggregations