Search in sources :

Example 31 with CartDiscountSyncOptions

use of com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions 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 32 with CartDiscountSyncOptions

use of com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions 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)

Example 33 with CartDiscountSyncOptions

use of com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions in project commercetools-sync-java by commercetools.

the class CartDiscountServiceImplTest method updateCartDiscount_WithMockSuccessfulCtpResponse_ShouldCallCartDiscountUpdateCommand.

@Test
void updateCartDiscount_WithMockSuccessfulCtpResponse_ShouldCallCartDiscountUpdateCommand() {
    // preparation
    final CartDiscount mockCartDiscount = mock(CartDiscount.class);
    final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(mock(SphereClient.class)).build();
    when(cartDiscountSyncOptions.getCtpClient().execute(any())).thenReturn(completedFuture(mockCartDiscount));
    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).isCompletedWithValue(mockCartDiscount);
    verify(cartDiscountSyncOptions.getCtpClient()).execute(eq(CartDiscountUpdateCommand.of(mockCartDiscount, updateActions)));
}
Also used : CartDiscountService(com.commercetools.sync.services.CartDiscountService) UpdateAction(io.sphere.sdk.commands.UpdateAction) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) Test(org.junit.jupiter.api.Test)

Aggregations

CartDiscountSyncOptions (com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions)33 Test (org.junit.jupiter.api.Test)31 CartDiscount (io.sphere.sdk.cartdiscounts.CartDiscount)27 CartDiscountDraft (io.sphere.sdk.cartdiscounts.CartDiscountDraft)25 CartDiscountQuery (io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery)24 SphereClient (io.sphere.sdk.client.SphereClient)24 Optional (java.util.Optional)24 CartDiscountSyncOptionsBuilder (com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder)23 UpdateAction (io.sphere.sdk.commands.UpdateAction)23 List (java.util.List)23 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)22 ChangeCartPredicate (io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeCartPredicate)20 BeforeEach (org.junit.jupiter.api.BeforeEach)20 CartDiscountITUtils.populateTargetProject (com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateTargetProject)19 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)19 CartDiscountDraftBuilder (io.sphere.sdk.cartdiscounts.CartDiscountDraftBuilder)19 ArrayList (java.util.ArrayList)19 AfterAll (org.junit.jupiter.api.AfterAll)19 CART_DISCOUNT_CART_PREDICATE_2 (com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_CART_PREDICATE_2)17 CART_DISCOUNT_KEY_1 (com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_1)17