Search in sources :

Example 11 with StateService

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;
}
Also used : StateService(com.commercetools.sync.services.StateService)

Example 12 with 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());
}
Also used : StateDraft(io.sphere.sdk.states.StateDraft) HashMap(java.util.HashMap) State(io.sphere.sdk.states.State) StateService(com.commercetools.sync.services.StateService) LocalizedString(io.sphere.sdk.models.LocalizedString) Test(org.junit.jupiter.api.Test)

Example 13 with StateService

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);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) StateQuery(io.sphere.sdk.states.queries.StateQuery) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Collections.singleton(java.util.Collections.singleton) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Arrays.asList(java.util.Arrays.asList) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) Map(java.util.Map) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) SphereException(io.sphere.sdk.models.SphereException) StateType(io.sphere.sdk.states.StateType) StateDraftBuilder(io.sphere.sdk.states.StateDraftBuilder) Collections.emptyMap(java.util.Collections.emptyMap) Collections.emptySet(java.util.Collections.emptySet) CompletionException(java.util.concurrent.CompletionException) Mockito.when(org.mockito.Mockito.when) StateService(com.commercetools.sync.services.StateService) UUID(java.util.UUID) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) State(io.sphere.sdk.states.State) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) Mockito.never(org.mockito.Mockito.never) List(java.util.List) Optional(java.util.Optional) StateDraft(io.sphere.sdk.states.StateDraft) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Mockito.mock(org.mockito.Mockito.mock) StateDraft(io.sphere.sdk.states.StateDraft) StateService(com.commercetools.sync.services.StateService) StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) SphereClient(io.sphere.sdk.client.SphereClient) ArrayList(java.util.ArrayList) LocalizedString(io.sphere.sdk.models.LocalizedString) Test(org.junit.jupiter.api.Test)

Example 14 with StateService

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());
}
Also used : StateDraft(io.sphere.sdk.states.StateDraft) StateService(com.commercetools.sync.services.StateService) Test(org.junit.jupiter.api.Test)

Example 15 with StateService

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);
}
Also used : StateDraft(io.sphere.sdk.states.StateDraft) StateService(com.commercetools.sync.services.StateService) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

StateService (com.commercetools.sync.services.StateService)17 StateDraft (io.sphere.sdk.states.StateDraft)16 Test (org.junit.jupiter.api.Test)16 StateSyncOptions (com.commercetools.sync.states.StateSyncOptions)11 State (io.sphere.sdk.states.State)11 HashSet (java.util.HashSet)11 SphereClient (io.sphere.sdk.client.SphereClient)9 StateDraftBuilder (io.sphere.sdk.states.StateDraftBuilder)9 StateType (io.sphere.sdk.states.StateType)9 Collections.singleton (java.util.Collections.singleton)9 List (java.util.List)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)9 Mockito.when (org.mockito.Mockito.when)9 StateServiceImpl (com.commercetools.sync.services.impl.StateServiceImpl)8 StateQuery (io.sphere.sdk.states.queries.StateQuery)8 ArrayList (java.util.ArrayList)8 Collections.singletonList (java.util.Collections.singletonList)8 Optional (java.util.Optional)8 Assertions.as (org.assertj.core.api.Assertions.as)8