Search in sources :

Example 16 with TaxCategoryService

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

the class TaxCategorySyncTest method sync_WithErrorCreating_ShouldIncrementFailedButNotApplyErrorCallback.

@Test
void sync_WithErrorCreating_ShouldIncrementFailedButNotApplyErrorCallback() {
    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(), null).key("someKey").build();
    when(taxCategoryService.fetchMatchingTaxCategoriesByKeys(any())).thenReturn(completedFuture(emptySet()));
    when(taxCategoryService.createTaxCategory(any())).thenReturn(completedFuture(empty()));
    final TaxCategorySyncStatistics result = sync.sync(singletonList(draft)).toCompletableFuture().join();
    assertAll(() -> assertThat(result.getProcessed().get()).isEqualTo(1), () -> assertThat(result.getFailed().get()).isEqualTo(1), () -> assertThat(errors).isEmpty());
    verify(taxCategoryService, times(1)).fetchMatchingTaxCategoriesByKeys(any());
    verify(taxCategoryService, times(1)).createTaxCategory(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) TaxCategorySyncStatistics(com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics) SphereClient(io.sphere.sdk.client.SphereClient) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 17 with TaxCategoryService

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

the class TaxCategorySyncTest method sync_WithErrorUpdatingAndTryingToRecoverWithEmptyResponse_ShouldApplyErrorCallbackAndIncrementFailed.

@Test
void sync_WithErrorUpdatingAndTryingToRecoverWithEmptyResponse_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 ConcurrentModificationException();
    }));
    when(taxCategoryService.fetchTaxCategory(any())).thenReturn(completedFuture(Optional.empty()));
    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("Not found when attempting to fetch 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) TaxCategoryDraft(io.sphere.sdk.taxcategories.TaxCategoryDraft) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) TaxCategorySyncStatistics(com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics) SphereClient(io.sphere.sdk.client.SphereClient) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

TaxCategoryService (com.commercetools.sync.services.TaxCategoryService)17 SphereClient (io.sphere.sdk.client.SphereClient)16 TaxCategory (io.sphere.sdk.taxcategories.TaxCategory)16 TaxCategoryDraft (io.sphere.sdk.taxcategories.TaxCategoryDraft)16 TaxCategoryDraftBuilder (io.sphere.sdk.taxcategories.TaxCategoryDraftBuilder)16 ArrayList (java.util.ArrayList)16 Collections.singletonList (java.util.Collections.singletonList)16 HashSet (java.util.HashSet)16 List (java.util.List)16 Optional (java.util.Optional)16 Assertions.as (org.assertj.core.api.Assertions.as)16 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)16 STRING (org.assertj.core.api.InstanceOfAssertFactories.STRING)16 Test (org.junit.jupiter.api.Test)16 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)16 Mockito.times (org.mockito.Mockito.times)16 Mockito.verify (org.mockito.Mockito.verify)16 Mockito.when (org.mockito.Mockito.when)16 TaxCategorySyncStatistics (com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics)11 CountryCode (com.neovisionaries.i18n.CountryCode)11