Search in sources :

Example 1 with CartDiscountSyncStatistics

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

the class CartDiscountSyncIT method sync_WithUpdates_ShouldReturnProperStatistics.

@Test
void sync_WithUpdates_ShouldReturnProperStatistics() {
    // preparation
    final List<CartDiscount> cartDiscounts = CTP_SOURCE_CLIENT.execute(CartDiscountQuery.of()).toCompletableFuture().join().getResults();
    final String newTypeKey = "new-type";
    createCartDiscountCustomType(newTypeKey, Locale.ENGLISH, newTypeKey, CTP_SOURCE_CLIENT);
    final Type newTargetCustomType = createCartDiscountCustomType(newTypeKey, Locale.ENGLISH, newTypeKey, CTP_TARGET_CLIENT);
    final List<CartDiscountDraft> cartDiscountDrafts = CartDiscountTransformUtils.toCartDiscountDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, cartDiscounts).join();
    // Apply some changes
    final List<CartDiscountDraft> updatedCartDiscountDrafts = cartDiscountDrafts.stream().map(draft -> CartDiscountDraftBuilder.of(draft).cartPredicate(CartPredicate.of("totalPrice >= \"100 EUR\"")).value(AbsoluteCartDiscountValue.of(MoneyImpl.of(40, EUR))).target(ShippingCostTarget.of()).custom(CustomFieldsDraft.ofTypeKeyAndJson(newTypeKey, emptyMap())).build()).collect(Collectors.toList());
    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(updatedCartDiscountDrafts).toCompletableFuture().join();
    // assertion
    assertThat(errorMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    assertThat(updateActionsList).containsExactly(ChangeValue.of(CartDiscountValue.ofAbsolute(Collections.singletonList(MoneyImpl.of(40, EUR)))), ChangeCartPredicate.of("totalPrice >= \"100 EUR\""), ChangeTarget.of(ShippingCostTarget.of()), SetCustomType.ofTypeIdAndJson(newTargetCustomType.getId(), emptyMap()));
    assertThat(cartDiscountSyncStatistics).hasValues(2, 1, 1, 0);
    assertThat(cartDiscountSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 cart discounts were processed in total" + " (1 created, 1 updated and 0 failed to sync).");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) MoneyImpl(io.sphere.sdk.utils.MoneyImpl) ITUtils.deleteTypesFromTargetAndSource(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypesFromTargetAndSource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CartPredicate(io.sphere.sdk.cartdiscounts.CartPredicate) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) CartDiscountSyncOptionsBuilder(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder) EUR(io.sphere.sdk.models.DefaultCurrencyUnits.EUR) ChangeCartPredicate(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeCartPredicate) CartDiscountQuery(io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery) ArrayList(java.util.ArrayList) 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) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) Locale(java.util.Locale) CartDiscountTransformUtils(com.commercetools.sync.cartdiscounts.utils.CartDiscountTransformUtils) CartDiscountSync(com.commercetools.sync.cartdiscounts.CartDiscountSync) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) ChangeTarget(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeTarget) ChangeValue(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeValue) CartDiscountITUtils.populateTargetProject(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateTargetProject) CartDiscountITUtils.populateSourceProject(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateSourceProject) Collections.emptyMap(java.util.Collections.emptyMap) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) CartDiscountValue(io.sphere.sdk.cartdiscounts.CartDiscountValue) AbsoluteCartDiscountValue(io.sphere.sdk.cartdiscounts.AbsoluteCartDiscountValue) CartDiscountITUtils.createCartDiscountCustomType(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.createCartDiscountCustomType) SetCustomType(io.sphere.sdk.cartdiscounts.commands.updateactions.SetCustomType) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) List(java.util.List) CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource) ShippingCostTarget(io.sphere.sdk.cartdiscounts.ShippingCostTarget) Collections(java.util.Collections) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) 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) CartDiscountITUtils.createCartDiscountCustomType(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.createCartDiscountCustomType) SetCustomType(io.sphere.sdk.cartdiscounts.commands.updateactions.SetCustomType) Type(io.sphere.sdk.types.Type) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Test(org.junit.jupiter.api.Test)

Example 2 with CartDiscountSyncStatistics

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

the class CartDiscountSyncIT method sync_WithoutUpdates_ShouldReturnProperStatistics.

@Test
void sync_WithoutUpdates_ShouldReturnProperStatistics() {
    // preparation
    final List<CartDiscount> cartDiscounts = CTP_SOURCE_CLIENT.execute(CartDiscountQuery.of()).toCompletableFuture().join().getResults();
    final List<CartDiscountDraft> cartDiscountDrafts = CartDiscountTransformUtils.toCartDiscountDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, cartDiscounts).join();
    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(cartDiscountDrafts).toCompletableFuture().join();
    // assertion
    assertThat(errorMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    assertThat(cartDiscountSyncStatistics).hasValues(2, 1, 0, 0);
    assertThat(cartDiscountSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 cart discounts were processed in total" + " (1 created, 0 updated and 0 failed to sync).");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) MoneyImpl(io.sphere.sdk.utils.MoneyImpl) ITUtils.deleteTypesFromTargetAndSource(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypesFromTargetAndSource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CartPredicate(io.sphere.sdk.cartdiscounts.CartPredicate) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) CartDiscountSyncOptionsBuilder(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder) EUR(io.sphere.sdk.models.DefaultCurrencyUnits.EUR) ChangeCartPredicate(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeCartPredicate) CartDiscountQuery(io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery) ArrayList(java.util.ArrayList) 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) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) Locale(java.util.Locale) CartDiscountTransformUtils(com.commercetools.sync.cartdiscounts.utils.CartDiscountTransformUtils) CartDiscountSync(com.commercetools.sync.cartdiscounts.CartDiscountSync) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) ChangeTarget(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeTarget) ChangeValue(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeValue) CartDiscountITUtils.populateTargetProject(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateTargetProject) CartDiscountITUtils.populateSourceProject(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateSourceProject) Collections.emptyMap(java.util.Collections.emptyMap) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) CartDiscountValue(io.sphere.sdk.cartdiscounts.CartDiscountValue) AbsoluteCartDiscountValue(io.sphere.sdk.cartdiscounts.AbsoluteCartDiscountValue) CartDiscountITUtils.createCartDiscountCustomType(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.createCartDiscountCustomType) SetCustomType(io.sphere.sdk.cartdiscounts.commands.updateactions.SetCustomType) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) List(java.util.List) CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource) ShippingCostTarget(io.sphere.sdk.cartdiscounts.ShippingCostTarget) Collections(java.util.Collections) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) 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) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Test(org.junit.jupiter.api.Test)

Example 3 with CartDiscountSyncStatistics

use of com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics 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 4 with CartDiscountSyncStatistics

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

the class CartDiscountSyncIT method sync_WithUpdatedCartDiscount_WithNewCartPredicate_ShouldUpdateCartDiscountWithNewCartPredicate.

@Test
void sync_WithUpdatedCartDiscount_WithNewCartPredicate_ShouldUpdateCartDiscountWithNewCartPredicate() {
    // preparation
    final CartDiscountDraft newCartDiscountDraftWithExistingKey = CartDiscountDraftBuilder.of(CART_DISCOUNT_DRAFT_1).cartPredicate(CART_DISCOUNT_CART_PREDICATE_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(ChangeCartPredicate.of(CART_DISCOUNT_CART_PREDICATE_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 5 with CartDiscountSyncStatistics

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

the class CartDiscountSyncIT method sync_WithUpdatedCartDiscount_WithNewCustomField_ShouldUpdateCartDiscountWithNewCustomField.

@Test
void sync_WithUpdatedCartDiscount_WithNewCustomField_ShouldUpdateCartDiscountWithNewCustomField() {
    // preparation
    final Map<String, JsonNode> customFieldsJsons = createCustomFieldsJsonMap();
    customFieldsJsons.put(BOOLEAN_CUSTOM_FIELD_NAME, JsonNodeFactory.instance.booleanNode(true));
    final CartDiscountDraft newCartDiscountDraftWithExistingKey = CartDiscountDraftBuilder.of(CART_DISCOUNT_DRAFT_1).custom(CustomFieldsDraft.ofTypeKeyAndJson(OLD_CART_DISCOUNT_TYPE_KEY, customFieldsJsons)).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(SetCustomField.ofJson(BOOLEAN_CUSTOM_FIELD_NAME, JsonNodeFactory.instance.booleanNode(true)));
    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) JsonNode(com.fasterxml.jackson.databind.JsonNode) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) Test(org.junit.jupiter.api.Test)

Aggregations

CartDiscountSyncStatistics (com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics)22 Test (org.junit.jupiter.api.Test)22 CartDiscountDraft (io.sphere.sdk.cartdiscounts.CartDiscountDraft)21 CartDiscount (io.sphere.sdk.cartdiscounts.CartDiscount)19 CartDiscountSync (com.commercetools.sync.cartdiscounts.CartDiscountSync)18 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)18 CartDiscountDraftBuilder (io.sphere.sdk.cartdiscounts.CartDiscountDraftBuilder)18 CustomFieldsDraft (io.sphere.sdk.types.CustomFieldsDraft)18 ArrayList (java.util.ArrayList)18 Collections.emptyMap (java.util.Collections.emptyMap)18 List (java.util.List)18 Locale (java.util.Locale)18 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)18 SyncException (com.commercetools.sync.commons.exceptions.SyncException)16 SphereClient (io.sphere.sdk.client.SphereClient)16 String.format (java.lang.String.format)16 Collections.singletonList (java.util.Collections.singletonList)16 Optional (java.util.Optional)16 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)16 CompletionException (java.util.concurrent.CompletionException)16