use of com.commercetools.sync.states.StateSync in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithUpdatedTransitionAndClientThrowsError_ShouldAddErrorMessage.
@Test
void sync_WithUpdatedTransitionAndClientThrowsError_ShouldAddErrorMessage() {
final StateDraft stateCDraft = createStateDraft(keyC);
final State stateC = createStateInSource(stateCDraft);
final StateDraft tagetStateCDraft = createStateDraft(keyC);
final State targetStateC = createStateInTarget(tagetStateCDraft);
final StateDraft stateBDraft = createStateDraft(keyB, stateC);
final State stateB = createStateInSource(stateBDraft);
final StateDraft tagetStateBDraft = createStateDraft(keyB, targetStateC);
final State targetStateB = createStateInTarget(tagetStateBDraft);
final StateDraft stateADraft = createStateDraft(keyA, stateB, stateC);
final State stateA = createStateInSource(stateADraft);
final StateDraft tagetStateADraft = createStateDraft(keyA, targetStateB);
final State targetStateA = createStateInTarget(tagetStateADraft);
Assertions.assertThat(targetStateB.getTransitions().size()).isEqualTo(1);
Assertions.assertThat(targetStateA.getTransitions().size()).isEqualTo(1);
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final StateUpdateCommand updateCommand = any(StateUpdateCommand.class);
when(spyClient.execute(updateCommand)).thenReturn(exceptionallyCompletedFuture(new BadRequestException("a test exception"))).thenReturn(exceptionallyCompletedFuture(new ConcurrentModificationException())).thenCallRealMethod();
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(stateA, stateB, stateC)).join();
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(stateDrafts).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(3, 0, 0, 1, 0);
Assertions.assertThat(errorCallBackExceptions).isNotEmpty();
Assertions.assertThat(errorCallBackMessages).isNotEmpty();
Assertions.assertThat(errorCallBackMessages.get(0)).contains(" detailMessage: a test exception");
Assertions.assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.states.StateSync in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_withCreateStateException_shouldPrintMessage.
@Test
void sync_withCreateStateException_shouldPrintMessage() {
final StateDraft stateDraft = StateDraftBuilder.of(keyA, StateType.REVIEW_STATE).roles(Collections.singleton(StateRole.REVIEW_INCLUDED_IN_STATISTICS)).build();
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final StateCreateCommand command = any(StateCreateCommand.class);
when(spyClient.execute(command)).thenReturn(exceptionallyCompletedFuture(new BadRequestException("test error message")));
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final StateSync stateSync = new StateSync(stateSyncOptions);
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(singletonList(stateDraft)).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(1, 0, 0, 1, 0);
Assertions.assertThat(errorCallBackExceptions).isNotEmpty();
Assertions.assertThat(errorCallBackMessages).isNotEmpty();
Assertions.assertThat(errorCallBackMessages.get(0)).contains(format("Failed to create draft with key: '%s'", keyA));
}
use of com.commercetools.sync.states.StateSync in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithNotExistentStates_ShouldResolveStateLater.
@Test
void sync_WithNotExistentStates_ShouldResolveStateLater() {
final StateDraft stateCDraft = createStateDraft(keyC);
final State stateC = createStateInSource(stateCDraft);
final StateDraft stateBDraft = createStateDraft(keyB, stateC);
final State stateB = createStateInSource(stateBDraft);
StateDraft[] draftsWithReplacesKeys = StateTransformUtils.toStateDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, asList(stateB, stateC)).join().toArray(new StateDraft[2]);
final StateDraft stateADraft = createStateDraftReferencingStateDrafts(keyA, draftsWithReplacesKeys);
final List<StateDraft> stateDrafts = asList(stateADraft);
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(1).build();
final StateSync stateSync = new StateSync(stateSyncOptions);
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(stateDrafts).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(1, 0, 0, 0, 1);
final UnresolvedReferencesService<WaitingToBeResolvedTransitions> unresolvedTransitionsService = new UnresolvedReferencesServiceImpl<>(stateSyncOptions);
Set<WaitingToBeResolvedTransitions> result = unresolvedTransitionsService.fetch(Collections.singleton(keyA), CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY, WaitingToBeResolvedTransitions.class).toCompletableFuture().join();
Assertions.assertThat(result.size()).isEqualTo(1);
WaitingToBeResolvedTransitions waitingToBeResolvedTransitions = result.iterator().next();
Assertions.assertThat(waitingToBeResolvedTransitions.getMissingTransitionStateKeys().containsAll(asList(keyB, keyC))).isTrue();
Assertions.assertThat(waitingToBeResolvedTransitions.getStateDraft().getKey()).isEqualTo(keyA);
}
use of com.commercetools.sync.states.StateSync in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithoutAnyNewStateDraft_ShouldProcessNothing.
@Test
void sync_WithoutAnyNewStateDraft_ShouldProcessNothing() {
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(3).build();
final StateSync stateSync = new StateSync(stateSyncOptions);
final StateSyncStatistics stateSyncStatistics = stateSync.sync(Collections.emptyList()).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(0, 0, 0, 0, 0);
}
use of com.commercetools.sync.states.StateSync in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithStateWithEmptyTransition_ShouldAddErrorMessage.
@Test
void sync_WithStateWithEmptyTransition_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
final StateSyncStatistics stateSyncStatistics = new StateSync(stateSyncOptions).sync(Arrays.asList(stateCDraft)).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(1, 0, 0, 1, 0);
Assertions.assertThat(errorCallBackExceptions).isNotEmpty();
Assertions.assertThat(errorCallBackMessages).isNotEmpty();
Assertions.assertThat(errorCallBackMessages.get(0)).isEqualTo(format("StateDraft with key: '%s' has invalid state transitions", keyC));
Assertions.assertThat(warningCallBackMessages).isEmpty();
}
Aggregations