Search in sources :

Example 26 with CartDiscountSyncOptions

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

the class CartDiscountSyncIT method sync_WithNewCartDiscount_ShouldCreateNewDiscount.

@Test
void sync_WithNewCartDiscount_ShouldCreateNewDiscount() {
    // preparation
    final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
    final CartDiscountSync cartDiscountSync = new CartDiscountSync(cartDiscountSyncOptions);
    // test
    final CartDiscountSyncStatistics cartDiscountSyncStatistics = cartDiscountSync.sync(singletonList(CART_DISCOUNT_DRAFT_2)).toCompletableFuture().join();
    // assertions
    assertThat(cartDiscountSyncStatistics).hasValues(1, 1, 0, 0);
    final Optional<CartDiscount> cartDiscountAfterCreation = getCartDiscountByKey(CTP_TARGET_CLIENT, CART_DISCOUNT_KEY_2);
    assertThat(cartDiscountAfterCreation).hasValueSatisfying(cartDiscount -> {
        assertThat(cartDiscount.getName()).isEqualTo(CART_DISCOUNT_DRAFT_2.getName());
        assertThat(cartDiscount.getDescription()).isEqualTo(CART_DISCOUNT_DRAFT_2.getDescription());
        assertThat(cartDiscount.getCartPredicate()).isEqualTo(CART_DISCOUNT_DRAFT_2.getCartPredicate());
        assertThat(cartDiscount.getValue()).isEqualTo(CART_DISCOUNT_DRAFT_2.getValue());
    });
}
Also used : CartDiscountSync(com.commercetools.sync.cartdiscounts.CartDiscountSync) CartDiscountSyncStatistics(com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Test(org.junit.jupiter.api.Test)

Example 27 with CartDiscountSyncOptions

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

the class CartDiscountSyncIT method sync_WithUpdatedCartDiscount_WithNewValue_ShouldUpdateCartDiscountWithNewValue.

@Test
void sync_WithUpdatedCartDiscount_WithNewValue_ShouldUpdateCartDiscountWithNewValue() {
    // preparation
    final CartDiscountDraft newCartDiscountDraftWithExistingKey = CartDiscountDraftBuilder.of(CART_DISCOUNT_DRAFT_1).value(CART_DISCOUNT_VALUE_2).build();
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final List<UpdateAction<CartDiscount>> updateActionsList = new ArrayList<>();
    final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception);
    }).beforeUpdateCallback((updateActions, newCartDiscount, oldCartDiscount) -> {
        updateActionsList.addAll(updateActions);
        return updateActions;
    }).build();
    final CartDiscountSync cartDiscountSync = new CartDiscountSync(cartDiscountSyncOptions);
    // test
    final CartDiscountSyncStatistics cartDiscountSyncStatistics = cartDiscountSync.sync(singletonList(newCartDiscountDraftWithExistingKey)).toCompletableFuture().join();
    // assertions
    assertThat(errorMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    assertThat(updateActionsList).containsExactly(ChangeValue.of(CART_DISCOUNT_VALUE_2));
    assertThat(cartDiscountSyncStatistics.getReportMessage()).isEqualTo("Summary: 1 cart discounts were processed in total" + " (0 created, 1 updated and 0 failed to sync).");
    assertThat(cartDiscountSyncStatistics).hasValues(1, 0, 1, 0);
}
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) UpdateAction(io.sphere.sdk.commands.UpdateAction) CartDiscountSyncStatistics(com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics) ArrayList(java.util.ArrayList) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) Test(org.junit.jupiter.api.Test)

Example 28 with CartDiscountSyncOptions

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

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

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

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