use of com.commercetools.sync.services.TaxCategoryService 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)));
}
use of com.commercetools.sync.services.TaxCategoryService 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);
}
use of com.commercetools.sync.services.TaxCategoryService 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);
}
use of com.commercetools.sync.services.TaxCategoryService 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);
}
use of com.commercetools.sync.services.TaxCategoryService in project commercetools-sync-java by commercetools.
the class ProductSyncMockUtils method getMockTaxCategoryService.
/**
* Creates a mock {@link TaxCategoryService} that returns a completed {@link CompletableFuture}
* containing an {@link Optional} containing the id of the supplied value whenever the following
* method is called on the service:
*
* <ul>
* <li>{@link TaxCategoryService#fetchCachedTaxCategoryId(String)}
* </ul>
*
* @return the created mock of the {@link TaxCategoryService}.
*/
public static TaxCategoryService getMockTaxCategoryService(@Nonnull final String id) {
final TaxCategoryService taxCategoryService = mock(TaxCategoryService.class);
when(taxCategoryService.fetchCachedTaxCategoryId(anyString())).thenReturn(CompletableFuture.completedFuture(Optional.of(id)));
return taxCategoryService;
}
Aggregations