Search in sources :

Example 6 with CartDiscountService

use of com.commercetools.sync.services.CartDiscountService in project commercetools-sync-java by commercetools.

the class CartDiscountServiceImplIT method setup.

/**
 * Deletes types & cart discounts from the target CTP project, then it populates the project with
 * test data.
 */
@BeforeEach
void setup() {
    deleteCartDiscountsFromTargetAndSource();
    populateTargetProject();
    errorCallBackMessages = new ArrayList<>();
    errorCallBackExceptions = new ArrayList<>();
    final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception);
    }).build();
    cartDiscountService = new CartDiscountServiceImpl(cartDiscountSyncOptions);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) CartDiscountServiceImpl(com.commercetools.sync.services.impl.CartDiscountServiceImpl) CART_DISCOUNT_KEY_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_2) CART_DISCOUNT_KEY_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_1) CART_DISCOUNT_NAME_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_NAME_2) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PagedResult(io.sphere.sdk.queries.PagedResult) CartDiscountSyncOptionsBuilder(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder) CART_DISCOUNT_VALUE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_VALUE_2) Mockito.spy(org.mockito.Mockito.spy) ChangeCartPredicate(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeCartPredicate) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) Collections.singletonList(java.util.Collections.singletonList) CartDiscountQuery(io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery) ArrayList(java.util.ArrayList) InstanceOfAssertFactories.throwable(org.assertj.core.api.InstanceOfAssertFactories.throwable) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscountService(com.commercetools.sync.services.CartDiscountService) CartDiscountDraftBuilder(io.sphere.sdk.cartdiscounts.CartDiscountDraftBuilder) Assertions.as(org.assertj.core.api.Assertions.as) ChangeName(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeName) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) CartDiscountITUtils.populateTargetProject(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateTargetProject) SORT_ORDER_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.SORT_ORDER_1) SORT_ORDER_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.SORT_ORDER_2) CartDiscountByKeyGet(io.sphere.sdk.cartdiscounts.queries.CartDiscountByKeyGet) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CART_DISCOUNT_TARGET_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_TARGET_2) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CART_DISCOUNT_CART_PREDICATE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_CART_PREDICATE_2) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) Optional(java.util.Optional) CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CartDiscountServiceImpl(com.commercetools.sync.services.impl.CartDiscountServiceImpl) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with CartDiscountService

use of com.commercetools.sync.services.CartDiscountService in project commercetools-sync-java by commercetools.

the class CartDiscountServiceImplIT method fetchMatchingCartDiscountsByKeys_WithBadGateWayExceptionAlways_ShouldFail.

@Test
void fetchMatchingCartDiscountsByKeys_WithBadGateWayExceptionAlways_ShouldFail() {
    // Mock sphere client to return BadGatewayException on any request.
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    when(spyClient.execute(any(CartDiscountQuery.class))).thenReturn(exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
    final CartDiscountSyncOptions spyOptions = CartDiscountSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, actions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception);
    }).build();
    final CartDiscountService spyCartDiscountService = new CartDiscountServiceImpl(spyOptions);
    final Set<String> cartDiscountKeys = new HashSet<>();
    cartDiscountKeys.add(CART_DISCOUNT_KEY_1);
    // test and assert
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(spyCartDiscountService.fetchMatchingCartDiscountsByKeys(cartDiscountKeys)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) CartDiscountServiceImpl(com.commercetools.sync.services.impl.CartDiscountServiceImpl) CART_DISCOUNT_KEY_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_2) CART_DISCOUNT_KEY_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_1) CART_DISCOUNT_NAME_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_NAME_2) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PagedResult(io.sphere.sdk.queries.PagedResult) CartDiscountSyncOptionsBuilder(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder) CART_DISCOUNT_VALUE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_VALUE_2) Mockito.spy(org.mockito.Mockito.spy) ChangeCartPredicate(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeCartPredicate) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) Collections.singletonList(java.util.Collections.singletonList) CartDiscountQuery(io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery) ArrayList(java.util.ArrayList) InstanceOfAssertFactories.throwable(org.assertj.core.api.InstanceOfAssertFactories.throwable) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscountService(com.commercetools.sync.services.CartDiscountService) CartDiscountDraftBuilder(io.sphere.sdk.cartdiscounts.CartDiscountDraftBuilder) Assertions.as(org.assertj.core.api.Assertions.as) ChangeName(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeName) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) CartDiscountITUtils.populateTargetProject(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateTargetProject) SORT_ORDER_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.SORT_ORDER_1) SORT_ORDER_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.SORT_ORDER_2) CartDiscountByKeyGet(io.sphere.sdk.cartdiscounts.queries.CartDiscountByKeyGet) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CART_DISCOUNT_TARGET_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_TARGET_2) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CART_DISCOUNT_CART_PREDICATE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_CART_PREDICATE_2) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) Optional(java.util.Optional) CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CartDiscountService(com.commercetools.sync.services.CartDiscountService) SphereClient(io.sphere.sdk.client.SphereClient) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CartDiscountServiceImpl(com.commercetools.sync.services.impl.CartDiscountServiceImpl) ExecutionException(java.util.concurrent.ExecutionException) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 8 with CartDiscountService

