Search in sources :

Example 6 with TaxCategorySyncStatistics

use of com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics in project commercetools-sync-java by commercetools.

the class TaxCategorySyncTest method sync_WithDuplicatedCountryCode_ShouldNotBuildActionAndTriggerErrorCallback.

@Test
void sync_WithDuplicatedCountryCode_ShouldNotBuildActionAndTriggerErrorCallback() {
    final String name = "DuplicatedName";
    final TaxCategoryDraft draft = TaxCategoryDraftBuilder.of(name, asList(// replace
    TaxRateDraftBuilder.of(name, 2.0, false, CountryCode.FR).build(), TaxRateDraftBuilder.of(name, 2.0, false, CountryCode.FR).build()), "desc").key("someKey").build();
    final AtomicReference<String> callback = new AtomicReference<>(null);
    final TaxCategorySyncOptions syncOptions = TaxCategorySyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, taxDraft, entry, actions) -> callback.set(exception.getMessage())).build();
    final TaxCategorySync sync = new TaxCategorySync(syncOptions, taxCategoryService);
    final TaxCategory taxCategory = mock(TaxCategory.class);
    when(taxCategory.getId()).thenReturn("id");
    when(taxCategory.getKey()).thenReturn("someKey");
    when(taxCategoryService.fetchMatchingTaxCategoriesByKeys(any())).thenReturn(completedFuture(new HashSet<>(singletonList(taxCategory))));
    when(taxCategoryService.updateTaxCategory(any(), any())).thenReturn(completedFuture(taxCategory));
    final TaxCategorySyncStatistics result = sync.sync(singletonList(draft)).toCompletableFuture().join();
    assertAll(() -> assertThat(result.getProcessed().get()).isEqualTo(1), () -> assertThat(result.getUpdated().get()).isEqualTo(0), () -> assertThat(result.getFailed().get()).isEqualTo(1), () -> assertThat(callback.get()).contains(format("Tax rate drafts have duplicated country codes. Duplicated " + "tax rate country code: '%s'. Tax rate country codes and states are " + "expected to be unique inside their tax category.", CountryCode.FR)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Optional.empty(java.util.Optional.empty) TaxCategorySyncStatistics(com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics) TaxCategoryDraft(io.sphere.sdk.taxcategories.TaxCategoryDraft) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Arrays.asList(java.util.Arrays.asList) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) SphereClient(io.sphere.sdk.client.SphereClient) SphereException(io.sphere.sdk.models.SphereException) TaxCategoryService(com.commercetools.sync.services.TaxCategoryService) Assertions.assertAll(org.junit.jupiter.api.Assertions.assertAll) TaxCategoryDraftBuilder(io.sphere.sdk.taxcategories.TaxCategoryDraftBuilder) CountryCode(com.neovisionaries.i18n.CountryCode) Collections.emptySet(java.util.Collections.emptySet) Collections.emptyList(java.util.Collections.emptyList) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) TaxRateDraftBuilder(io.sphere.sdk.taxcategories.TaxRateDraftBuilder) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) Optional(java.util.Optional) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Mockito.reset(org.mockito.Mockito.reset) Mockito.mock(org.mockito.Mockito.mock) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) TaxCategoryDraft(io.sphere.sdk.taxcategories.TaxCategoryDraft) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) TaxCategorySyncStatistics(com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics) SphereClient(io.sphere.sdk.client.SphereClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 7 with TaxCategorySyncStatistics

use of com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics in project commercetools-sync-java by commercetools.

the class TaxCategorySyncTest method sync_WithErrorUpdating_ShouldApplyErrorCallbackAndIncrementFailed.

@Test
void sync_WithErrorUpdating_ShouldApplyErrorCallbackAndIncrementFailed() {
    final List<String> errors = new ArrayList<>();
    final TaxCategorySyncOptions options = TaxCategorySyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, draft, entry, actions) -> errors.add(exception.getMessage())).build();
    final TaxCategorySync sync = new TaxCategorySync(options, taxCategoryService);
    final TaxCategoryDraft draft = TaxCategoryDraftBuilder.of("someName", emptyList(), "changed").key("someKey").build();
    final TaxCategory taxCategory = mock(TaxCategory.class);
    when(taxCategory.getKey()).thenReturn("someKey");
    when(taxCategoryService.fetchMatchingTaxCategoriesByKeys(any())).thenReturn(completedFuture(new HashSet<>(singletonList(taxCategory))));
    when(taxCategoryService.updateTaxCategory(any(), any())).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final TaxCategorySyncStatistics result = sync.sync(singletonList(draft)).toCompletableFuture().join();
    assertAll(() -> assertThat(result.getProcessed().get()).isEqualTo(1), () -> assertThat(result.getUpdated().get()).isEqualTo(0), () -> assertThat(result.getFailed().get()).isEqualTo(1), () -> assertThat(errors).hasSize(1));
    verify(taxCategoryService, times(1)).fetchMatchingTaxCategoriesByKeys(any());
    verify(taxCategoryService, times(1)).updateTaxCategory(any(), any());
    verifyNoMoreInteractions(taxCategoryService);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Optional.empty(java.util.Optional.empty) TaxCategorySyncStatistics(com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics) TaxCategoryDraft(io.sphere.sdk.taxcategories.TaxCategoryDraft) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Arrays.asList(java.util.Arrays.asList) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) SphereClient(io.sphere.sdk.client.SphereClient) SphereException(io.sphere.sdk.models.SphereException) TaxCategoryService(com.commercetools.sync.services.TaxCategoryService) Assertions.assertAll(org.junit.jupiter.api.Assertions.assertAll) TaxCategoryDraftBuilder(io.sphere.sdk.taxcategories.TaxCategoryDraftBuilder) CountryCode(com.neovisionaries.i18n.CountryCode) Collections.emptySet(java.util.Collections.emptySet) Collections.emptyList(java.util.Collections.emptyList) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) TaxRateDraftBuilder(io.sphere.sdk.taxcategories.TaxRateDraftBuilder) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) Optional(java.util.Optional) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Mockito.reset(org.mockito.Mockito.reset) Mockito.mock(org.mockito.Mockito.mock) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) TaxCategoryDraft(io.sphere.sdk.taxcategories.TaxCategoryDraft) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) TaxCategorySyncStatistics(com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics) SphereClient(io.sphere.sdk.client.SphereClient) ArrayList(java.util.ArrayList) SphereException(io.sphere.sdk.models.SphereException) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 8 with TaxCategorySyncStatistics

