use of com.commercetools.sync.services.TaxCategoryService in project commercetools-sync-java by commercetools.
the class TaxCategoryServiceImplIT method fetchMatchingTaxCategoriesByKeys_WithBadGateWayExceptionAlways_ShouldFail.
@Test
void fetchMatchingTaxCategoriesByKeys_WithBadGateWayExceptionAlways_ShouldFail() {
// Mock sphere client to return BadGatewayException on any request.
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
when(spyClient.execute(any(TaxCategoryQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
final TaxCategorySyncOptions spyOptions = TaxCategorySyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final TaxCategoryService spyTaxCategoryService = new TaxCategoryServiceImpl(spyOptions);
final Set<String> keys = new HashSet<>();
keys.add(TAXCATEGORY_KEY);
// test and assert
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(spyTaxCategoryService.fetchMatchingTaxCategoriesByKeys(keys)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
use of com.commercetools.sync.services.TaxCategoryService in project commercetools-sync-java by commercetools.
the class TaxCategoryServiceImplIT method createTaxCategory_WithValidTaxCategory_ShouldCreateTaxCategoryAndCacheId.
@Test
void createTaxCategory_WithValidTaxCategory_ShouldCreateTaxCategoryAndCacheId() {
final TaxCategoryDraft newTaxCategoryDraft = TaxCategoryDraftBuilder.of(TAXCATEGORY_NAME_1, singletonList(createTaxRateDraft()), TAXCATEGORY_DESCRIPTION_1).key(TAXCATEGORY_KEY_1).build();
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final TaxCategorySyncOptions spyOptions = TaxCategorySyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final TaxCategoryService spyTaxCategoryService = new TaxCategoryServiceImpl(spyOptions);
// test
final Optional<TaxCategory> createdTaxCategory = spyTaxCategoryService.createTaxCategory(newTaxCategoryDraft).toCompletableFuture().join();
final Optional<TaxCategory> queriedOptional = CTP_TARGET_CLIENT.execute(TaxCategoryQuery.of().withPredicates(taxCategoryQueryModel -> taxCategoryQueryModel.key().is(TAXCATEGORY_KEY_1))).toCompletableFuture().join().head();
assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdTaxCategory).hasValueSatisfying(created -> {
assertThat(created.getKey()).isEqualTo(queried.getKey());
assertThat(created.getDescription()).isEqualTo(queried.getDescription());
assertThat(created.getName()).isEqualTo(queried.getName());
}));
// Assert that the created taxCategory is cached
final Optional<String> taxCategoryId = spyTaxCategoryService.fetchCachedTaxCategoryId(TAXCATEGORY_KEY_1).toCompletableFuture().join();
assertThat(taxCategoryId).isPresent();
verify(spyClient, times(0)).execute(any(TaxCategoryQuery.class));
}
use of com.commercetools.sync.services.TaxCategoryService in project commercetools-sync-java by commercetools.
the class TaxCategoryServiceImplIT method createTaxCategory_WithInvalidTaxCategory_ShouldHaveEmptyOptionalAsAResult.
@Test
void createTaxCategory_WithInvalidTaxCategory_ShouldHaveEmptyOptionalAsAResult() {
// preparation
final TaxCategoryDraft newTaxCategoryDraft = TaxCategoryDraftBuilder.of(TAXCATEGORY_NAME_1, singletonList(createTaxRateDraft()), TAXCATEGORY_DESCRIPTION_1).key("").build();
final TaxCategorySyncOptions options = TaxCategorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final TaxCategoryService taxCategoryService = new TaxCategoryServiceImpl(options);
// test
final Optional<TaxCategory> result = taxCategoryService.createTaxCategory(newTaxCategoryDraft).toCompletableFuture().join();
// assertion
assertThat(result).isEmpty();
assertThat(errorCallBackMessages).containsExactly("Failed to create draft with key: ''. Reason: Draft key is blank!");
}
use of com.commercetools.sync.services.TaxCategoryService in project commercetools-sync-java by commercetools.
the class TaxCategoryServiceImplIT method setup.
/**
* Deletes tax categories from the target CTP projects, then it populates target CTP project with
* test data.
*/
@BeforeEach
void setup() {
errorCallBackMessages = new ArrayList<>();
errorCallBackExceptions = new ArrayList<>();
deleteTaxCategories(CTP_TARGET_CLIENT);
warnings = new ArrayList<>();
oldTaxCategory = createTaxCategory(CTP_TARGET_CLIENT);
final TaxCategorySyncOptions taxCategorySyncOptions = TaxCategorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
taxCategoryService = new TaxCategoryServiceImpl(taxCategorySyncOptions);
}
use of com.commercetools.sync.services.TaxCategoryService in project commercetools-sync-java by commercetools.
the class TaxCategorySyncTest method sync_WithErrorFetchingExistingKeys_ShouldApplyErrorCallbackAndIncrementFailed.
@Test
void sync_WithErrorFetchingExistingKeys_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(), null).key("someKey").build();
when(taxCategoryService.fetchMatchingTaxCategoriesByKeys(any())).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final TaxCategorySyncStatistics result = sync.sync(singletonList(draft)).toCompletableFuture().join();
assertAll(() -> assertThat(result.getProcessed().get()).isEqualTo(1), () -> assertThat(errors).hasSize(1), () -> assertThat(errors).contains("Failed to fetch existing tax categories with keys: '[someKey]'."));
verify(taxCategoryService, times(1)).fetchMatchingTaxCategoriesByKeys(any());
verifyNoMoreInteractions(taxCategoryService);
}
Aggregations