Search in sources :

Example 1 with CART_DISCOUNT_KEY_1

use of com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_1 in project commercetools-sync-java by commercetools.

the class CartDiscountSyncIT method sync_WithoutCartPredicate_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithoutCartPredicate_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    // prepare
    // Draft without "cartPredicate" throws a commercetools exception because "cartPredicate" is a
    // required value
    final CartDiscountDraft newCartDiscountDraftWithoutName = CartDiscountDraftBuilder.of(CART_DISCOUNT_NAME_1, (String) null, CART_DISCOUNT_VALUE_1, CART_DISCOUNT_TARGET_1, SORT_ORDER_1, false).key(CART_DISCOUNT_KEY_1).active(false).description(CART_DISCOUNT_DESC_1).validFrom(JANUARY_FROM).validUntil(JANUARY_UNTIL).build();
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception);
    }).build();
    final CartDiscountSync cartDiscountSync = new CartDiscountSync(cartDiscountSyncOptions);
    // test
    final CartDiscountSyncStatistics cartDiscountSyncStatistics = cartDiscountSync.sync(singletonList(newCartDiscountDraftWithoutName)).toCompletableFuture().join();
    // assertions
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to update cart discount with key: 'key_1'.");
    assertThat(exceptions).hasSize(1).singleElement().matches(throwable -> {
        assertThat(throwable).isExactlyInstanceOf(SyncException.class);
        assertThat(throwable).hasCauseExactlyInstanceOf(CompletionException.class);
        assertThat(throwable.getCause()).hasCauseExactlyInstanceOf(ErrorResponseException.class);
        assertThat(throwable.getCause()).hasMessageContaining("cartPredicate: Missing required value");
        return true;
    });
    assertThat(cartDiscountSyncStatistics).hasValues(1, 0, 0, 1);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) JANUARY_FROM(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.JANUARY_FROM) CartDiscountITUtils.deleteCartDiscounts(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.deleteCartDiscounts) ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) 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) UpdateAction(io.sphere.sdk.commands.UpdateAction) ChangeCartPredicate(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeCartPredicate) Collections.singletonList(java.util.Collections.singletonList) CartDiscountQuery(io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery) AfterAll(org.junit.jupiter.api.AfterAll) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscountDraftBuilder(io.sphere.sdk.cartdiscounts.CartDiscountDraftBuilder) CartDiscountSyncStatistics(com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics) Locale(java.util.Locale) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) CartDiscountSync(com.commercetools.sync.cartdiscounts.CartDiscountSync) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) JsonNode(com.fasterxml.jackson.databind.JsonNode) ChangeTarget(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeTarget) CartDiscountITUtils.populateTargetProject(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateTargetProject) ITUtils.deleteTypes(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes) CART_DISCOUNT_TARGET_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_TARGET_1) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CART_DISCOUNT_TARGET_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_TARGET_2) CART_DISCOUNT_DESC_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_DESC_1) CompletionException(java.util.concurrent.CompletionException) SetCustomType(io.sphere.sdk.cartdiscounts.commands.updateactions.SetCustomType) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) List(java.util.List) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Optional(java.util.Optional) JANUARY_UNTIL(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.JANUARY_UNTIL) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) CART_DISCOUNT_KEY_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_2) CART_DISCOUNT_NAME_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_NAME_1) 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) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CART_DISCOUNT_DRAFT_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_DRAFT_2) CART_DISCOUNT_VALUE_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_VALUE_1) CartDiscountSyncOptionsBuilder(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder) CART_DISCOUNT_DRAFT_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_DRAFT_1) CART_DISCOUNT_VALUE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_VALUE_2) Mockito.spy(org.mockito.Mockito.spy) CartDiscountCreateCommand(io.sphere.sdk.cartdiscounts.commands.CartDiscountCreateCommand) ArrayList(java.util.ArrayList) BOOLEAN_CUSTOM_FIELD_NAME(com.commercetools.sync.integration.commons.utils.ITUtils.BOOLEAN_CUSTOM_FIELD_NAME) Assertions.as(org.assertj.core.api.Assertions.as) ChangeValue(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeValue) Nonnull(javax.annotation.Nonnull) CartDiscountITUtils.getCartDiscountByKey(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.getCartDiscountByKey) SORT_ORDER_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.SORT_ORDER_1) Collections.emptyMap(java.util.Collections.emptyMap) CartDiscountITUtils.getSortOrders(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.getSortOrders) CartDiscountUpdateCommand(io.sphere.sdk.cartdiscounts.commands.CartDiscountUpdateCommand) Mockito.when(org.mockito.Mockito.when) ITUtils.createCustomFieldsJsonMap(com.commercetools.sync.integration.commons.utils.ITUtils.createCustomFieldsJsonMap) CartDiscountITUtils.createCartDiscountCustomType(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.createCartDiscountCustomType) OLD_CART_DISCOUNT_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.OLD_CART_DISCOUNT_TYPE_KEY) CART_DISCOUNT_CART_PREDICATE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_CART_PREDICATE_2) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) SetCustomField(io.sphere.sdk.cartdiscounts.commands.updateactions.SetCustomField) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) CART_DISCOUNT_CART_PREDICATE_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_CART_PREDICATE_1) Type(io.sphere.sdk.types.Type) CartDiscountSync(com.commercetools.sync.cartdiscounts.CartDiscountSync) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) CartDiscountSyncStatistics(com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics) ArrayList(java.util.ArrayList) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) Test(org.junit.jupiter.api.Test)

