use of com.commercetools.sync.services.CartDiscountService 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.services.CartDiscountService 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.services.CartDiscountService 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());
}
use of com.commercetools.sync.services.CartDiscountService in project commercetools-sync-java by commercetools.
the class CartDiscountServiceImplTest method createCartDiscount_WithNullCartDiscountKey_ShouldNotCreateCartDiscount.
@Test
void createCartDiscount_WithNullCartDiscountKey_ShouldNotCreateCartDiscount() {
// preparation
final CartDiscountDraft mockCartDiscountDraft = mock(CartDiscountDraft.class);
final Map<String, Throwable> errors = new HashMap<>();
when(mockCartDiscountDraft.getKey()).thenReturn(null);
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);
// test
final CompletionStage<Optional<CartDiscount>> result = cartDiscountService.createCartDiscount(mockCartDiscountDraft);
// assertions
assertThat(result).isCompletedWithValue(Optional.empty());
assertThat(errors.keySet()).containsExactly("Failed to create draft with key: 'null'. Reason: Draft key is blank!");
verify(cartDiscountSyncOptions.getCtpClient(), times(0)).execute(any());
}
use of com.commercetools.sync.services.CartDiscountService in project commercetools-sync-java by commercetools.
the class CartDiscountServiceImplTest method createCartDiscount_WithEmptyCartDiscountKey_ShouldHaveEmptyOptionalAsAResult.
@Test
void createCartDiscount_WithEmptyCartDiscountKey_ShouldHaveEmptyOptionalAsAResult() {
// preparation
final SphereClient sphereClient = mock(SphereClient.class);
final CartDiscountDraft mockCartDiscountDraft = mock(CartDiscountDraft.class);
final Map<String, Throwable> errors = new HashMap<>();
when(mockCartDiscountDraft.getKey()).thenReturn("");
final CartDiscountSyncOptions options = CartDiscountSyncOptionsBuilder.of(sphereClient).errorCallback((exception, oldResource, newResource, actions) -> errors.put(exception.getMessage(), exception)).build();
final CartDiscountServiceImpl cartDiscountService = new CartDiscountServiceImpl(options);
// test
final CompletionStage<Optional<CartDiscount>> result = cartDiscountService.createCartDiscount(mockCartDiscountDraft);
// assertion
assertThat(result).isCompletedWithValue(Optional.empty());
assertThat(errors.keySet()).containsExactly("Failed to create draft with key: ''. Reason: Draft key is blank!");
verify(options.getCtpClient(), times(0)).execute(any());
}
Aggregations