Search in sources :

Example 11 with CartDiscountService

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;
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) CartDiscountSyncOptionsBuilder(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder) HashMap(java.util.HashMap) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) Collections.singletonList(java.util.Collections.singletonList) CartDiscountQuery(io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscountService(com.commercetools.sync.services.CartDiscountService) VerificationModeFactory.only(org.mockito.internal.verification.VerificationModeFactory.only) Assertions.as(org.assertj.core.api.Assertions.as) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) InternalServerErrorException(io.sphere.sdk.client.InternalServerErrorException) SetDescription(io.sphere.sdk.cartdiscounts.commands.updateactions.SetDescription) CartDiscountUpdateCommand(io.sphere.sdk.cartdiscounts.commands.CartDiscountUpdateCommand) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) Mockito.never(org.mockito.Mockito.never) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) Optional(java.util.Optional) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Mockito.mock(org.mockito.Mockito.mock) Optional(java.util.Optional) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) HashMap(java.util.HashMap) CartDiscountService(com.commercetools.sync.services.CartDiscountService) LocalizedString(io.sphere.sdk.models.LocalizedString) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) SphereClient(io.sphere.sdk.client.SphereClient) InternalServerErrorException(io.sphere.sdk.client.InternalServerErrorException) Test(org.junit.jupiter.api.Test)

Example 12 with CartDiscountService

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());
}
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 13 with CartDiscountService

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

Example 14 with CartDiscountService

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());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) CartDiscountSyncOptionsBuilder(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder) HashMap(java.util.HashMap) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) Collections.singletonList(java.util.Collections.singletonList) CartDiscountQuery(io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscountService(com.commercetools.sync.services.CartDiscountService) VerificationModeFactory.only(org.mockito.internal.verification.VerificationModeFactory.only) Assertions.as(org.assertj.core.api.Assertions.as) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) InternalServerErrorException(io.sphere.sdk.client.InternalServerErrorException) SetDescription(io.sphere.sdk.cartdiscounts.commands.updateactions.SetDescription) CartDiscountUpdateCommand(io.sphere.sdk.cartdiscounts.commands.CartDiscountUpdateCommand) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) Mockito.never(org.mockito.Mockito.never) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) Optional(java.util.Optional) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Mockito.mock(org.mockito.Mockito.mock) Optional(java.util.Optional) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) HashMap(java.util.HashMap) CartDiscountService(com.commercetools.sync.services.CartDiscountService) SphereClient(io.sphere.sdk.client.SphereClient) LocalizedString(io.sphere.sdk.models.LocalizedString) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) Test(org.junit.jupiter.api.Test)

Example 15 with CartDiscountService

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());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) CartDiscountSyncOptionsBuilder(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder) HashMap(java.util.HashMap) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) Collections.singletonList(java.util.Collections.singletonList) CartDiscountQuery(io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscountService(com.commercetools.sync.services.CartDiscountService) VerificationModeFactory.only(org.mockito.internal.verification.VerificationModeFactory.only) Assertions.as(org.assertj.core.api.Assertions.as) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) InternalServerErrorException(io.sphere.sdk.client.InternalServerErrorException) SetDescription(io.sphere.sdk.cartdiscounts.commands.updateactions.SetDescription) CartDiscountUpdateCommand(io.sphere.sdk.cartdiscounts.commands.CartDiscountUpdateCommand) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) Mockito.never(org.mockito.Mockito.never) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) Optional(java.util.Optional) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Mockito.mock(org.mockito.Mockito.mock) Optional(java.util.Optional) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) HashMap(java.util.HashMap) SphereClient(io.sphere.sdk.client.SphereClient) LocalizedString(io.sphere.sdk.models.LocalizedString) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) 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