Example 2 with CART_DISCOUNT_KEY_1

use of com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_1 in project commercetools-sync-java by commercetools.

the class CartDiscountServiceImplIT method updateCartDiscount_WithValidChanges_ShouldUpdateCartDiscountCorrectly.

@Test
void updateCartDiscount_WithValidChanges_ShouldUpdateCartDiscountCorrectly() {
    final CartDiscount cartDiscount = CTP_TARGET_CLIENT.execute(CartDiscountQuery.of().withPredicates(cartDiscountQueryModel -> cartDiscountQueryModel.key().is(CART_DISCOUNT_KEY_1))).toCompletableFuture().thenApply(PagedResult::head).thenApply(Optional::get).join();
    final ChangeCartPredicate changeCartPredicateUpdateAction = ChangeCartPredicate.of(CART_DISCOUNT_CART_PREDICATE_2);
    final CartDiscount updatedCartDiscount = cartDiscountService.updateCartDiscount(cartDiscount, singletonList(changeCartPredicateUpdateAction)).toCompletableFuture().join();
    assertThat(updatedCartDiscount).satisfies(updated -> {
        assertThat(updated.getKey()).isEqualTo(cartDiscount.getKey());
        assertThat(updated.getName()).isEqualTo(cartDiscount.getName());
        assertThat(updated.getValue()).isEqualTo(cartDiscount.getValue());
        assertThat(updated.getTarget()).isEqualTo(cartDiscount.getTarget());
        assertThat(updated.getCartPredicate()).isEqualTo(CART_DISCOUNT_CART_PREDICATE_2.toSphereCartPredicate());
    });
}
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) ChangeCartPredicate(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeCartPredicate) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) PagedResult(io.sphere.sdk.queries.PagedResult) Test(org.junit.jupiter.api.Test)

Example 3 with CART_DISCOUNT_KEY_1

use of com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_1 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 4 with CART_DISCOUNT_KEY_1

use of com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_1 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)

Example 5 with CART_DISCOUNT_KEY_1

use of com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_1 in project commercetools-sync-java by commercetools.

the class CartDiscountSyncIT method sync_WithConcurrentModificationException_ShouldRetryToUpdateNewCartDiscountWithSuccess.

