use of com.commercetools.sync.states.helpers.StateSyncStatistics in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate.
@Test
void sync_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate() {
// preparation
final SphereClient spyClient = buildClientWithConcurrentModificationUpdateAndFailedFetchOnRetry();
final StateSyncOptions spyOptions = StateSyncOptionsBuilder.of(spyClient).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(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)));
// Test and assertion
assertThat(syncStatistics).hasValues(1, 0, 0, 1, 0);
Assertions.assertThat(errorCallBackMessages).hasSize(1);
Assertions.assertThat(errorCallBackExceptions).hasSize(1);
Assertions.assertThat(errorCallBackExceptions.get(0).getCause()).isExactlyInstanceOf(BadGatewayException.class);
Assertions.assertThat(errorCallBackMessages.get(0)).contains(format("Failed to update state with key: '%s'. Reason: Failed to fetch from CTP while retrying " + "after concurrency modification.", stateDraft.getKey()));
}
use of com.commercetools.sync.states.helpers.StateSyncStatistics in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithExceptionWhenFetchingUnresolvedTransition_ShouldPrintErrorMessage.
@Test
void sync_WithExceptionWhenFetchingUnresolvedTransition_ShouldPrintErrorMessage() {
final StateDraft stateCDraft = createStateDraft(keyC);
final State stateC = createStateInSource(stateCDraft);
final StateDraft stateBDraft = createStateDraft(keyB, stateC);
final State stateB = createStateInSource(stateBDraft);
final StateDraft stateADraft = createStateDraft(keyA, stateB, stateC);
final State stateA = createStateInSource(stateADraft);
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
when(spyClient.execute(any(CustomObjectQuery.class))).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, 1, 0, 2, 1);
Assertions.assertThat(errorCallBackExceptions).isNotEmpty();
Assertions.assertThat(errorCallBackMessages).isNotEmpty();
Assertions.assertThat(errorCallBackMessages.get(0)).contains(format("Failed to fetch StateDrafts waiting to be resolved with keys"));
}
use of com.commercetools.sync.states.helpers.StateSyncStatistics 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.helpers.StateSyncStatistics 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.helpers.StateSyncStatistics 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();
}
Aggregations