use of com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics in project commercetools-sync-java by commercetools.

the class TaxCategorySyncTest method sync_WithErrorUpdatingAndTryingToRecoverWithFetchException_ShouldApplyErrorCallbackAndIncrementFailed.

@Test
void sync_WithErrorUpdatingAndTryingToRecoverWithFetchException_ShouldApplyErrorCallbackAndIncrementFailed() {
    final List<String> errors = new ArrayList<>();
    final TaxCategorySyncOptions options = TaxCategorySyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, draft, entry, actions) -> errors.add(exception.getMessage())).build();
    final TaxCategorySync sync = new TaxCategorySync(options, taxCategoryService);
    final TaxCategoryDraft draft = TaxCategoryDraftBuilder.of("someName", emptyList(), "changed").key("someKey").build();
    final TaxCategory taxCategory = mock(TaxCategory.class);
    when(taxCategory.getKey()).thenReturn("someKey");
    when(taxCategoryService.fetchMatchingTaxCategoriesByKeys(any())).thenReturn(completedFuture(new HashSet<>(singletonList(taxCategory))));
    when(taxCategoryService.updateTaxCategory(any(), any())).thenReturn(supplyAsync(() -> {
        throw new io.sphere.sdk.client.ConcurrentModificationException();
    }));
    when(taxCategoryService.fetchTaxCategory(any())).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final TaxCategorySyncStatistics result = sync.sync(singletonList(draft)).toCompletableFuture().join();
    assertAll(() -> assertThat(result.getProcessed().get()).isEqualTo(1), () -> assertThat(result.getUpdated().get()).isEqualTo(0), () -> assertThat(result.getFailed().get()).isEqualTo(1), () -> assertThat(errors).hasSize(1), () -> assertThat(errors).singleElement(as(STRING)).contains("Failed to fetch from CTP while retrying after concurrency modification."));
    verify(taxCategoryService, times(1)).fetchMatchingTaxCategoriesByKeys(any());
    verify(taxCategoryService, times(1)).updateTaxCategory(any(), any());
    verify(taxCategoryService, times(1)).fetchTaxCategory(any());
    verifyNoMoreInteractions(taxCategoryService);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Optional.empty(java.util.Optional.empty) TaxCategorySyncStatistics(com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics) TaxCategoryDraft(io.sphere.sdk.taxcategories.TaxCategoryDraft) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Arrays.asList(java.util.Arrays.asList) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) SphereClient(io.sphere.sdk.client.SphereClient) SphereException(io.sphere.sdk.models.SphereException) TaxCategoryService(com.commercetools.sync.services.TaxCategoryService) Assertions.assertAll(org.junit.jupiter.api.Assertions.assertAll) TaxCategoryDraftBuilder(io.sphere.sdk.taxcategories.TaxCategoryDraftBuilder) CountryCode(com.neovisionaries.i18n.CountryCode) Collections.emptySet(java.util.Collections.emptySet) Collections.emptyList(java.util.Collections.emptyList) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) TaxRateDraftBuilder(io.sphere.sdk.taxcategories.TaxRateDraftBuilder) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) Optional(java.util.Optional) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Mockito.reset(org.mockito.Mockito.reset) Mockito.mock(org.mockito.Mockito.mock) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) TaxCategorySyncStatistics(com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics) ArrayList(java.util.ArrayList) SphereException(io.sphere.sdk.models.SphereException) TaxCategoryDraft(io.sphere.sdk.taxcategories.TaxCategoryDraft) SphereClient(io.sphere.sdk.client.SphereClient) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 9 with TaxCategorySyncStatistics

