Search in sources :

Example 6 with StateServiceImpl

use of com.commercetools.sync.services.impl.StateServiceImpl in project commercetools-sync-java by commercetools.

the class StateServiceImplIT method fetchMatchingStatesByKeys_WithBadGateWayExceptionAlways_ShouldFail.

@Test
void fetchMatchingStatesByKeys_WithBadGateWayExceptionAlways_ShouldFail() {
    // Mock sphere client to return BadGatewayException on any request.
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    when(spyClient.execute(any(StateQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
    final StateSyncOptions spyOptions = StateSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    final StateService spyStateService = new StateServiceImpl(spyOptions);
    final Set<String> keys = new HashSet<>();
    keys.add(OLD_STATE_KEY);
    // test and assert
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(spyStateService.fetchMatchingStatesByKeys(keys)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Reference(io.sphere.sdk.models.Reference) StateITUtils.createState(com.commercetools.sync.integration.commons.utils.StateITUtils.createState) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SetName(io.sphere.sdk.states.commands.updateactions.SetName) StringUtils(org.apache.commons.lang3.StringUtils) Collections.singletonList(java.util.Collections.singletonList) AfterAll(org.junit.jupiter.api.AfterAll) Collections.singleton(java.util.Collections.singleton) SphereClient(io.sphere.sdk.client.SphereClient) StateType(io.sphere.sdk.states.StateType) BadGatewayException(io.sphere.sdk.client.BadGatewayException) STATE_KEY_1(com.commercetools.sync.integration.commons.utils.StateITUtils.STATE_KEY_1) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) Set(java.util.Set) StateITUtils.clearTransitions(com.commercetools.sync.integration.commons.utils.StateITUtils.clearTransitions) StateService(com.commercetools.sync.services.StateService) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) State(io.sphere.sdk.states.State) Test(org.junit.jupiter.api.Test) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) Optional(java.util.Optional) StateDraft(io.sphere.sdk.states.StateDraft) STATE_NAME_1(com.commercetools.sync.integration.commons.utils.StateITUtils.STATE_NAME_1) StateITUtils.deleteStates(com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStates) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) StateQuery(io.sphere.sdk.states.queries.StateQuery) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Assertions.as(org.assertj.core.api.Assertions.as) StateDraftBuilder(io.sphere.sdk.states.StateDraftBuilder) StateSyncOptionsBuilder(com.commercetools.sync.states.StateSyncOptionsBuilder) ChangeKey(io.sphere.sdk.states.commands.updateactions.ChangeKey) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) ChangeType(io.sphere.sdk.states.commands.updateactions.ChangeType) STATE_DESCRIPTION_1(com.commercetools.sync.integration.commons.utils.StateITUtils.STATE_DESCRIPTION_1) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Collections(java.util.Collections) StateService(com.commercetools.sync.services.StateService) SphereClient(io.sphere.sdk.client.SphereClient) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ExecutionException(java.util.concurrent.ExecutionException) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 7 with StateServiceImpl

use of com.commercetools.sync.services.impl.StateServiceImpl in project commercetools-sync-java by commercetools.

the class StateServiceImplIT method createState_WithValidState_ShouldCreateStateAndCacheId.