use of com.commercetools.sync.services.CartDiscountService in project commercetools-sync-java by commercetools.

the class CartDiscountServiceImplIT method createCartDiscount_WithValidCartDiscount_ShouldCreateCartDiscount.

@Test
void createCartDiscount_WithValidCartDiscount_ShouldCreateCartDiscount() {
    // preparation
    final CartDiscountDraft newCartDiscountDraft = CartDiscountDraftBuilder.of(CART_DISCOUNT_NAME_2, CART_DISCOUNT_CART_PREDICATE_2, CART_DISCOUNT_VALUE_2, CART_DISCOUNT_TARGET_2, SORT_ORDER_2, false).key(CART_DISCOUNT_KEY_2).active(false).build();
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    final CartDiscountSyncOptions spyOptions = CartDiscountSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, actions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception);
    }).build();
    final CartDiscountService spyCartDiscountService = new CartDiscountServiceImpl(spyOptions);
    // test
    final Optional<CartDiscount> createdCartDiscount = spyCartDiscountService.createCartDiscount(newCartDiscountDraft).toCompletableFuture().join();
    final Optional<CartDiscount> queriedOptional = CTP_TARGET_CLIENT.execute(CartDiscountQuery.of().withPredicates(cartDiscountQueryModel -> cartDiscountQueryModel.key().is(CART_DISCOUNT_KEY_2))).toCompletableFuture().join().head();
    assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdCartDiscount).hasValueSatisfying(created -> {
        assertThat(created.getKey()).isEqualTo(queried.getKey());
        assertThat(created.getName()).isEqualTo(queried.getName());
        assertThat(created.getCartPredicate()).isEqualTo(queried.getCartPredicate());
        assertThat(created.getValue()).isEqualTo(queried.getValue());
        assertThat(created.getTarget()).isEqualTo(queried.getTarget());
        assertThat(created.getSortOrder()).isEqualTo(queried.getSortOrder());
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) CartDiscountServiceImpl(com.commercetools.sync.services.impl.CartDiscountServiceImpl) CART_DISCOUNT_KEY_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_2) CART_DISCOUNT_KEY_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_1) CART_DISCOUNT_NAME_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_NAME_2) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PagedResult(io.sphere.sdk.queries.PagedResult) CartDiscountSyncOptionsBuilder(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder) CART_DISCOUNT_VALUE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_VALUE_2) Mockito.spy(org.mockito.Mockito.spy) ChangeCartPredicate(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeCartPredicate) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) Collections.singletonList(java.util.Collections.singletonList) CartDiscountQuery(io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery) ArrayList(java.util.ArrayList) InstanceOfAssertFactories.throwable(org.assertj.core.api.InstanceOfAssertFactories.throwable) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscountService(com.commercetools.sync.services.CartDiscountService) CartDiscountDraftBuilder(io.sphere.sdk.cartdiscounts.CartDiscountDraftBuilder) Assertions.as(org.assertj.core.api.Assertions.as) ChangeName(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeName) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) CartDiscountITUtils.populateTargetProject(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateTargetProject) SORT_ORDER_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.SORT_ORDER_1) SORT_ORDER_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.SORT_ORDER_2) CartDiscountByKeyGet(io.sphere.sdk.cartdiscounts.queries.CartDiscountByKeyGet) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CART_DISCOUNT_TARGET_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_TARGET_2) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CART_DISCOUNT_CART_PREDICATE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_CART_PREDICATE_2) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) Optional(java.util.Optional) CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) CartDiscountService(com.commercetools.sync.services.CartDiscountService) SphereClient(io.sphere.sdk.client.SphereClient) CartDiscountServiceImpl(com.commercetools.sync.services.impl.CartDiscountServiceImpl) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Test(org.junit.jupiter.api.Test)

Example 9 with CartDiscountService

use of com.commercetools.sync.services.CartDiscountService in project commercetools-sync-java by commercetools.

the class CartDiscountServiceImplIT method createCartDiscount_WithDuplicateSortOrder_ShouldHaveEmptyOptionalAsAResult.