use of com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics in project commercetools-sync-java by commercetools.

the class TaxCategorySyncTest method sync_WithFilteredActions_ShouldApplyBeforeUpdateCallbackAndNotIncrementUpdated.

@Test
void sync_WithFilteredActions_ShouldApplyBeforeUpdateCallbackAndNotIncrementUpdated() {
    final AtomicBoolean callbackApplied = new AtomicBoolean(false);
    final TaxCategorySyncOptions options = TaxCategorySyncOptionsBuilder.of(mock(SphereClient.class)).beforeUpdateCallback((actions, draft, old) -> {
        callbackApplied.set(true);
        return emptyList();
    }).build();
    final TaxCategorySync sync = new TaxCategorySync(options, taxCategoryService);
    final TaxCategoryDraft draft = TaxCategoryDraftBuilder.of("someName", emptyList(), "changed").key("someKey").build();
    final TaxCategory taxCategory = mock(TaxCategory.class);
    when(taxCategory.getId()).thenReturn("id");
    when(taxCategory.getKey()).thenReturn("someKey");
    when(taxCategoryService.fetchMatchingTaxCategoriesByKeys(any())).thenReturn(completedFuture(new HashSet<>(singletonList(taxCategory))));
    final TaxCategorySyncStatistics result = sync.sync(singletonList(draft)).toCompletableFuture().join();
    assertAll(() -> assertThat(result.getProcessed().get()).isEqualTo(1), () -> assertThat(result.getUpdated().get()).isEqualTo(0), () -> assertThat(result.getFailed().get()).isEqualTo(0), () -> assertThat(callbackApplied.get()).isTrue());
    verify(taxCategoryService, times(1)).fetchMatchingTaxCategoriesByKeys(any());
    verifyNoMoreInteractions(taxCategoryService);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Optional.empty(java.util.Optional.empty) TaxCategorySyncStatistics(com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics) TaxCategoryDraft(io.sphere.sdk.taxcategories.TaxCategoryDraft) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Arrays.asList(java.util.Arrays.asList) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) SphereClient(io.sphere.sdk.client.SphereClient) SphereException(io.sphere.sdk.models.SphereException) TaxCategoryService(com.commercetools.sync.services.TaxCategoryService) Assertions.assertAll(org.junit.jupiter.api.Assertions.assertAll) TaxCategoryDraftBuilder(io.sphere.sdk.taxcategories.TaxCategoryDraftBuilder) CountryCode(com.neovisionaries.i18n.CountryCode) Collections.emptySet(java.util.Collections.emptySet) Collections.emptyList(java.util.Collections.emptyList) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) TaxRateDraftBuilder(io.sphere.sdk.taxcategories.TaxRateDraftBuilder) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) Optional(java.util.Optional) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Mockito.reset(org.mockito.Mockito.reset) Mockito.mock(org.mockito.Mockito.mock) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TaxCategoryDraft(io.sphere.sdk.taxcategories.TaxCategoryDraft) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) TaxCategorySyncStatistics(com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics) SphereClient(io.sphere.sdk.client.SphereClient) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 10 with TaxCategorySyncStatistics