@Test
void createState_WithValidState_ShouldCreateStateAndCacheId() {
    final StateDraft newStateDraft = StateDraftBuilder.of(STATE_KEY_1, STATE_TYPE).name(STATE_NAME_1).description(STATE_DESCRIPTION_1).build();
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    final StateSyncOptions spyOptions = StateSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    final StateService spyStateService = new StateServiceImpl(spyOptions);
    // test
    final Optional<State> createdState = spyStateService.createState(newStateDraft).toCompletableFuture().join();
    final Optional<State> queriedOptional = CTP_TARGET_CLIENT.execute(StateQuery.of().withPredicates(stateQueryModel -> stateQueryModel.key().is(STATE_KEY_1))).toCompletableFuture().join().head();
    assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdState).hasValueSatisfying(created -> {
        assertThat(created.getKey()).isEqualTo(queried.getKey());
        assertThat(created.getDescription()).isEqualTo(queried.getDescription());
        assertThat(created.getName()).isEqualTo(queried.getName());
    }));
    // Assert that the created state is cached
    final Optional<String> stateId = spyStateService.fetchCachedStateId(STATE_KEY_1).toCompletableFuture().join();
    assertThat(stateId).isPresent();
    verify(spyClient, times(0)).execute(any(StateQuery.class));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Reference(io.sphere.sdk.models.Reference) StateITUtils.createState(com.commercetools.sync.integration.commons.utils.StateITUtils.createState) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SetName(io.sphere.sdk.states.commands.updateactions.SetName) StringUtils(org.apache.commons.lang3.StringUtils) Collections.singletonList(java.util.Collections.singletonList) AfterAll(org.junit.jupiter.api.AfterAll) Collections.singleton(java.util.Collections.singleton) SphereClient(io.sphere.sdk.client.SphereClient) StateType(io.sphere.sdk.states.StateType) BadGatewayException(io.sphere.sdk.client.BadGatewayException) STATE_KEY_1(com.commercetools.sync.integration.commons.utils.StateITUtils.STATE_KEY_1) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) Set(java.util.Set) StateITUtils.clearTransitions(com.commercetools.sync.integration.commons.utils.StateITUtils.clearTransitions) StateService(com.commercetools.sync.services.StateService) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) State(io.sphere.sdk.states.State) Test(org.junit.jupiter.api.Test) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) Optional(java.util.Optional) StateDraft(io.sphere.sdk.states.StateDraft) STATE_NAME_1(com.commercetools.sync.integration.commons.utils.StateITUtils.STATE_NAME_1) StateITUtils.deleteStates(com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStates) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) StateQuery(io.sphere.sdk.states.queries.StateQuery) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Assertions.as(org.assertj.core.api.Assertions.as) StateDraftBuilder(io.sphere.sdk.states.StateDraftBuilder) StateSyncOptionsBuilder(com.commercetools.sync.states.StateSyncOptionsBuilder) ChangeKey(io.sphere.sdk.states.commands.updateactions.ChangeKey) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) ChangeType(io.sphere.sdk.states.commands.updateactions.ChangeType) STATE_DESCRIPTION_1(com.commercetools.sync.integration.commons.utils.StateITUtils.STATE_DESCRIPTION_1) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Collections(java.util.Collections) StateDraft(io.sphere.sdk.states.StateDraft) StateService(com.commercetools.sync.services.StateService) StateITUtils.createState(com.commercetools.sync.integration.commons.utils.StateITUtils.createState) State(io.sphere.sdk.states.State) SphereClient(io.sphere.sdk.client.SphereClient) StateQuery(io.sphere.sdk.states.queries.StateQuery) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) Test(org.junit.jupiter.api.Test)

Example 8 with StateServiceImpl

use of com.commercetools.sync.services.impl.StateServiceImpl in project commercetools-sync-java by commercetools.

the class StateSyncIT method sync_WithStateWithEmptyTransitionShouldBeResolved_ShouldAddErrorMessage.