@Test
void createCartDiscount_WithDuplicateSortOrder_ShouldHaveEmptyOptionalAsAResult() {
    // preparation
    final CartDiscountDraft newCartDiscountDraft = CartDiscountDraftBuilder.of(CART_DISCOUNT_NAME_2, CART_DISCOUNT_CART_PREDICATE_2, CART_DISCOUNT_VALUE_2, CART_DISCOUNT_TARGET_2, SORT_ORDER_1, false).key(CART_DISCOUNT_KEY_2).active(false).build();
    final CartDiscountSyncOptions options = CartDiscountSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception);
    }).build();
    cartDiscountService = new CartDiscountServiceImpl(options);
    // test
    final Optional<CartDiscount> result = cartDiscountService.createCartDiscount(newCartDiscountDraft).toCompletableFuture().join();
    // assertion
    assertThat(result).isEmpty();
    assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains(format("A duplicate value '\"%s\"' exists for field 'sortOrder'.", SORT_ORDER_1));
    ensureErrorCallbackIsDuplicateFieldError();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) CartDiscountServiceImpl(com.commercetools.sync.services.impl.CartDiscountServiceImpl) CART_DISCOUNT_KEY_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_2) CART_DISCOUNT_KEY_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_1) CART_DISCOUNT_NAME_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_NAME_2) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PagedResult(io.sphere.sdk.queries.PagedResult) CartDiscountSyncOptionsBuilder(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder) CART_DISCOUNT_VALUE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_VALUE_2) Mockito.spy(org.mockito.Mockito.spy) ChangeCartPredicate(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeCartPredicate) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) Collections.singletonList(java.util.Collections.singletonList) CartDiscountQuery(io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery) ArrayList(java.util.ArrayList) InstanceOfAssertFactories.throwable(org.assertj.core.api.InstanceOfAssertFactories.throwable) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscountService(com.commercetools.sync.services.CartDiscountService) CartDiscountDraftBuilder(io.sphere.sdk.cartdiscounts.CartDiscountDraftBuilder) Assertions.as(org.assertj.core.api.Assertions.as) ChangeName(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeName) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) CartDiscountITUtils.populateTargetProject(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateTargetProject) SORT_ORDER_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.SORT_ORDER_1) SORT_ORDER_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.SORT_ORDER_2) CartDiscountByKeyGet(io.sphere.sdk.cartdiscounts.queries.CartDiscountByKeyGet) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CART_DISCOUNT_TARGET_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_TARGET_2) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CART_DISCOUNT_CART_PREDICATE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_CART_PREDICATE_2) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) Optional(java.util.Optional) CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) CartDiscountServiceImpl(com.commercetools.sync.services.impl.CartDiscountServiceImpl) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Test(org.junit.jupiter.api.Test)

Example 10 with CartDiscountService

use of com.commercetools.sync.services.CartDiscountService in project commercetools-sync-java by commercetools.

the class CartDiscountServiceImplIT method createCartDiscount_WithDuplicateCartDiscountKey_ShouldHaveEmptyOptionalAsAResult.

@Test
void createCartDiscount_WithDuplicateCartDiscountKey_ShouldHaveEmptyOptionalAsAResult() {
    // preparation
    final CartDiscountDraft newCartDiscountDraft = CartDiscountDraftBuilder.of(CART_DISCOUNT_NAME_2, CART_DISCOUNT_CART_PREDICATE_2, CART_DISCOUNT_VALUE_2, CART_DISCOUNT_TARGET_2, SORT_ORDER_2, false).key(CART_DISCOUNT_KEY_1).active(false).build();
    final CartDiscountSyncOptions options = CartDiscountSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception);
    }).build();
    cartDiscountService = new CartDiscountServiceImpl(options);
    // test
    final Optional<CartDiscount> result = cartDiscountService.createCartDiscount(newCartDiscountDraft).toCompletableFuture().join();
    // assertion
    assertThat(result).isEmpty();
    assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains(format("A duplicate value '\"%s\"' exists for field 'key'.", CART_DISCOUNT_KEY_1));
    ensureErrorCallbackIsDuplicateFieldError();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) CartDiscountServiceImpl(com.commercetools.sync.services.impl.CartDiscountServiceImpl) CART_DISCOUNT_KEY_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_2) CART_DISCOUNT_KEY_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_1) CART_DISCOUNT_NAME_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_NAME_2) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PagedResult(io.sphere.sdk.queries.PagedResult) CartDiscountSyncOptionsBuilder(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder) CART_DISCOUNT_VALUE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_VALUE_2) Mockito.spy(org.mockito.Mockito.spy) ChangeCartPredicate(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeCartPredicate) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) Collections.singletonList(java.util.Collections.singletonList) CartDiscountQuery(io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery) ArrayList(java.util.ArrayList) InstanceOfAssertFactories.throwable(org.assertj.core.api.InstanceOfAssertFactories.throwable) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscountService(com.commercetools.sync.services.CartDiscountService) CartDiscountDraftBuilder(io.sphere.sdk.cartdiscounts.CartDiscountDraftBuilder) Assertions.as(org.assertj.core.api.Assertions.as) ChangeName(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeName) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) CartDiscountITUtils.populateTargetProject(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateTargetProject) SORT_ORDER_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.SORT_ORDER_1) SORT_ORDER_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.SORT_ORDER_2) CartDiscountByKeyGet(io.sphere.sdk.cartdiscounts.queries.CartDiscountByKeyGet) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CART_DISCOUNT_TARGET_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_TARGET_2) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CART_DISCOUNT_CART_PREDICATE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_CART_PREDICATE_2) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) Optional(java.util.Optional) CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) CartDiscountServiceImpl(com.commercetools.sync.services.impl.CartDiscountServiceImpl) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) 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