use of com.commercetools.sync.services.StateService 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.services.StateService in project commercetools-sync-java by commercetools.
the class StateSyncTest method sync_WithErrorCachingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorCachingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final StateDraft stateDraft = StateDraftBuilder.of("state-1", StateType.LINE_ITEM_STATE).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final StateSyncOptions syncOptions = StateSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final StateService stateService = spy(new StateServiceImpl(syncOptions));
when(stateService.cacheKeysToIds(anySet())).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final StateSync stateSync = new StateSync(syncOptions, stateService);
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(singletonList(stateDraft)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to build a cache of keys to ids.");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(CompletionException.class).hasCauseExactlyInstanceOf(SphereException.class);
assertThat(stateSyncStatistics).hasValues(1, 0, 0, 1);
}
use of com.commercetools.sync.services.StateService in project commercetools-sync-java by commercetools.
the class StateSyncTest method sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final StateDraft stateDraft = StateDraftBuilder.of("state-1", StateType.LINE_ITEM_STATE).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final SphereClient mockClient = mock(SphereClient.class);
when(mockClient.execute(any(StateQuery.class))).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final StateSyncOptions syncOptions = StateSyncOptionsBuilder.of(mockClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final StateService stateService = spy(new StateServiceImpl(syncOptions));
final Map<String, String> keyToIds = new HashMap<>();
keyToIds.put(stateDraft.getKey(), UUID.randomUUID().toString());
when(stateService.cacheKeysToIds(anySet())).thenReturn(completedFuture(keyToIds));
final StateSync stateSync = new StateSync(syncOptions, stateService);
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(singletonList(stateDraft)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to fetch existing states");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(CompletionException.class).hasCauseExactlyInstanceOf(SphereException.class);
assertThat(stateSyncStatistics).hasValues(1, 0, 0, 1);
}
use of com.commercetools.sync.services.StateService in project commercetools-sync-java by commercetools.
the class StateReferenceResolverTest method resolveReferences_WithStateKeys_ShouldResolveReferences.
@Test
void resolveReferences_WithStateKeys_ShouldResolveReferences() {
// preparation
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(mock(SphereClient.class)).build();
final int nStates = 10;
final List<State> states = IntStream.range(0, nStates).mapToObj(i -> i + "").map(key -> {
final State state = mock(State.class);
when(state.getKey()).thenReturn(key);
when(state.getId()).thenReturn(key);
when(state.toReference()).thenReturn(State.referenceOfId(key));
return state;
}).collect(Collectors.toList());
final StateService mockStateService = mock(StateService.class);
when(mockStateService.fetchMatchingStatesByKeysWithTransitions(any())).thenReturn(CompletableFuture.completedFuture(new HashSet<>(states)));
final Set<Reference<State>> stateReferences = states.stream().map(State::toReference).collect(Collectors.toSet());
final StateDraft stateDraft = StateDraftBuilder.of("state-key", StateType.LINE_ITEM_STATE).transitions(stateReferences).build();
final StateReferenceResolver stateReferenceResolver = new StateReferenceResolver(stateSyncOptions, mockStateService);
// test
final StateDraft resolvedDraft = stateReferenceResolver.resolveReferences(stateDraft).toCompletableFuture().join();
// assertion
assertThat(resolvedDraft.getTransitions()).isNotNull();
assertThat(resolvedDraft.getTransitions()).hasSize(nStates);
assertThat(resolvedDraft.getTransitions()).hasSameElementsAs(stateReferences);
}
use of com.commercetools.sync.services.StateService in project commercetools-sync-java by commercetools.
the class StateReferenceResolverTest method resolveReferences_WithExceptionStateFetch_ShouldNotResolveReference.
@Test
void resolveReferences_WithExceptionStateFetch_ShouldNotResolveReference() {
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(mock(SphereClient.class)).build();
final StateService mockStateService = mock(StateService.class);
final State state = mock(State.class);
when(state.getKey()).thenReturn("state-key");
when(state.getId()).thenReturn("state-id");
when(state.toReference()).thenReturn(State.referenceOfId("state-id"));
when(mockStateService.fetchMatchingStatesByKeysWithTransitions(any())).thenReturn(CompletableFuture.completedFuture(singleton(state)));
final StateDraft stateDraft = StateDraftBuilder.of("state-key", StateType.LINE_ITEM_STATE).transitions(singleton(state.toReference())).build();
final CompletableFuture<Set<State>> futureThrowingSphereException = new CompletableFuture<>();
futureThrowingSphereException.completeExceptionally(new SphereException("CTP error on fetch"));
when(mockStateService.fetchMatchingStatesByKeysWithTransitions(anySet())).thenReturn(futureThrowingSphereException);
final StateReferenceResolver stateReferenceResolver = new StateReferenceResolver(stateSyncOptions, mockStateService);
// test and assertion
assertThat(stateReferenceResolver.resolveReferences(stateDraft).toCompletableFuture()).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(SphereException.class).withMessageContaining("CTP error on fetch");
}
Aggregations