Search in sources :

Example 1 with STATE_NAME_1

use of com.commercetools.sync.integration.commons.utils.StateITUtils.STATE_NAME_1 in project commercetools-sync-java by commercetools.

the class StateServiceImplIT method createState_WithDuplicateKey_ShouldHaveEmptyOptionalAsAResult.

@Test
void createState_WithDuplicateKey_ShouldHaveEmptyOptionalAsAResult() {
    // preparation
    final StateDraft newStateDraft = StateDraftBuilder.of(OLD_STATE_KEY, STATE_TYPE).name(STATE_NAME_1).description(STATE_DESCRIPTION_1).build();
    final StateSyncOptions options = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    final StateService stateService = new StateServiceImpl(options);
    // test
    final Optional<State> result = stateService.createState(newStateDraft).toCompletableFuture().join();
    // assertion
    assertThat(result).isEmpty();
    assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains("A duplicate value");
    assertThat(errorCallBackExceptions).hasSize(1).singleElement().matches(exception -> {
        assertThat(exception).isExactlyInstanceOf(ErrorResponseException.class);
        final ErrorResponseException errorResponseException = (ErrorResponseException) exception;
        final List<DuplicateFieldError> fieldErrors = errorResponseException.getErrors().stream().map(sphereError -> {
            assertThat(sphereError.getCode()).isEqualTo(DuplicateFieldError.CODE);
            return sphereError.as(DuplicateFieldError.class);
        }).collect(toList());
        return fieldErrors.size() == 1;
    });
}
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) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) Test(org.junit.jupiter.api.Test)

Example 2 with STATE_NAME_1

use of com.commercetools.sync.integration.commons.utils.StateITUtils.STATE_NAME_1 in project commercetools-sync-java by commercetools.

the class StateServiceImplIT method createState_WithInvalidState_ShouldHaveEmptyOptionalAsAResult.

@Test
void createState_WithInvalidState_ShouldHaveEmptyOptionalAsAResult() {
    // preparation
    final StateDraft newStateDraft = StateDraftBuilder.of("", STATE_TYPE).name(STATE_NAME_1).description(STATE_DESCRIPTION_1).build();
    final StateSyncOptions options = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    final StateService stateService = new StateServiceImpl(options);
    // test
    final Optional<State> result = stateService.createState(newStateDraft).toCompletableFuture().join();
    // assertion
    assertThat(result).isEmpty();
    assertThat(errorCallBackMessages).containsExactly("Failed to create draft with key: ''. Reason: Draft key is blank!");
}
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) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) Test(org.junit.jupiter.api.Test)

Example 3 with STATE_NAME_1

use of com.commercetools.sync.integration.commons.utils.StateITUtils.STATE_NAME_1 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)

Aggregations

CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)3 STATE_DESCRIPTION_1 (com.commercetools.sync.integration.commons.utils.StateITUtils.STATE_DESCRIPTION_1)3 STATE_KEY_1 (com.commercetools.sync.integration.commons.utils.StateITUtils.STATE_KEY_1)3 STATE_NAME_1 (com.commercetools.sync.integration.commons.utils.StateITUtils.STATE_NAME_1)3 StateITUtils.clearTransitions (com.commercetools.sync.integration.commons.utils.StateITUtils.clearTransitions)3 StateITUtils.createState (com.commercetools.sync.integration.commons.utils.StateITUtils.createState)3 StateITUtils.deleteStates (com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStates)3 StateService (com.commercetools.sync.services.StateService)3 StateServiceImpl (com.commercetools.sync.services.impl.StateServiceImpl)3 StateSyncOptions (com.commercetools.sync.states.StateSyncOptions)3 StateSyncOptionsBuilder (com.commercetools.sync.states.StateSyncOptionsBuilder)3 CompletionStageUtil.executeBlocking (com.commercetools.tests.utils.CompletionStageUtil.executeBlocking)3 BadGatewayException (io.sphere.sdk.client.BadGatewayException)3 ErrorResponseException (io.sphere.sdk.client.ErrorResponseException)3 SphereClient (io.sphere.sdk.client.SphereClient)3 LocalizedString.ofEnglish (io.sphere.sdk.models.LocalizedString.ofEnglish)3 Reference (io.sphere.sdk.models.Reference)3 DuplicateFieldError (io.sphere.sdk.models.errors.DuplicateFieldError)3 State (io.sphere.sdk.states.State)3 StateDraft (io.sphere.sdk.states.StateDraft)3