use of com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions in project commercetools-sync-java by commercetools.
the class CartDiscountSyncIT method sync_WithUpdatedCartDiscount_WithNewCustomType_ShouldUpdateCartDiscountWithNewCustomType.
@Test
void sync_WithUpdatedCartDiscount_WithNewCustomType_ShouldUpdateCartDiscountWithNewCustomType() {
// preparation
final Type newCustomType = createCartDiscountCustomType("new-type", Locale.ENGLISH, "new-type", CTP_TARGET_CLIENT);
final CartDiscountDraft newCartDiscountDraftWithExistingKey = CartDiscountDraftBuilder.of(CART_DISCOUNT_DRAFT_1).custom(CustomFieldsDraft.ofTypeKeyAndJson(newCustomType.getKey(), emptyMap())).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final List<UpdateAction<CartDiscount>> updateActionsList = new ArrayList<>();
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).beforeUpdateCallback((updateActions, newCartDiscount, oldCartDiscount) -> {
updateActionsList.addAll(updateActions);
return updateActions;
}).build();
final CartDiscountSync cartDiscountSync = new CartDiscountSync(cartDiscountSyncOptions);
// test
final CartDiscountSyncStatistics cartDiscountSyncStatistics = cartDiscountSync.sync(singletonList(newCartDiscountDraftWithExistingKey)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(updateActionsList).containsExactly(SetCustomType.ofTypeIdAndJson(newCustomType.getId(), emptyMap()));
assertThat(cartDiscountSyncStatistics.getReportMessage()).isEqualTo("Summary: 1 cart discounts were processed in total" + " (0 created, 1 updated and 0 failed to sync).");
assertThat(cartDiscountSyncStatistics).hasValues(1, 0, 1, 0);
}
use of com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions in project commercetools-sync-java by commercetools.
the class CartDiscountSyncIT method sync_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate.
@Test
void sync_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate() {
// preparation
final SphereClient spyClient = buildClientWithConcurrentModificationUpdateAndFailedFetchOnRetry();
final CartDiscountDraft draft2 = CartDiscountDraftBuilder.of(CART_DISCOUNT_DRAFT_2).custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CART_DISCOUNT_TYPE_KEY, createCustomFieldsJsonMap())).build();
CTP_TARGET_CLIENT.execute(CartDiscountCreateCommand.of(draft2)).toCompletableFuture().join();
final CartDiscountDraft updatedDraft = CartDiscountDraftBuilder.of(CART_DISCOUNT_DRAFT_2).description(CART_DISCOUNT_DESC_1).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
final CartDiscountSync cartDiscountSync = new CartDiscountSync(cartDiscountSyncOptions);
// test
final CartDiscountSyncStatistics statistics = cartDiscountSync.sync(singletonList(updatedDraft)).toCompletableFuture().join();
// assertion
assertThat(statistics).hasValues(1, 0, 0, 1);
assertThat(errorMessages).hasSize(1);
assertThat(exceptions).hasSize(1);
assertThat(exceptions.get(0).getCause()).hasCauseExactlyInstanceOf(BadGatewayException.class);
assertThat(errorMessages.get(0)).contains(format("Failed to update cart discount with key: '%s'. Reason: Failed to fetch from CTP while retrying " + "after concurrency modification.", CART_DISCOUNT_KEY_2));
}
use of com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions in project commercetools-sync-java by commercetools.
the class CartDiscountSyncIT method sync_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate.
@Test
void sync_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate() {
// preparation
final SphereClient spyClient = buildClientWithConcurrentModificationUpdateAndNotFoundFetchOnRetry();
final CartDiscountDraft draft2 = CartDiscountDraftBuilder.of(CART_DISCOUNT_DRAFT_2).custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CART_DISCOUNT_TYPE_KEY, createCustomFieldsJsonMap())).build();
CTP_TARGET_CLIENT.execute(CartDiscountCreateCommand.of(draft2)).toCompletableFuture().join();
final CartDiscountDraft updatedDraft = CartDiscountDraftBuilder.of(CART_DISCOUNT_DRAFT_2).description(CART_DISCOUNT_DESC_1).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
final CartDiscountSync cartDiscountSync = new CartDiscountSync(cartDiscountSyncOptions);
// test
final CartDiscountSyncStatistics statistics = cartDiscountSync.sync(singletonList(updatedDraft)).toCompletableFuture().join();
// Assertion
assertThat(statistics).hasValues(1, 0, 0, 1);
assertThat(errorMessages).hasSize(1);
assertThat(exceptions).hasSize(1);
assertThat(errorMessages.get(0)).contains(format("Failed to update cart discount with key: '%s'. Reason: Not found when attempting to fetch while " + "retrying after concurrency modification.", CART_DISCOUNT_KEY_2));
}
use of com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions in project commercetools-sync-java by commercetools.
the class CartDiscountSyncIT method sync_WithUpdatedCartDiscount_WithNonExistingResIdentifier_ShouldFailToResolveReference.
@Test
void sync_WithUpdatedCartDiscount_WithNonExistingResIdentifier_ShouldFailToResolveReference() {
// preparation
final CartDiscountDraft newCartDiscountDraftWithExistingKey = CartDiscountDraftBuilder.of(CART_DISCOUNT_DRAFT_1).custom(CustomFieldsDraft.ofTypeKeyAndJson("not-existing-key", emptyMap())).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
final CartDiscountSync cartDiscountSync = new CartDiscountSync(cartDiscountSyncOptions);
// test
final CartDiscountSyncStatistics cartDiscountSyncStatistics = cartDiscountSync.sync(singletonList(newCartDiscountDraftWithExistingKey)).toCompletableFuture().join();
// assertions
assertThat(cartDiscountSyncStatistics).hasValues(1, 0, 0, 1);
assertThat(errorMessages).containsExactly("Failed to process the CartDiscountDraft with key:'key_1'. Reason: " + ReferenceResolutionException.class.getCanonicalName() + ": " + "Failed to resolve custom type reference on CartDiscountDraft with key:'key_1'. " + "Reason: Type with key 'not-existing-key' doesn't exist.");
}
use of com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions in project commercetools-sync-java by commercetools.
the class CartDiscountSyncIT method sync_WithoutValue_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithoutValue_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// prepare
// Draft without "value" throws a commercetools exception because "value" is a required value
final CartDiscountDraft newCartDiscountDraftWithoutValue = CartDiscountDraftBuilder.of(CART_DISCOUNT_NAME_1, CART_DISCOUNT_CART_PREDICATE_1, null, CART_DISCOUNT_TARGET_1, SORT_ORDER_1, false).key(CART_DISCOUNT_KEY_1).active(false).description(CART_DISCOUNT_DESC_1).validFrom(JANUARY_FROM).validUntil(JANUARY_UNTIL).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
final CartDiscountSync cartDiscountSync = new CartDiscountSync(cartDiscountSyncOptions);
// test
final CartDiscountSyncStatistics cartDiscountSyncStatistics = cartDiscountSync.sync(singletonList(newCartDiscountDraftWithoutValue)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to update cart discount with key: 'key_1'.");
assertThat(exceptions).hasSize(1).singleElement().matches(throwable -> {
assertThat(throwable).isExactlyInstanceOf(SyncException.class);
assertThat(throwable).hasCauseExactlyInstanceOf(CompletionException.class);
assertThat(throwable.getCause()).hasCauseExactlyInstanceOf(ErrorResponseException.class);
assertThat(throwable.getCause()).hasMessageContaining("value: Missing required value");
return true;
});
assertThat(cartDiscountSyncStatistics).hasValues(1, 0, 0, 1);
}
Aggregations