use of com.commercetools.sync.states.StateSyncOptions in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_withNewStateWithoutRole_shouldRemoveRole.
@Test
void sync_withNewStateWithoutRole_shouldRemoveRole() {
final StateDraft stateDraft = StateDraftBuilder.of(keyA, StateType.REVIEW_STATE).roles(Collections.emptySet()).build();
final StateDraft stateDraftTarget = StateDraftBuilder.of(keyA, StateType.REVIEW_STATE).roles(Collections.singleton(StateRole.REVIEW_INCLUDED_IN_STATISTICS)).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().isEmpty()).isTrue();
}).toCompletableFuture().join();
}
use of com.commercetools.sync.states.StateSyncOptions in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithDeletedTransition_ShouldRemoveTransitions.
@Test
void sync_WithDeletedTransition_ShouldRemoveTransitions() {
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);
final State stateA = createStateInSource(stateADraft);
final StateDraft tagetStateADraft = createStateDraft(keyA, targetStateB, targetStateC);
final State targetStateA = createStateInTarget(tagetStateADraft);
Assertions.assertThat(targetStateB.getTransitions().size()).isEqualTo(1);
Assertions.assertThat(targetStateA.getTransitions().size()).isEqualTo(2);
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(3).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, 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).getTransitions().size()).isEqualTo(1);
}).toCompletableFuture().join();
}
use of com.commercetools.sync.states.StateSyncOptions 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();
}
use of com.commercetools.sync.states.StateSyncOptions in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_withNewStateWihCreationException_shouldPrintErrorMessage.
@Test
void sync_withNewStateWihCreationException_shouldPrintErrorMessage() {
final StateDraft stateDraft = StateDraftBuilder.of(keyA, StateType.REVIEW_STATE).roles(asSet(StateRole.REVIEW_INCLUDED_IN_STATISTICS)).build();
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final StateCreateCommand command = any(StateCreateCommand.class);
when(spyClient.execute(command)).thenReturn(completedFuture(any(State.class)));
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 process the StateDraft with key: '%s'", keyA));
Assertions.assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.states.StateSyncOptions in project commercetools-sync-java by commercetools.
the class StateServiceImplTest method setup.
@BeforeEach
void setup() {
stateId = RandomStringUtils.random(15);
stateKey = RandomStringUtils.random(15);
errorMessages = new ArrayList<>();
errorExceptions = new ArrayList<>();
StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(client).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
errorExceptions.add(exception.getCause());
}).build();
service = new StateServiceImpl(stateSyncOptions);
}
Aggregations