use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class TypeServiceImplIT method setup.
/**
* Deletes types from the target CTP project, then it populates the project with test data.
*/
@BeforeEach
void setup() {
errorCallBackMessages = new ArrayList<>();
errorCallBackExceptions = new ArrayList<>();
deleteAllCategories(CTP_TARGET_CLIENT);
deleteTypes(CTP_TARGET_CLIENT);
createCategoriesCustomType(OLD_TYPE_KEY, OLD_TYPE_LOCALE, OLD_TYPE_NAME, CTP_TARGET_CLIENT);
final TypeSyncOptions typeSyncOptions = TypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
typeService = new TypeServiceImpl(typeSyncOptions);
}
use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class TypeServiceImplIT method createType_WithInvalidType_ShouldHaveEmptyOptionalAsAResult.
@Test
void createType_WithInvalidType_ShouldHaveEmptyOptionalAsAResult() {
// preparation
final TypeDraft newTypeDraft = TypeDraftBuilder.of("", TYPE_NAME_1, ResourceTypeIdsSetBuilder.of().addCategories().build()).description(TYPE_DESCRIPTION_1).fieldDefinitions(singletonList(FIELD_DEFINITION_1)).build();
final TypeSyncOptions options = TypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final TypeService typeService = new TypeServiceImpl(options);
// test
final Optional<Type> result = typeService.createType(newTypeDraft).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.TypeService in project commercetools-sync-java by commercetools.
the class TypeServiceImplIT method createType_WithDuplicateKey_ShouldHaveEmptyOptionalAsAResult.
@Test
void createType_WithDuplicateKey_ShouldHaveEmptyOptionalAsAResult() {
// preparation
final TypeDraft newTypeDraft = TypeDraftBuilder.of(OLD_TYPE_KEY, TYPE_NAME_1, ResourceTypeIdsSetBuilder.of().addCategories().build()).description(TYPE_DESCRIPTION_1).fieldDefinitions(singletonList(FIELD_DEFINITION_1)).build();
final TypeSyncOptions options = TypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final TypeService typeService = new TypeServiceImpl(options);
// test
final Optional<Type> result = typeService.createType(newTypeDraft).toCompletableFuture().join();
// assertion
assertThat(result).isEmpty();
assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains("A duplicate value");
assertThat(errorCallBackExceptions).hasSize(1).singleElement().matches(exception -> {
assertThat(exception).isExactlyInstanceOf(ErrorResponseException.class);
final ErrorResponseException errorResponseException = (ErrorResponseException) exception;
final List<DuplicateFieldError> fieldErrors = errorResponseException.getErrors().stream().map(sphereError -> {
assertThat(sphereError.getCode()).isEqualTo(DuplicateFieldError.CODE);
return sphereError.as(DuplicateFieldError.class);
}).collect(toList());
return fieldErrors.size() == 1;
});
}
use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class TypeServiceImplIT method fetchMatchingTypesByKeys_WithBadGateWayExceptionAlways_ShouldFail.
@Test
void fetchMatchingTypesByKeys_WithBadGateWayExceptionAlways_ShouldFail() {
// Mock sphere client to return BadGatewayException on any request.
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
when(spyClient.execute(any(TypeQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
final TypeSyncOptions spyOptions = TypeSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final TypeService spyTypeService = new TypeServiceImpl(spyOptions);
final Set<String> keys = new HashSet<>();
keys.add(OLD_TYPE_KEY);
// test and assert
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(spyTypeService.fetchMatchingTypesByKeys(keys)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class CartDiscountSyncTest method sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats.
@Test
void sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats() {
// preparation
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final TypeService typeService = spy(new TypeServiceImpl(cartDiscountSyncOptions));
when(typeService.cacheKeysToIds(anySet())).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final CartDiscountSync cartDiscountSync = new CartDiscountSync(cartDiscountSyncOptions, typeService, mock(CartDiscountService.class));
final CartDiscountDraft newCartDiscountDraftWithCustomType = mock(CartDiscountDraft.class);
when(newCartDiscountDraftWithCustomType.getKey()).thenReturn("cart-discount-key");
when(newCartDiscountDraftWithCustomType.getCustom()).thenReturn(CustomFieldsDraft.ofTypeKeyAndJson("typeKey", emptyMap()));
// test
final CartDiscountSyncStatistics cartDiscountSyncStatistics = cartDiscountSync.sync(singletonList(newCartDiscountDraftWithCustomType)).toCompletableFuture().join();
// assertions
AssertionsForStatistics.assertThat(cartDiscountSyncStatistics).hasValues(1, 0, 0, 1);
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to build a cache of keys to ids.");
assertThat(exceptions).hasSize(1).singleElement().matches(throwable -> {
assertThat(throwable).isExactlyInstanceOf(CompletionException.class);
assertThat(throwable).hasCauseExactlyInstanceOf(SphereException.class);
return true;
});
}
Aggregations