use of com.commercetools.sync.integration.commons.utils.StateITUtils.STATE_NAME_1 in project commercetools-sync-java by commercetools.
the class StateServiceImplIT method createState_WithDuplicateKey_ShouldHaveEmptyOptionalAsAResult.
@Test
void createState_WithDuplicateKey_ShouldHaveEmptyOptionalAsAResult() {
// preparation
final StateDraft newStateDraft = StateDraftBuilder.of(OLD_STATE_KEY, STATE_TYPE).name(STATE_NAME_1).description(STATE_DESCRIPTION_1).build();
final StateSyncOptions options = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final StateService stateService = new StateServiceImpl(options);
// test
final Optional<State> result = stateService.createState(newStateDraft).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.StateITUtils.STATE_NAME_1 in project commercetools-sync-java by commercetools.
the class StateServiceImplIT method createState_WithInvalidState_ShouldHaveEmptyOptionalAsAResult.
@Test
void createState_WithInvalidState_ShouldHaveEmptyOptionalAsAResult() {
// preparation
final StateDraft newStateDraft = StateDraftBuilder.of("", STATE_TYPE).name(STATE_NAME_1).description(STATE_DESCRIPTION_1).build();
final StateSyncOptions options = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final StateService stateService = new StateServiceImpl(options);
// test
final Optional<State> result = stateService.createState(newStateDraft).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.StateITUtils.STATE_NAME_1 in project commercetools-sync-java by commercetools.
the class StateServiceImplIT method createState_WithValidState_ShouldCreateStateAndCacheId.
@Test
void createState_WithValidState_ShouldCreateStateAndCacheId() {
final StateDraft newStateDraft = StateDraftBuilder.of(STATE_KEY_1, STATE_TYPE).name(STATE_NAME_1).description(STATE_DESCRIPTION_1).build();
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final StateSyncOptions spyOptions = StateSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final StateService spyStateService = new StateServiceImpl(spyOptions);
// test
final Optional<State> createdState = spyStateService.createState(newStateDraft).toCompletableFuture().join();
final Optional<State> queriedOptional = CTP_TARGET_CLIENT.execute(StateQuery.of().withPredicates(stateQueryModel -> stateQueryModel.key().is(STATE_KEY_1))).toCompletableFuture().join().head();
assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdState).hasValueSatisfying(created -> {
assertThat(created.getKey()).isEqualTo(queried.getKey());
assertThat(created.getDescription()).isEqualTo(queried.getDescription());
assertThat(created.getName()).isEqualTo(queried.getName());
}));
// Assert that the created state is cached
final Optional<String> stateId = spyStateService.fetchCachedStateId(STATE_KEY_1).toCompletableFuture().join();
assertThat(stateId).isPresent();
verify(spyClient, times(0)).execute(any(StateQuery.class));
}
Aggregations