Search in sources :

Example 1 with CartDiscountService

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);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CartPredicate(io.sphere.sdk.cartdiscounts.CartPredicate) ZonedDateTime(java.time.ZonedDateTime) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) Mockito.spy(org.mockito.Mockito.spy) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) CartDiscountService(com.commercetools.sync.services.CartDiscountService) CartDiscountDraftBuilder(io.sphere.sdk.cartdiscounts.CartDiscountDraftBuilder) CartDiscountSyncStatistics(com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics) Collections.singleton(java.util.Collections.singleton) BeforeAll(org.junit.jupiter.api.BeforeAll) Assertions.as(org.assertj.core.api.Assertions.as) Locale(java.util.Locale) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) TypeService(com.commercetools.sync.services.TypeService) SphereClient(io.sphere.sdk.client.SphereClient) SphereException(io.sphere.sdk.models.SphereException) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) Collections.emptyMap(java.util.Collections.emptyMap) CartDiscountValue(io.sphere.sdk.cartdiscounts.CartDiscountValue) Collections.emptySet(java.util.Collections.emptySet) CompletionException(java.util.concurrent.CompletionException) Mockito.when(org.mockito.Mockito.when) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) String.format(java.lang.String.format) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) Mockito.never(org.mockito.Mockito.never) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) List(java.util.List) MockUtils.getMockTypeService(com.commercetools.sync.commons.MockUtils.getMockTypeService) Optional(java.util.Optional) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) ShippingCostTarget(io.sphere.sdk.cartdiscounts.ShippingCostTarget) AssertionsForStatistics(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics) Mockito.mock(org.mockito.Mockito.mock) CartDiscountService(com.commercetools.sync.services.CartDiscountService) CartDiscountSyncStatistics(com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics) SphereClient(io.sphere.sdk.client.SphereClient) ArrayList(java.util.ArrayList) LocalizedString(io.sphere.sdk.models.LocalizedString) SphereException(io.sphere.sdk.models.SphereException) Test(org.junit.jupiter.api.Test)

Example 2 with CartDiscountService

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);
}
Also used : CartDiscountService(com.commercetools.sync.services.CartDiscountService) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Test(org.junit.jupiter.api.Test)

Example 3 with CartDiscountService

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);
}
Also used : CartDiscountService(com.commercetools.sync.services.CartDiscountService) UpdateAction(io.sphere.sdk.commands.UpdateAction) InternalServerErrorException(io.sphere.sdk.client.InternalServerErrorException) ExecutionException(java.util.concurrent.ExecutionException) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) Test(org.junit.jupiter.api.Test)

Example 4 with CartDiscountService

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());
}
Also used : Optional(java.util.Optional) CartDiscountService(com.commercetools.sync.services.CartDiscountService) SphereClient(io.sphere.sdk.client.SphereClient) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) Test(org.junit.jupiter.api.Test)

Example 5 with CartDiscountService

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());
}
Also used : CartDiscountService(com.commercetools.sync.services.CartDiscountService) Test(org.junit.jupiter.api.Test)

Aggregations

CartDiscountService (com.commercetools.sync.services.CartDiscountService)16 Test (org.junit.jupiter.api.Test)16 CartDiscountSyncOptions (com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions)13 CartDiscount (io.sphere.sdk.cartdiscounts.CartDiscount)13 SphereClient (io.sphere.sdk.client.SphereClient)12 Optional (java.util.Optional)12 CartDiscountDraft (io.sphere.sdk.cartdiscounts.CartDiscountDraft)9 CartDiscountQuery (io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery)9 Collections.singletonList (java.util.Collections.singletonList)9 List (java.util.List)9 ExecutionException (java.util.concurrent.ExecutionException)9 Assertions.as (org.assertj.core.api.Assertions.as)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 STRING (org.assertj.core.api.InstanceOfAssertFactories.STRING)9 CartDiscountSyncOptionsBuilder (com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder)8 TimeUnit (java.util.concurrent.TimeUnit)8 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)8 Mockito.when (org.mockito.Mockito.when)8 CartDiscountDraftBuilder (io.sphere.sdk.cartdiscounts.CartDiscountDraftBuilder)6 String.format (java.lang.String.format)6