use of com.commercetools.sync.types.TypeSyncOptions 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.types.TypeSyncOptions 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.types.TypeSyncOptions 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.types.TypeSyncOptions 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.types.TypeSyncOptions in project commercetools-sync-java by commercetools.
the class BuildFieldDefinitionUpdateActionsTest method buildFieldDefinitionsUpdateActions_WithDuplicateFieldDefNames_ShouldNotBuildActionsAndTriggerErrorCb.
@Test
void buildFieldDefinitionsUpdateActions_WithDuplicateFieldDefNames_ShouldNotBuildActionsAndTriggerErrorCb() {
final Type oldType = readObjectFromResource(TYPE_WITH_FIELDS_ABC, Type.class);
final TypeDraft newTypeDraft = readObjectFromResource(TYPE_WITH_FIELDS_ABB, TypeDraft.class);
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final TypeSyncOptions syncOptions = TypeSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final List<UpdateAction<Type>> updateActions = buildFieldDefinitionsUpdateActions(oldType, newTypeDraft, syncOptions);
assertThat(updateActions).isEmpty();
assertThat(errorMessages).hasSize(1);
assertThat(errorMessages.get(0)).matches("Failed to build update actions for the field definitions of the " + "type with the key 'key'. Reason: .*DuplicateNameException: Field definitions " + "have duplicated names. Duplicated field definition name: 'b'. Field definitions names are " + "expected to be unique inside their type.");
assertThat(exceptions).hasSize(1);
assertThat(exceptions.get(0)).isExactlyInstanceOf(BuildUpdateActionException.class);
assertThat(exceptions.get(0).getMessage()).contains("Field definitions have duplicated names. " + "Duplicated field definition name: 'b'. Field definitions names are expected to be unique " + "inside their type.");
assertThat(exceptions.get(0).getCause()).isExactlyInstanceOf(DuplicateNameException.class);
}
Aggregations