use of com.commercetools.sync.integration.commons.utils.TypeITUtils.TYPE_NAME_1 in project commercetools-sync-java by commercetools.
the class TypeSyncIT method sync_WithoutKey_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithoutKey_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
final TypeDraft newTypeDraft = TypeDraftBuilder.of(null, TYPE_NAME_1, ResourceTypeIdsSetBuilder.of().addCategories().build()).description(TYPE_DESCRIPTION_1).fieldDefinitions(asList(FIELD_DEFINITION_1, FIELD_DEFINITION_2)).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final TypeSyncOptions typeSyncOptions = TypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final TypeSync typeSync = new TypeSync(typeSyncOptions);
// test
final TypeSyncStatistics typeSyncStatistics = typeSync.sync(singletonList(newTypeDraft)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo("TypeDraft with name: LocalizedString(en -> name_1) doesn't have a key. " + "Please make sure all type drafts have keys.");
assertThat(exceptions).hasSize(1).singleElement().isNull();
assertThat(typeSyncStatistics).hasValues(1, 0, 0, 1);
}
use of com.commercetools.sync.integration.commons.utils.TypeITUtils.TYPE_NAME_1 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.integration.commons.utils.TypeITUtils.TYPE_NAME_1 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.integration.commons.utils.TypeITUtils.TYPE_NAME_1 in project commercetools-sync-java by commercetools.
the class TypeServiceImplIT method createType_WithValidType_ShouldCreateTypeAndCacheId.
@Test
void createType_WithValidType_ShouldCreateTypeAndCacheId() {
final TypeDraft newTypeDraft = TypeDraftBuilder.of(TYPE_KEY_1, TYPE_NAME_1, ResourceTypeIdsSetBuilder.of().addCategories().build()).description(TYPE_DESCRIPTION_1).fieldDefinitions(singletonList(FIELD_DEFINITION_1)).build();
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
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);
// test
final Optional<Type> createdType = spyTypeService.createType(newTypeDraft).toCompletableFuture().join();
final Optional<Type> queriedOptional = CTP_TARGET_CLIENT.execute(TypeQuery.of().withPredicates(typeQueryModel -> typeQueryModel.key().is(TYPE_KEY_1))).toCompletableFuture().join().head();
assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdType).hasValueSatisfying(created -> {
assertThat(created.getKey()).isEqualTo(queried.getKey());
assertThat(created.getDescription()).isEqualTo(queried.getDescription());
assertThat(created.getName()).isEqualTo(queried.getName());
assertThat(created.getFieldDefinitions()).isEqualTo(queried.getFieldDefinitions());
}));
// Assert that the created type is cached
final Optional<String> typeId = spyTypeService.fetchCachedTypeId(TYPE_KEY_1).toCompletableFuture().join();
assertThat(typeId).isPresent();
verify(spyClient, times(0)).execute(any(TypeQuery.class));
}
use of com.commercetools.sync.integration.commons.utils.TypeITUtils.TYPE_NAME_1 in project commercetools-sync-java by commercetools.
the class TypeSyncIT method sync_WithConcurrentModificationException_ShouldRetryToUpdateNewTypeWithSuccess.
@Test
void sync_WithConcurrentModificationException_ShouldRetryToUpdateNewTypeWithSuccess() {
// Preparation
final SphereClient spyClient = buildClientWithConcurrentModificationUpdate();
final TypeDraft typeDraft = TypeDraftBuilder.of(TYPE_KEY_2, TYPE_NAME_2, ResourceTypeIdsSetBuilder.of().addChannels()).build();
CTP_TARGET_CLIENT.execute(TypeCreateCommand.of(typeDraft)).toCompletableFuture().join();
final TypeDraft updatedDraft = TypeDraftBuilder.of(typeDraft).name(TYPE_NAME_1).build();
final TypeSyncOptions typeSyncOptions = TypeSyncOptionsBuilder.of(spyClient).build();
final TypeSync typeSync = new TypeSync(typeSyncOptions);
// test
final TypeSyncStatistics statistics = typeSync.sync(Collections.singletonList(updatedDraft)).toCompletableFuture().join();
// assertion
assertThat(statistics).hasValues(1, 0, 1, 0);
// Assert CTP state.
final PagedQueryResult<Type> queryResult = CTP_TARGET_CLIENT.execute(TypeQuery.of().plusPredicates(queryModel -> queryModel.key().is(typeDraft.getKey()))).toCompletableFuture().join();
assertThat(queryResult.head()).hasValueSatisfying(type -> assertThat(type.getName()).isEqualTo(TYPE_NAME_1));
}
Aggregations