use of com.commercetools.sync.states.helpers.StateSyncStatistics in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_withNewState_shouldCreateState.
@Test
void sync_withNewState_shouldCreateState() {
final StateDraft stateDraft = StateDraftBuilder.of("new-state", StateType.REVIEW_STATE).roles(Collections.singleton(StateRole.REVIEW_INCLUDED_IN_STATISTICS)).build();
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, 1, 0, 0, 0);
}
use of com.commercetools.sync.states.helpers.StateSyncStatistics in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithAllExistentStates_ShouldResolveAllStates.
@Test
void sync_WithAllExistentStates_ShouldResolveAllStates() {
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 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, 3, 0, 0, 0);
Assertions.assertThat(stateSyncStatistics.getReportMessage()).isEqualTo("Summary: 3 state(s) were processed in total " + "(3 created, 0 updated, 0 failed to sync and 0 state(s) with missing transition(s)).");
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(0);
}
use of com.commercetools.sync.states.helpers.StateSyncStatistics in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate.
@Test
void sync_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate() {
// preparation
final SphereClient spyClient = buildClientWithConcurrentModificationUpdateAndNotFoundFetchOnRetry();
List<String> errorCallBackMessages = new ArrayList<>();
List<String> warningCallBackMessages = new ArrayList<>();
List<Throwable> errorCallBackExceptions = new ArrayList<>();
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(errorCallBackMessages.get(0)).contains(format("Failed to update state with key: '%s'. Reason: Not found when attempting to fetch 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_WithFailureInKeysToIdCreation_ShouldAddErrorMessage.
@Test
void sync_WithFailureInKeysToIdCreation_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);
when(spyClient.execute(any(ResourceKeyIdGraphQlRequest.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 List<StateDraft> stateDrafts = StateTransformUtils.toStateDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, Arrays.asList(stateA, stateB, stateC)).join();
// test
final StateSyncStatistics stateSyncStatistics = new StateSync(stateSyncOptions).sync(stateDrafts).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(3, 0, 0, 3, 0);
Assertions.assertThat(errorCallBackExceptions).isNotEmpty();
Assertions.assertThat(errorCallBackMessages).isNotEmpty();
Assertions.assertThat(errorCallBackMessages.get(0)).isEqualTo("Failed to build a cache of keys to ids.");
Assertions.assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.states.helpers.StateSyncStatistics in project commercetools-sync-java by commercetools.
the class StateSyncTest method sync_WithErrorCachingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorCachingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final StateDraft stateDraft = StateDraftBuilder.of("state-1", StateType.LINE_ITEM_STATE).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final StateSyncOptions syncOptions = StateSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final StateService stateService = spy(new StateServiceImpl(syncOptions));
when(stateService.cacheKeysToIds(anySet())).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final StateSync stateSync = new StateSync(syncOptions, stateService);
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(singletonList(stateDraft)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to build a cache of keys to ids.");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(CompletionException.class).hasCauseExactlyInstanceOf(SphereException.class);
assertThat(stateSyncStatistics).hasValues(1, 0, 0, 1);
}
Aggregations