@Test
void sync_WithStateWithEmptyTransitionShouldBeResolved_ShouldAddErrorMessage() {
    final StateDraft stateCDraft = StateDraftBuilder.of(keyC, StateType.REVIEW_STATE).roles(Collections.singleton(StateRole.REVIEW_INCLUDED_IN_STATISTICS)).transitions(new HashSet<>(Arrays.asList(Reference.of(State.referenceTypeId(), "")))).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();
    // test
    StateServiceImpl stateService = new StateServiceImpl(stateSyncOptions);
    final StateReferenceResolver stateReferenceResolver = new StateReferenceResolver(stateSyncOptions, stateService);
    CompletionStage<StateDraft> result = stateReferenceResolver.resolveReferences(stateCDraft);
    result.exceptionally(exception -> {
        Assertions.assertThat(exception.getMessage()).contains(format("Failed to resolve 'transition' reference on StateDraft with key:'%s", keyC));
        return null;
    }).toCompletableFuture().join();
}
Also used : CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) Arrays(java.util.Arrays) Reference(io.sphere.sdk.models.Reference) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Collections.singletonList(java.util.Collections.singletonList) AfterAll(org.junit.jupiter.api.AfterAll) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) StateTransformUtils(com.commercetools.sync.states.utils.StateTransformUtils) StateCreateCommand(io.sphere.sdk.states.commands.StateCreateCommand) Arrays.asList(java.util.Arrays.asList) StateITUtils.getStateByKey(com.commercetools.sync.integration.commons.utils.StateITUtils.getStateByKey) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) StateType(io.sphere.sdk.states.StateType) Assertions(org.assertj.core.api.Assertions) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ThreadLocalRandom.current(java.util.concurrent.ThreadLocalRandom.current) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) StateUpdateCommand(io.sphere.sdk.states.commands.StateUpdateCommand) Set(java.util.Set) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) State(io.sphere.sdk.states.State) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) StateITUtils.deleteStatesFromTargetAndSource(com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStatesFromTargetAndSource) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) List(java.util.List) CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) CompletionStage(java.util.concurrent.CompletionStage) SphereInternalUtils.asSet(io.sphere.sdk.utils.SphereInternalUtils.asSet) Optional(java.util.Optional) StateDraft(io.sphere.sdk.states.StateDraft) StateITUtils.deleteStates(com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStates) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) StateQuery(io.sphere.sdk.states.queries.StateQuery) BadRequestException(io.sphere.sdk.client.BadRequestException) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) Mockito.spy(org.mockito.Mockito.spy) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) State.referenceOfId(io.sphere.sdk.states.State.referenceOfId) StateReferenceResolver(com.commercetools.sync.states.helpers.StateReferenceResolver) UnresolvedReferencesService(com.commercetools.sync.services.UnresolvedReferencesService) StateRole(io.sphere.sdk.states.StateRole) Nonnull(javax.annotation.Nonnull) StateDraftBuilder(io.sphere.sdk.states.StateDraftBuilder) StateQueryBuilder(io.sphere.sdk.states.queries.StateQueryBuilder) StateSyncOptionsBuilder(com.commercetools.sync.states.StateSyncOptionsBuilder) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) StateSync(com.commercetools.sync.states.StateSync) Mockito.when(org.mockito.Mockito.when) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) CtpQueryUtils(com.commercetools.sync.commons.utils.CtpQueryUtils) WaitingToBeResolvedTransitions(com.commercetools.sync.commons.models.WaitingToBeResolvedTransitions) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) Collections(java.util.Collections) StateDraft(io.sphere.sdk.states.StateDraft) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) StateReferenceResolver(com.commercetools.sync.states.helpers.StateReferenceResolver) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) Test(org.junit.jupiter.api.Test)

Aggregations

StateServiceImpl (com.commercetools.sync.services.impl.StateServiceImpl)8 SphereClient (io.sphere.sdk.client.SphereClient)8 State (io.sphere.sdk.states.State)8 StateDraft (io.sphere.sdk.states.StateDraft)8 StateDraftBuilder (io.sphere.sdk.states.StateDraftBuilder)8 StateType (io.sphere.sdk.states.StateType)8 StateQuery (io.sphere.sdk.states.queries.StateQuery)8 ArrayList (java.util.ArrayList)8 Collections.singletonList (java.util.Collections.singletonList)8 List (java.util.List)8 Optional (java.util.Optional)8 Test (org.junit.jupiter.api.Test)8 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)8 Mockito.spy (org.mockito.Mockito.spy)8 Mockito.when (org.mockito.Mockito.when)8 StateService (com.commercetools.sync.services.StateService)7 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)6 StateITUtils.deleteStates (com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStates)5 StateSyncOptions (com.commercetools.sync.states.StateSyncOptions)5 StateSyncOptionsBuilder (com.commercetools.sync.states.StateSyncOptionsBuilder)5