Testing polish and generic reactive changes

- Remove use of statemachine assertj assertions to prepare move
- Move statemachine assertj assertion to spring-statemachine-test
- Polish some tests and user TestUtils from core tests
- Remove most of a deprecation warning from core tests
- Relates #744
This commit is contained in:
Janne Valkealahti
2019-05-09 08:05:36 +01:00
parent 10f0ec0edf
commit 32dfb58a4b
43 changed files with 868 additions and 916 deletions

View File

@@ -0,0 +1,133 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.statemachine.test.assertj;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.util.Objects;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.StateContext.Stage;
/**
* Assertions applicable to a {@link StateContext}.
*
* @author Janne Valkealahti
*
*/
public class StateContextAssert extends AbstractAssert<StateContextAssert, StateContext<?, ?>> {
/**
* Instantiates a new state context assert.
*
* @param actual the actual state context
*/
public StateContextAssert(StateContext<?, ?> actual) {
super(actual, StateContextAssert.class);
}
/**
* Verifies that the actual context has the same {@link Stage} as given {@link Stage}.
*
* @param stage the expected stage
* @return {@code this} assertion object.
* @throws AssertionError if the stage of the actual context is not equal to the given one.
*/
public StateContextAssert hasStage(Stage stage) {
isNotNull();
if (!Objects.areEqual(actual.getStage(), stage)) {
failWithMessage("Expected context's stage to be <%s> but was <%s>", stage, actual.getStage());
}
return this;
}
/**
* Verifies that the actual context has the same {@code event} as given {@code event}.
*
* @param event the expected event
* @return {@code this} assertion object.
* @throws AssertionError if the stage of the actual context is not equal to the given one.
*/
public StateContextAssert hasEvent(Object event) {
isNotNull();
if (!Objects.areEqual(actual.getEvent(), event)) {
failWithMessage("Expected context's event to be <%s> but was <%s>", event, actual.getEvent());
}
return this;
}
/**
* Verifies that the actual context has the same {@code source id} as given {@code id}.
*
* @param id the expected source id
* @return {@code this} assertion object.
* @throws AssertionError if the source id of the actual context is not equal to the given one.
*/
public StateContextAssert hasSourceId(Object id) {
isNotNull();
if (actual.getSource() == null) {
failWithMessage("Expected context's source to be not null");
}
if (!Objects.areEqual(actual.getSource().getId(), id)) {
failWithMessage("Expected context's source id to be <%s> but was <%s>", id, actual.getSource().getId());
}
return this;
}
/**
* Verifies that the actual context does not have a source.
*
* @return {@code this} assertion object.
* @throws AssertionError if the machine has a source
*/
public StateContextAssert doesNotHaveSource() {
isNotNull();
if (actual.getSource() != null) {
failWithMessage("Expected context's source to be null but was <%s>", actual.getSource());
}
return this;
}
/**
* Verifies that the actual context has the same {@code target id} as given {@code id}.
*
* @param id the expected target id
* @return {@code this} assertion object.
* @throws AssertionError if the target id of the actual context is not equal to the given one.
*/
public StateContextAssert hasTargetId(Object id) {
isNotNull();
if (actual.getTarget() == null) {
failWithMessage("Expected context's target to be not null");
}
if (!Objects.areEqual(actual.getTarget().getId(), id)) {
failWithMessage("Expected context's target id to be <%s> but was <%s>", id, actual.getTarget().getId());
}
return this;
}
/**
* Verifies that the actual context does not have a target.
*
* @return {@code this} assertion object.
* @throws AssertionError if the machine has a target
*/
public StateContextAssert doesNotHaveTarget() {
isNotNull();
if (actual.getTarget() != null) {
failWithMessage("Expected context's target to be null but was <%s>", actual.getTarget());
}
return this;
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.statemachine.test.assertj;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.util.Objects;
import org.springframework.statemachine.StateMachine;
/**
* Assertions applicable to a {@link StateMachine}.
*
* @author Janne Valkealahti
*
*/
public class StateMachineAssert extends AbstractAssert<StateMachineAssert, StateMachine<?, ?>> {
/**
* Instantiates a new state machine assert.
*
* @param actual the actual
*/
public StateMachineAssert(StateMachine<?, ?> actual) {
super(actual, StateMachineAssert.class);
}
/**
* Verifies that the actual machine has the same {@code state id} as given {@code id}.
*
* @param id the expected state id
* @return {@code this} assertion object.
* @throws AssertionError if the target id of the actual context is not equal to the given one.
*/
public StateMachineAssert hasStateId(Object id) {
isNotNull();
if (actual.getState() == null) {
failWithMessage("Expected machine's state to be not null");
}
if (!Objects.areEqual(actual.getState().getId(), id)) {
failWithMessage("Expected machine's state id to be <%s> but was <%s>", id, actual.getState().getId());
}
return this;
}
/**
* Verifies that the actual machine does not have a state.
*
* @return {@code this} assertion object.
* @throws AssertionError if the machine has a state
*/
public StateMachineAssert doesNotHaveState() {
isNotNull();
if (actual.getState() != null) {
failWithMessage("Expected machine's state to be null but was <%s>", actual.getState());
}
return this;
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.statemachine.test.assertj;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.StateMachineEventResult;
/**
* Entry point for all {@code assertj} definitions for a {@code StateMachine}.
* <p>
* NOTE: we build assertj features here in core tests before moving them into spring-statemachine-test.
*
* @author Janne Valkealahti
*
*/
public class StateMachineAsserts {
/**
* Creates a new instance of {@link StateContextAssert} allowing to perform assertions on it.
*
* @param stateContext the state context
* @return the created assertion object.
*/
public static StateContextAssert assertThat(StateContext<?, ?> stateContext) {
return new StateContextAssert(stateContext);
}
/**
* Creates a new instance of {@link StateMachineAssert} allowing to perform assertions on it.
*
* @param stateMachine the state machine
* @return the created assertion object.
*/
public static StateMachineAssert assertThat(StateMachine<?, ?> stateMachine) {
return new StateMachineAssert(stateMachine);
}
/**
* Creates a new instance of {@link StateMachineEventResultAssert} allowing to perform assertions on it.
*
* @param stateMachineEventResult the state machine event result
* @return the created assertion object.
*/
public static StateMachineEventResultAssert assertThat(StateMachineEventResult<?, ?> stateMachineEventResult) {
return new StateMachineEventResultAssert(stateMachineEventResult);
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.statemachine.test.assertj;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.util.Objects;
import org.springframework.statemachine.StateMachineEventResult;
import org.springframework.statemachine.StateMachineEventResult.ResultType;
/**
* Assertions applicable to a {@link StateMachineEventResult}.
*
* @author Janne Valkealahti
*
*/
public class StateMachineEventResultAssert
extends AbstractAssert<StateMachineEventResultAssert, StateMachineEventResult<?, ?>> {
/**
* Instantiates a new state machine event result assert.
*
* @param actual the actual state machine event result
*/
public StateMachineEventResultAssert(StateMachineEventResult<?, ?> actual) {
super(actual, StateMachineEventResultAssert.class);
}
/**
* Verifies that the actual event result has the same {@link ResultType} as
* given {@link ResultType}.
*
* @param resultType the expected result type
* @return {@code this} assertion object.
* @throws AssertionError if the result type of the actual event result is not equal to the given one.
*/
public StateMachineEventResultAssert hasResultType(ResultType resultType) {
isNotNull();
if (!Objects.areEqual(actual.getResultType(), resultType)) {
failWithMessage("Expected result's type to be <%s> but was <%s>", resultType, actual.getResultType());
}
return this;
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.statemachine.test.assertj;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.statemachine.StateMachineEventResult;
import org.springframework.statemachine.StateMachineEventResult.ResultType;
public class StateMachineEventResultAssertTests {
@Test
public void test() {
StateMachineEventResult<?, ?> mock = mock(StateMachineEventResult.class);
Mockito.when(mock.getResultType()).thenReturn(ResultType.ACCEPTED);
StateMachineEventResultAssert assertions = new StateMachineEventResultAssert(mock);
assertThat(assertions.hasResultType(ResultType.ACCEPTED));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(assertions.hasResultType(ResultType.DENIED)))
.withMessageContaining("Expected result's type to be <DENIED> but was <ACCEPTED>");
}
}