use of com.commercetools.sync.states.StateSync in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithSeveralBatches_ShouldReturnProperStatistics.
@Test
void sync_WithSeveralBatches_ShouldReturnProperStatistics() {
// 2 batches
final List<StateDraft> stateDrafts = IntStream.range(0, 10).mapToObj(i -> StateDraft.of("key" + i, StateType.REVIEW_STATE).withName(ofEnglish("name" + i))).collect(Collectors.toList());
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(5).build();
final StateSync stateSync = new StateSync(stateSyncOptions);
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(stateDrafts).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(10, 10, 0, 0, 0);
}
use of com.commercetools.sync.states.StateSync in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithStateWithoutKey_ShouldAddErrorMessage.
@Test
void sync_WithStateWithoutKey_ShouldAddErrorMessage() {
String nameA = "state-A";
final StateDraft stateADraft = StateDraftBuilder.of(null, StateType.REVIEW_STATE).name(LocalizedString.ofEnglish(nameA)).roles(Collections.singleton(StateRole.REVIEW_INCLUDED_IN_STATISTICS)).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();
final StateSync stateSync = new StateSync(stateSyncOptions);
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(Arrays.asList(stateADraft)).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(1, 0, 0, 1, 0);
Assertions.assertThat(errorCallBackExceptions).isNotEmpty();
Assertions.assertThat(errorCallBackMessages).isNotEmpty();
Assertions.assertThat(errorCallBackMessages.get(0)).contains("StateDraft with name:" + " LocalizedString(en -> state-A) doesn't have a key.");
Assertions.assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.states.StateSync in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_withChangedStateButConcurrentModificationException_shouldRetryAndUpdateState.
@Test
void sync_withChangedStateButConcurrentModificationException_shouldRetryAndUpdateState() {
// preparation
final SphereClient spyClient = buildClientWithConcurrentModificationUpdate();
List<String> errorCallBackMessages = new ArrayList<>();
List<String> warningCallBackMessages = new ArrayList<>();
List<Throwable> errorCallBackExceptions = new ArrayList<>();
final StateSyncOptions spyOptions = StateSyncOptionsBuilder.of(spyClient).build();
final StateSync stateSync = new StateSync(spyOptions);
final StateDraft stateDraft = StateDraftBuilder.of(key, StateType.REVIEW_STATE).name(ofEnglish("state-name-updated")).description(ofEnglish("state-desc-updated")).roles(Collections.singleton(StateRole.REVIEW_INCLUDED_IN_STATISTICS)).initial(true).build();
final StateSyncStatistics syncStatistics = executeBlocking(stateSync.sync(singletonList(stateDraft)));
assertThat(syncStatistics).hasValues(1, 0, 1, 0, 0);
Assertions.assertThat(errorCallBackExceptions).isEmpty();
Assertions.assertThat(errorCallBackMessages).isEmpty();
Assertions.assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.states.StateSync in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithListOfNullElements_ShouldPrintErrorMessage.
@Test
void sync_WithListOfNullElements_ShouldPrintErrorMessage() {
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();
final StateSync stateSync = new StateSync(stateSyncOptions);
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(Collections.singletonList(null)).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(1, 0, 0, 1, 0);
Assertions.assertThat(errorCallBackExceptions).isNotEmpty();
Assertions.assertThat(errorCallBackMessages).isNotEmpty();
Assertions.assertThat(errorCallBackMessages.get(0)).contains("StateDraft is null.");
}
use of com.commercetools.sync.states.StateSync in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithEmptyNewTransition_ShouldRemoveTransitions.
@Test
void sync_WithEmptyNewTransition_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);
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()).isNull();
}).toCompletableFuture().join();
}
Aggregations