use of com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions in project commercetools-sync-java by commercetools.
the class CartDiscountSyncIT method sync_WithNewCartDiscount_ShouldCreateNewDiscount.
@Test
void sync_WithNewCartDiscount_ShouldCreateNewDiscount() {
// preparation
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
final CartDiscountSync cartDiscountSync = new CartDiscountSync(cartDiscountSyncOptions);
// test
final CartDiscountSyncStatistics cartDiscountSyncStatistics = cartDiscountSync.sync(singletonList(CART_DISCOUNT_DRAFT_2)).toCompletableFuture().join();
// assertions
assertThat(cartDiscountSyncStatistics).hasValues(1, 1, 0, 0);
final Optional<CartDiscount> cartDiscountAfterCreation = getCartDiscountByKey(CTP_TARGET_CLIENT, CART_DISCOUNT_KEY_2);
assertThat(cartDiscountAfterCreation).hasValueSatisfying(cartDiscount -> {
assertThat(cartDiscount.getName()).isEqualTo(CART_DISCOUNT_DRAFT_2.getName());
assertThat(cartDiscount.getDescription()).isEqualTo(CART_DISCOUNT_DRAFT_2.getDescription());
assertThat(cartDiscount.getCartPredicate()).isEqualTo(CART_DISCOUNT_DRAFT_2.getCartPredicate());
assertThat(cartDiscount.getValue()).isEqualTo(CART_DISCOUNT_DRAFT_2.getValue());
});
}
use of com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions in project commercetools-sync-java by commercetools.
the class CartDiscountSyncIT method sync_WithUpdatedCartDiscount_WithNewValue_ShouldUpdateCartDiscountWithNewValue.
@Test
void sync_WithUpdatedCartDiscount_WithNewValue_ShouldUpdateCartDiscountWithNewValue() {
// preparation
final CartDiscountDraft newCartDiscountDraftWithExistingKey = CartDiscountDraftBuilder.of(CART_DISCOUNT_DRAFT_1).value(CART_DISCOUNT_VALUE_2).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(ChangeValue.of(CART_DISCOUNT_VALUE_2));
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 CartDiscountServiceImplTest method createCartDiscount_WithUnsuccessfulMockCtpResponse_ShouldNotCreateCartDiscount.
@Test
void createCartDiscount_WithUnsuccessfulMockCtpResponse_ShouldNotCreateCartDiscount() {
// preparation
final CartDiscountDraft mockCartDiscountDraft = mock(CartDiscountDraft.class);
final Map<String, Throwable> errors = new HashMap<>();
when(mockCartDiscountDraft.getKey()).thenReturn("cartDiscountKey");
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, actions) -> errors.put(exception.getMessage(), exception)).build();
final CartDiscountService cartDiscountService = new CartDiscountServiceImpl(cartDiscountSyncOptions);
when(cartDiscountSyncOptions.getCtpClient().execute(any())).thenReturn(CompletableFutureUtils.failed(new InternalServerErrorException()));
// test
final CompletionStage<Optional<CartDiscount>> result = cartDiscountService.createCartDiscount(mockCartDiscountDraft);
// assertions
assertThat(result).isCompletedWithValue(Optional.empty());
assertThat(errors.keySet()).hasSize(1).singleElement(as(STRING)).contains("Failed to create draft with key: 'cartDiscountKey'.");
assertThat(errors.values()).hasSize(1).singleElement().matches(exception -> {
assertThat(exception).isExactlyInstanceOf(SyncException.class);
assertThat(exception.getCause()).isExactlyInstanceOf(InternalServerErrorException.class);
return true;
});
}
use of com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions in project commercetools-sync-java by commercetools.
the class CartDiscountServiceImplTest method fetchCartDiscount_WithNullKey_ShouldNotFetchAnyCartDiscount.
@Test
void fetchCartDiscount_WithNullKey_ShouldNotFetchAnyCartDiscount() {
// preparation
final SphereClient sphereClient = mock(SphereClient.class);
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(sphereClient).build();
final CartDiscountService cartDiscountService = new CartDiscountServiceImpl(cartDiscountSyncOptions);
// test
final CompletionStage<Optional<CartDiscount>> result = cartDiscountService.fetchCartDiscount(null);
// assertions
assertThat(result).isCompletedWithValue(Optional.empty());
verify(sphereClient, never()).execute(any());
}
use of com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions in project commercetools-sync-java by commercetools.
the class CartDiscountServiceImplTest method fetchCartDiscount_WithValidKey_ShouldReturnMockCartDiscount.
@Test
void fetchCartDiscount_WithValidKey_ShouldReturnMockCartDiscount() {
// preparation
final SphereClient sphereClient = mock(SphereClient.class);
final CartDiscount mockCartDiscount = mock(CartDiscount.class);
when(mockCartDiscount.getId()).thenReturn("testId");
when(mockCartDiscount.getKey()).thenReturn("any_key");
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(sphereClient).build();
final CartDiscountService cartDiscountService = new CartDiscountServiceImpl(cartDiscountSyncOptions);
@SuppressWarnings("unchecked") final PagedQueryResult<CartDiscount> pagedQueryResult = mock(PagedQueryResult.class);
when(pagedQueryResult.head()).thenReturn(Optional.of(mockCartDiscount));
when(cartDiscountSyncOptions.getCtpClient().execute(any(CartDiscountQuery.class))).thenReturn(completedFuture(pagedQueryResult));
// test
final CompletionStage<Optional<CartDiscount>> result = cartDiscountService.fetchCartDiscount("any_key");
// assertions
assertThat(result).isCompletedWithValue(Optional.of(mockCartDiscount));
verify(sphereClient, only()).execute(any());
}
Aggregations