use of com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics in project commercetools-sync-java by commercetools.
the class TaxCategorySyncIT method sync_withEqualTaxCategory_shouldNotUpdateTaxCategory.
@Test
void sync_withEqualTaxCategory_shouldNotUpdateTaxCategory() {
final SubRate subRate1 = SubRate.of("subRate-1", 0.08);
final SubRate subRate2 = SubRate.of("subRate-2", 0.11);
final TaxRateDraft taxRateDraft = TaxRateDraftBuilder.of("%19 VAT DE", 0.19, false, CountryCode.DE).subRates(asList(subRate1, subRate2)).build();
final TaxCategoryDraft taxCategoryDraft = TaxCategoryDraftBuilder.of("tax-category-name", singletonList(taxRateDraft), "tax-category-description").key("tax-category-key").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, 0, 0, 0);
}
use of com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics in project commercetools-sync-java by commercetools.
the class TaxCategorySyncTest method sync_WithDuplicatedState_ShouldNotBuildActionAndTriggerErrorCallback.
@Test
void sync_WithDuplicatedState_ShouldNotBuildActionAndTriggerErrorCallback() {
final String name = "DuplicatedName";
final TaxCategoryDraft draft = TaxCategoryDraftBuilder.of(name, asList(// replace
TaxRateDraftBuilder.of(name, 2.0, false, CountryCode.FR).state("NYON").build(), TaxRateDraftBuilder.of(name, 2.0, false, CountryCode.FR).state("PARIS").build(), TaxRateDraftBuilder.of(name, 3.0, false, CountryCode.DE).state("BERLIN").build(), TaxRateDraftBuilder.of(name, 3.0, false, CountryCode.DE).state("BERLIN").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 and states. Duplicated " + "tax rate country code: '%s'. state : '%s'. Tax rate country codes and states are " + "expected to be unique inside their tax category.", CountryCode.DE, "BERLIN")));
}
use of com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics in project commercetools-sync-java by commercetools.
the class TaxCategorySyncTest method sync_WithInvalidDrafts_ShouldApplyErrorCallbackAndIncrementFailed.
@Test
void sync_WithInvalidDrafts_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 withoutKeyDraft = TaxCategoryDraftBuilder.of(null, emptyList(), null).build();
final TaxCategorySyncStatistics result = sync.sync(asList(null, withoutKeyDraft)).toCompletableFuture().join();
assertAll(() -> assertThat(result.getProcessed().get()).isEqualTo(2), () -> assertThat(result.getFailed().get()).isEqualTo(2), () -> assertThat(errors).hasSize(2), () -> assertThat(errors).contains("TaxCategoryDraft is null.", "TaxCategoryDraft with name: null doesn't have a key. " + "Please make sure all tax category drafts have keys."));
verifyNoMoreInteractions(taxCategoryService);
}
use of com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics in project commercetools-sync-java by commercetools.
the class TaxCategorySyncTest method sync_WithNoError_ShouldApplyBeforeCreateCallbackAndIncrementCreated.
@Test
void sync_WithNoError_ShouldApplyBeforeCreateCallbackAndIncrementCreated() {
final AtomicBoolean callbackApplied = new AtomicBoolean(false);
final TaxCategorySyncOptions options = TaxCategorySyncOptionsBuilder.of(mock(SphereClient.class)).beforeCreateCallback((draft) -> {
callbackApplied.set(true);
return draft;
}).build();
final TaxCategorySync sync = new TaxCategorySync(options, taxCategoryService);
final TaxCategoryDraft draft = TaxCategoryDraftBuilder.of("someName", emptyList(), null).key("someKey").build();
final TaxCategory taxCategory = mock(TaxCategory.class);
when(taxCategoryService.fetchMatchingTaxCategoriesByKeys(any())).thenReturn(completedFuture(emptySet()));
when(taxCategoryService.createTaxCategory(any())).thenReturn(completedFuture(Optional.of(taxCategory)));
final TaxCategorySyncStatistics result = sync.sync(singletonList(draft)).toCompletableFuture().join();
assertAll(() -> assertThat(result.getProcessed().get()).isEqualTo(1), () -> assertThat(result.getCreated().get()).isEqualTo(1), () -> assertThat(result.getFailed().get()).isEqualTo(0), () -> assertThat(callbackApplied.get()).isTrue());
verify(taxCategoryService, times(1)).fetchMatchingTaxCategoriesByKeys(any());
verify(taxCategoryService, times(1)).createTaxCategory(any());
verifyNoMoreInteractions(taxCategoryService);
}
use of com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics in project commercetools-sync-java by commercetools.
the class TaxCategorySyncTest method sync_WithNoError_ShouldApplyBeforeUpdateCallbackAndIncrementUpdated.
@Test
void sync_WithNoError_ShouldApplyBeforeUpdateCallbackAndIncrementUpdated() {
final AtomicBoolean callbackApplied = new AtomicBoolean(false);
final TaxCategorySyncOptions options = TaxCategorySyncOptionsBuilder.of(mock(SphereClient.class)).beforeUpdateCallback((actions, draft, old) -> {
callbackApplied.set(true);
return actions;
}).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))));
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(1), () -> assertThat(result.getFailed().get()).isEqualTo(0), () -> assertThat(callbackApplied.get()).isTrue());
verify(taxCategoryService, times(1)).fetchMatchingTaxCategoriesByKeys(any());
verify(taxCategoryService, times(1)).updateTaxCategory(any(), any());
verifyNoMoreInteractions(taxCategoryService);
}
Aggregations