use of com.commercetools.sync.services.impl.StateServiceImpl in project commercetools-sync-java by commercetools.
the class StateServiceImplIT method fetchMatchingStatesByKeys_WithBadGateWayExceptionAlways_ShouldFail.
@Test
void fetchMatchingStatesByKeys_WithBadGateWayExceptionAlways_ShouldFail() {
// Mock sphere client to return BadGatewayException on any request.
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
when(spyClient.execute(any(StateQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
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);
final Set<String> keys = new HashSet<>();
keys.add(OLD_STATE_KEY);
// test and assert
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(spyStateService.fetchMatchingStatesByKeys(keys)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
use of com.commercetools.sync.services.impl.StateServiceImpl 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));
}
use of com.commercetools.sync.services.impl.StateServiceImpl in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithStateWithEmptyTransitionShouldBeResolved_ShouldAddErrorMessage.
@Test
void sync_WithStateWithEmptyTransitionShouldBeResolved_ShouldAddErrorMessage() {
final StateDraft stateCDraft = StateDraftBuilder.of(keyC, StateType.REVIEW_STATE).roles(Collections.singleton(StateRole.REVIEW_INCLUDED_IN_STATISTICS)).transitions(new HashSet<>(Arrays.asList(Reference.of(State.referenceTypeId(), "")))).initial(true).build();
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(3).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).warningCallback((exception, newResource, oldResource) -> warningCallBackMessages.add(exception.getMessage())).build();
// test
StateServiceImpl stateService = new StateServiceImpl(stateSyncOptions);
final StateReferenceResolver stateReferenceResolver = new StateReferenceResolver(stateSyncOptions, stateService);
CompletionStage<StateDraft> result = stateReferenceResolver.resolveReferences(stateCDraft);
result.exceptionally(exception -> {
Assertions.assertThat(exception.getMessage()).contains(format("Failed to resolve 'transition' reference on StateDraft with key:'%s", keyC));
return null;
}).toCompletableFuture().join();
}
Aggregations