use of com.commercetools.sync.services.CartDiscountService in project commercetools-sync-java by commercetools.
the class CartDiscountSyncTest method sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final CartDiscountSyncOptions syncOptions = CartDiscountSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
final CartDiscountService mockCartDiscountService = mock(CartDiscountService.class);
when(mockCartDiscountService.fetchMatchingCartDiscountsByKeys(singleton(KEY))).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final CartDiscountSync cartDiscountSync = new CartDiscountSync(syncOptions, getMockTypeService(), mockCartDiscountService);
// test
final CartDiscountSyncStatistics cartDiscountSyncStatistics = cartDiscountSync.sync(singletonList(newCartDiscount)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo(format("Failed to fetch existing cart discounts with keys: '[%s]'.", KEY));
assertThat(exceptions).hasSize(1).singleElement().matches(throwable -> {
assertThat(throwable).isExactlyInstanceOf(SyncException.class);
assertThat(throwable).hasCauseExactlyInstanceOf(CompletionException.class);
assertThat(throwable.getCause()).hasCauseExactlyInstanceOf(SphereException.class);
return true;
});
assertThat(cartDiscountSyncStatistics).hasValues(1, 0, 0, 1);
}
use of com.commercetools.sync.services.CartDiscountService in project commercetools-sync-java by commercetools.
the class CartDiscountSyncTest method sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback.
@Test
void sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback() {
// preparation
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(mock(SphereClient.class)).build();
final CartDiscount mockedExistingCartDiscount = mock(CartDiscount.class);
when(mockedExistingCartDiscount.getKey()).thenReturn(newCartDiscount.getKey());
final CartDiscountService cartDiscountService = mock(CartDiscountService.class);
when(cartDiscountService.fetchMatchingCartDiscountsByKeys(anySet())).thenReturn(completedFuture(singleton(mockedExistingCartDiscount)));
when(cartDiscountService.updateCartDiscount(any(), any())).thenReturn(completedFuture(mockedExistingCartDiscount));
final CartDiscountSyncOptions spyCartDiscountSyncOptions = spy(cartDiscountSyncOptions);
// test
new CartDiscountSync(spyCartDiscountSyncOptions, getMockTypeService(), cartDiscountService).sync(singletonList(newCartDiscount)).toCompletableFuture().join();
// assertion
verify(spyCartDiscountSyncOptions).applyBeforeUpdateCallback(any(), any(), any());
verify(spyCartDiscountSyncOptions, never()).applyBeforeCreateCallback(newCartDiscount);
}
use of com.commercetools.sync.services.CartDiscountService in project commercetools-sync-java by commercetools.
the class CartDiscountServiceImplTest method updateCartDiscount_WithMockUnsuccessfulCtpResponse_ShouldCompleteExceptionally.
@Test
void updateCartDiscount_WithMockUnsuccessfulCtpResponse_ShouldCompleteExceptionally() {
// preparation
final CartDiscount mockCartDiscount = mock(CartDiscount.class);
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(mock(SphereClient.class)).build();
when(cartDiscountSyncOptions.getCtpClient().execute(any())).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new InternalServerErrorException()));
final CartDiscountService cartDiscountService = new CartDiscountServiceImpl(cartDiscountSyncOptions);
final List<UpdateAction<CartDiscount>> updateActions = singletonList(SetDescription.of(LocalizedString.ofEnglish("new_desc")));
// test
final CompletionStage<CartDiscount> result = cartDiscountService.updateCartDiscount(mockCartDiscount, updateActions);
// assertions
assertThat(result).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(InternalServerErrorException.class);
}
use of com.commercetools.sync.services.CartDiscountService in project commercetools-sync-java by commercetools.
the class CartDiscountServiceImplTest method fetchCartDiscount_WithEmptyKey_ShouldNotFetchAnyCartDiscount.
@Test
void fetchCartDiscount_WithEmptyKey_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("");
// 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 CartDiscountSyncTest method sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback.
@Test
void sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback() {
// preparation
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(mock(SphereClient.class)).build();
final CartDiscountService cartDiscountService = mock(CartDiscountService.class);
when(cartDiscountService.fetchMatchingCartDiscountsByKeys(anySet())).thenReturn(completedFuture(emptySet()));
when(cartDiscountService.createCartDiscount(any())).thenReturn(completedFuture(Optional.empty()));
final CartDiscountSyncOptions spyCartDiscountSyncOptions = spy(cartDiscountSyncOptions);
// test
new CartDiscountSync(spyCartDiscountSyncOptions, getMockTypeService(), cartDiscountService).sync(singletonList(newCartDiscount)).toCompletableFuture().join();
// assertion
verify(spyCartDiscountSyncOptions).applyBeforeCreateCallback(newCartDiscount);
verify(spyCartDiscountSyncOptions, never()).applyBeforeUpdateCallback(any(), any(), any());
}
Aggregations