use of com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics in project commercetools-sync-java by commercetools.

the class TaxCategorySyncIT method sync_withNewTaxCategory_shouldCreateTaxCategory.

@Test
void sync_withNewTaxCategory_shouldCreateTaxCategory() {
    final SubRate subRate1 = SubRate.of("subRate-1", 0.05);
    final SubRate subRate2 = SubRate.of("subRate-2", 0.06);
    final TaxRateDraft taxRateDraft = TaxRateDraftBuilder.of("%11 US", 0.11, false, CountryCode.US).subRates(asList(subRate1, subRate2)).build();
    final TaxCategoryDraft taxCategoryDraft = TaxCategoryDraftBuilder.of("tax-category-name-new", singletonList(taxRateDraft), "tax-category-description-new").key("tax-category-key-new").build();
    final TaxCategorySyncOptions taxCategorySyncOptions = TaxCategorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
    final TaxCategorySync taxCategorySync = new TaxCategorySync(taxCategorySyncOptions);
    // test
    final TaxCategorySyncStatistics taxCategorySyncStatistics = taxCategorySync.sync(singletonList(taxCategoryDraft)).toCompletableFuture().join();
    assertThat(taxCategorySyncStatistics).hasValues(1, 1, 0, 0);
}
Also used : TaxCategoryDraft(io.sphere.sdk.taxcategories.TaxCategoryDraft) TaxCategorySync(com.commercetools.sync.taxcategories.TaxCategorySync) TaxCategorySyncOptions(com.commercetools.sync.taxcategories.TaxCategorySyncOptions) TaxCategorySyncStatistics(com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics) SubRate(io.sphere.sdk.taxcategories.SubRate) TaxRateDraft(io.sphere.sdk.taxcategories.TaxRateDraft) Test(org.junit.jupiter.api.Test)

Aggregations

TaxCategorySyncStatistics (com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics)17 TaxCategoryDraft (io.sphere.sdk.taxcategories.TaxCategoryDraft)17 Test (org.junit.jupiter.api.Test)17 TaxCategory (io.sphere.sdk.taxcategories.TaxCategory)15 CountryCode (com.neovisionaries.i18n.CountryCode)14 ConcurrentModificationException (io.sphere.sdk.client.ConcurrentModificationException)14 SphereClient (io.sphere.sdk.client.SphereClient)14 TaxCategoryDraftBuilder (io.sphere.sdk.taxcategories.TaxCategoryDraftBuilder)14 TaxRateDraftBuilder (io.sphere.sdk.taxcategories.TaxRateDraftBuilder)14 String.format (java.lang.String.format)14 ArrayList (java.util.ArrayList)14 Arrays.asList (java.util.Arrays.asList)14 Collections.singletonList (java.util.Collections.singletonList)14 List (java.util.List)14 Optional (java.util.Optional)14 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)14 Mockito.when (org.mockito.Mockito.when)14 TaxCategoryService (com.commercetools.sync.services.TaxCategoryService)11 SphereException (io.sphere.sdk.models.SphereException)11 Collections.emptyList (java.util.Collections.emptyList)11