@Test
void sync_WithConcurrentModificationException_ShouldRetryToUpdateNewCartDiscountWithSuccess() {
    // Preparation
    final SphereClient spyClient = buildClientWithConcurrentModificationUpdate();
    final CartDiscountDraft draft2 = CartDiscountDraftBuilder.of(CART_DISCOUNT_DRAFT_2).custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CART_DISCOUNT_TYPE_KEY, createCustomFieldsJsonMap())).build();
    CTP_TARGET_CLIENT.execute(CartDiscountCreateCommand.of(draft2)).toCompletableFuture().join();
    final CartDiscountDraft updatedDraft = CartDiscountDraftBuilder.of(CART_DISCOUNT_DRAFT_2).description(CART_DISCOUNT_DESC_1).build();
    final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(spyClient).build();
    final CartDiscountSync cartDiscountSync = new CartDiscountSync(cartDiscountSyncOptions);
    // test
    final CartDiscountSyncStatistics statistics = cartDiscountSync.sync(singletonList(updatedDraft)).toCompletableFuture().join();
    // assertion
    assertThat(statistics).hasValues(1, 0, 1, 0);
    // Assert CTP state.
    final PagedQueryResult<CartDiscount> queryResult = CTP_TARGET_CLIENT.execute(CartDiscountQuery.of().plusPredicates(queryModel -> queryModel.key().is(CART_DISCOUNT_KEY_1))).toCompletableFuture().join();
    assertThat(queryResult.head()).hasValueSatisfying(cartDiscount -> assertThat(cartDiscount.getKey()).isEqualTo(CART_DISCOUNT_KEY_1));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) JANUARY_FROM(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.JANUARY_FROM) CartDiscountITUtils.deleteCartDiscounts(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.deleteCartDiscounts) ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) 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) UpdateAction(io.sphere.sdk.commands.UpdateAction) ChangeCartPredicate(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeCartPredicate) Collections.singletonList(java.util.Collections.singletonList) CartDiscountQuery(io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery) AfterAll(org.junit.jupiter.api.AfterAll) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscountDraftBuilder(io.sphere.sdk.cartdiscounts.CartDiscountDraftBuilder) CartDiscountSyncStatistics(com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics) Locale(java.util.Locale) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) CartDiscountSync(com.commercetools.sync.cartdiscounts.CartDiscountSync) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) JsonNode(com.fasterxml.jackson.databind.JsonNode) ChangeTarget(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeTarget) CartDiscountITUtils.populateTargetProject(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateTargetProject) ITUtils.deleteTypes(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes) CART_DISCOUNT_TARGET_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_TARGET_1) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CART_DISCOUNT_TARGET_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_TARGET_2) CART_DISCOUNT_DESC_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_DESC_1) CompletionException(java.util.concurrent.CompletionException) SetCustomType(io.sphere.sdk.cartdiscounts.commands.updateactions.SetCustomType) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) List(java.util.List) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Optional(java.util.Optional) JANUARY_UNTIL(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.JANUARY_UNTIL) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) CART_DISCOUNT_KEY_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_2) CART_DISCOUNT_NAME_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_NAME_1) 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) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CART_DISCOUNT_DRAFT_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_DRAFT_2) CART_DISCOUNT_VALUE_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_VALUE_1) CartDiscountSyncOptionsBuilder(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder) CART_DISCOUNT_DRAFT_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_DRAFT_1) CART_DISCOUNT_VALUE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_VALUE_2) Mockito.spy(org.mockito.Mockito.spy) CartDiscountCreateCommand(io.sphere.sdk.cartdiscounts.commands.CartDiscountCreateCommand) ArrayList(java.util.ArrayList) BOOLEAN_CUSTOM_FIELD_NAME(com.commercetools.sync.integration.commons.utils.ITUtils.BOOLEAN_CUSTOM_FIELD_NAME) Assertions.as(org.assertj.core.api.Assertions.as) ChangeValue(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeValue) Nonnull(javax.annotation.Nonnull) CartDiscountITUtils.getCartDiscountByKey(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.getCartDiscountByKey) SORT_ORDER_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.SORT_ORDER_1) Collections.emptyMap(java.util.Collections.emptyMap) CartDiscountITUtils.getSortOrders(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.getSortOrders) CartDiscountUpdateCommand(io.sphere.sdk.cartdiscounts.commands.CartDiscountUpdateCommand) Mockito.when(org.mockito.Mockito.when) ITUtils.createCustomFieldsJsonMap(com.commercetools.sync.integration.commons.utils.ITUtils.createCustomFieldsJsonMap) CartDiscountITUtils.createCartDiscountCustomType(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.createCartDiscountCustomType) OLD_CART_DISCOUNT_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.OLD_CART_DISCOUNT_TYPE_KEY) CART_DISCOUNT_CART_PREDICATE_2(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_CART_PREDICATE_2) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) SetCustomField(io.sphere.sdk.cartdiscounts.commands.updateactions.SetCustomField) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) CART_DISCOUNT_CART_PREDICATE_1(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_CART_PREDICATE_1) Type(io.sphere.sdk.types.Type) CartDiscountSync(com.commercetools.sync.cartdiscounts.CartDiscountSync) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) CartDiscountSyncStatistics(com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics) SphereClient(io.sphere.sdk.client.SphereClient) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Test(org.junit.jupiter.api.Test)

Aggregations

CartDiscountSyncOptions (com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions)6 CartDiscountSyncOptionsBuilder (com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder)6 CART_DISCOUNT_CART_PREDICATE_2 (com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_CART_PREDICATE_2)6 CART_DISCOUNT_KEY_1 (com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_1)6 CART_DISCOUNT_KEY_2 (com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_KEY_2)6 CART_DISCOUNT_NAME_2 (com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_NAME_2)6 CART_DISCOUNT_TARGET_2 (com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_TARGET_2)6 CART_DISCOUNT_VALUE_2 (com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.CART_DISCOUNT_VALUE_2)6 SORT_ORDER_1 (com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.SORT_ORDER_1)6 CartDiscountITUtils.populateTargetProject (com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateTargetProject)6 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)6 CartDiscount (io.sphere.sdk.cartdiscounts.CartDiscount)6 CartDiscountDraft (io.sphere.sdk.cartdiscounts.CartDiscountDraft)6 CartDiscountDraftBuilder (io.sphere.sdk.cartdiscounts.CartDiscountDraftBuilder)6 ChangeCartPredicate (io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeCartPredicate)6 CartDiscountQuery (io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery)6 BadGatewayException (io.sphere.sdk.client.BadGatewayException)6 ErrorResponseException (io.sphere.sdk.client.ErrorResponseException)6 SphereClient (io.sphere.sdk.client.SphereClient)6 CompletableFutureUtils.exceptionallyCompletedFuture (io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture)6