use of com.commercetools.sync.services.StateService in project commercetools-sync-java by commercetools.
the class ProductSyncMockUtils method getMockStateService.
/**
* Creates a mock {@link StateService} that returns a completed {@link CompletableFuture}
* containing an {@link Optional} containing the id of the supplied value whenever the following
* method is called on the service:
*
* <ul>
* <li>{@link StateService#fetchCachedStateId(String)}
* </ul>
*
* @return the created mock of the {@link StateService}.
*/
public static StateService getMockStateService(@Nonnull final String id) {
final StateService stateService = mock(StateService.class);
when(stateService.fetchCachedStateId(anyString())).thenReturn(CompletableFuture.completedFuture(Optional.of(id)));
return stateService;
}
use of com.commercetools.sync.services.StateService in project commercetools-sync-java by commercetools.
the class StateSyncTest method sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback.
@Test
void sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback() {
// preparation
final StateDraft stateDraft = StateDraftBuilder.of("state-1", StateType.LINE_ITEM_STATE).name(LocalizedString.ofEnglish("foo")).transitions(null).build();
final State mockedExistingState = mock(State.class);
when(mockedExistingState.getKey()).thenReturn(stateDraft.getKey());
when(mockedExistingState.getName()).thenReturn(LocalizedString.ofEnglish("bar"));
when(mockedExistingState.getTransitions()).thenReturn(null);
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(mock(SphereClient.class)).build();
final StateService stateService = mock(StateService.class);
final Map<String, String> keyToIds = new HashMap<>();
keyToIds.put(stateDraft.getKey(), UUID.randomUUID().toString());
when(stateService.cacheKeysToIds(anySet())).thenReturn(completedFuture(keyToIds));
when(stateService.fetchMatchingStatesByKeysWithTransitions(anySet())).thenReturn(completedFuture(singleton(mockedExistingState)));
when(stateService.updateState(any(), any())).thenReturn(completedFuture(mockedExistingState));
final StateSyncOptions spyStateSyncOptions = spy(stateSyncOptions);
final StateSync stateSync = new StateSync(spyStateSyncOptions, stateService);
// test
stateSync.sync(singletonList(stateDraft)).toCompletableFuture().join();
// assertion
verify(spyStateSyncOptions).applyBeforeUpdateCallback(any(), any(), any());
verify(spyStateSyncOptions, never()).applyBeforeCreateCallback(any());
}
use of com.commercetools.sync.services.StateService in project commercetools-sync-java by commercetools.
the class StateSyncTest method sync_WithInvalidDrafts_ShouldCompleteWithoutAnyProcessing.
@Test
void sync_WithInvalidDrafts_ShouldCompleteWithoutAnyProcessing() {
// preparation
final SphereClient ctpClient = mock(SphereClient.class);
final List<String> errors = new ArrayList<>();
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(ctpClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errors.add(exception.getMessage());
}).build();
final StateService stateService = mock(StateService.class);
final StateSync stateSync = new StateSync(stateSyncOptions, stateService);
final StateDraft stateDraftWithoutKey = StateDraftBuilder.of(null, StateType.LINE_ITEM_STATE).name(LocalizedString.ofEnglish("state-name")).build();
// test
final StateSyncStatistics statistics = stateSync.sync(asList(stateDraftWithoutKey, null)).toCompletableFuture().join();
// assertion
verifyNoMoreInteractions(ctpClient);
verifyNoMoreInteractions(stateService);
assertThat(errors).hasSize(2);
assertThat(errors).containsExactly("StateDraft with name: LocalizedString(en -> state-name) doesn't have a key. " + "Please make sure all state drafts have keys.", "StateDraft is null.");
assertThat(statistics).hasValues(2, 0, 0, 2, 0);
}
use of com.commercetools.sync.services.StateService in project commercetools-sync-java by commercetools.
the class StateSyncTest method sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback.
@Test
void sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback() {
// preparation
final StateDraft stateDraft = StateDraftBuilder.of("state-1", StateType.LINE_ITEM_STATE).build();
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(mock(SphereClient.class)).build();
final StateService stateService = mock(StateService.class);
when(stateService.cacheKeysToIds(anySet())).thenReturn(completedFuture(emptyMap()));
when(stateService.fetchMatchingStatesByKeysWithTransitions(anySet())).thenReturn(completedFuture(emptySet()));
when(stateService.createState(any())).thenReturn(completedFuture(Optional.empty()));
final StateSyncOptions spyStateSyncOptions = spy(stateSyncOptions);
final StateSync stateSync = new StateSync(spyStateSyncOptions, stateService);
// test
stateSync.sync(singletonList(stateDraft)).toCompletableFuture().join();
// assertion
verify(spyStateSyncOptions).applyBeforeCreateCallback(any());
verify(spyStateSyncOptions, never()).applyBeforeUpdateCallback(any(), any(), any());
}
use of com.commercetools.sync.services.StateService in project commercetools-sync-java by commercetools.
the class StateReferenceResolverTest method resolveReferences_WithNullStateReferences_ShouldNotResolveReferences.
@Test
void resolveReferences_WithNullStateReferences_ShouldNotResolveReferences() {
// preparation
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(mock(SphereClient.class)).build();
final StateService mockStateService = mock(StateService.class);
when(mockStateService.fetchMatchingStatesByKeysWithTransitions(any())).thenReturn(CompletableFuture.completedFuture(new HashSet<>()));
final StateDraft stateDraft = StateDraftBuilder.of("state-key", StateType.LINE_ITEM_STATE).build();
final StateReferenceResolver stateReferenceResolver = new StateReferenceResolver(stateSyncOptions, mockStateService);
// test and assertion
assertThat(stateReferenceResolver.resolveReferences(stateDraft).toCompletableFuture()).isCompletedWithValueMatching(resolvedDraft -> resolvedDraft.getTransitions() == null);
}
Aggregations