use of com.commercetools.sync.states.StateSyncOptions 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.states.StateSyncOptions 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");
}
use of com.commercetools.sync.states.StateSyncOptions in project commercetools-sync-java by commercetools.
the class StateReferenceResolverTest method resolveReferences_WithNullIdOnStateReference_ShouldNotResolveReference.
@Test
void resolveReferences_WithNullIdOnStateReference_ShouldNotResolveReference() {
// preparation
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(mock(SphereClient.class)).build();
final StateService mockStateService = mock(StateService.class);
when(mockStateService.fetchMatchingStatesByKeysWithTransitions(any())).thenReturn(CompletableFuture.completedFuture(new HashSet<>()));
final StateDraft stateDraft = StateDraftBuilder.of("state-key", StateType.LINE_ITEM_STATE).transitions(singleton(State.referenceOfId(null))).build();
final StateReferenceResolver stateReferenceResolver = new StateReferenceResolver(stateSyncOptions, mockStateService);
// test and assertion
assertThat(stateReferenceResolver.resolveReferences(stateDraft).toCompletableFuture()).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(ReferenceResolutionException.class).withMessageContaining(format("Failed to resolve 'transition' reference on StateDraft with " + "key:'%s'. Reason: %s", stateDraft.getKey(), BLANK_ID_VALUE_ON_REFERENCE));
}
use of com.commercetools.sync.states.StateSyncOptions in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithExceptionOnResolvingTransition_ShouldUpdateTransitions.
@Test
void sync_WithExceptionOnResolvingTransition_ShouldUpdateTransitions() {
final StateDraft stateCDraft = createStateDraft(keyC);
final State stateC = createStateInSource(stateCDraft);
final StateDraft stateBDraft = createStateDraft(keyB, stateC);
final State stateB = createStateInSource(stateBDraft);
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
when(spyClient.execute(any())).thenCallRealMethod().thenReturn(exceptionallyCompletedFuture(new BadGatewayException()));
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(spyClient).batchSize(3).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).warningCallback((exception, newResource, oldResource) -> warningCallBackMessages.add(exception.getMessage())).build();
final StateSync stateSync = new StateSync(stateSyncOptions);
final List<StateDraft> stateDrafts = StateTransformUtils.toStateDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, Arrays.asList(stateB, stateC)).join();
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(stateDrafts).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(2, 0, 0, 2, 0);
Assertions.assertThat(errorCallBackExceptions).isNotEmpty();
Assertions.assertThat(errorCallBackMessages).isNotEmpty();
Assertions.assertThat(errorCallBackMessages.get(0)).contains("Failed to fetch existing states with keys");
Assertions.assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.states.StateSyncOptions in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_withNewStateWithNewRole_shouldAddRole.
@Test
void sync_withNewStateWithNewRole_shouldAddRole() {
final StateDraft stateDraft = StateDraftBuilder.of(keyA, StateType.REVIEW_STATE).roles(asSet(StateRole.REVIEW_INCLUDED_IN_STATISTICS)).build();
final StateDraft stateDraftTarget = StateDraftBuilder.of(keyA, StateType.REVIEW_STATE).roles(Collections.emptySet()).build();
createStateInTarget(stateDraftTarget);
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
final StateSync stateSync = new StateSync(stateSyncOptions);
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(singletonList(stateDraft)).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(1, 0, 1, 0, 0);
CtpQueryUtils.queryAll(CTP_TARGET_CLIENT, StateQueryBuilder.of().plusPredicates(q -> q.key().is(keyA)).build(), Function.identity()).thenApply(fetchedCategories -> fetchedCategories.stream().flatMap(List::stream).collect(Collectors.toList())).thenAccept(resultStates -> {
Assertions.assertThat(resultStates.size()).isEqualTo(1);
Assertions.assertThat(resultStates.get(0).getRoles().size()).isEqualTo(1);
}).toCompletableFuture().join();
}
Aggregations