Spring State Machine (#1493)

* Neo4j cleanup

* Neo4j cleanup

* Neo4j cleanup x2

* State Machine Init

* cleanup

* White background, Java Util Logging

* Change to Logging

* Static import of asserts.
rename test methods

* fix attempt for S3 -> S4

* Remove awaitility.  add teardown

* fix attempt for S3 -> S4

* fix attempt for S3 -> S4 Final
This commit is contained in:
Danil Kornishev
2017-03-27 15:21:03 -04:00
committed by Zeger Hendrikse
parent 365d75a8d7
commit 1c1c557a39
20 changed files with 814 additions and 0 deletions
@@ -0,0 +1,45 @@
package com.baeldung.spring.stateMachine;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine;
import com.baeldung.spring.stateMachine.config.ForkJoinStateMachineConfiguration;
public class ForkJoinStateMachineTest {
@Test
public void whenForkStateEntered_thenMultipleSubStatesEntered() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ForkJoinStateMachineConfiguration.class);
StateMachine stateMachine = ctx.getBean(StateMachine.class);
stateMachine.start();
boolean success = stateMachine.sendEvent("E1");
assertTrue(success);
assertTrue(Arrays.asList("SFork", "Sub1-1", "Sub2-1").containsAll(stateMachine.getState().getIds()));
}
@Test
public void whenAllConfiguredJoinEntryStatesAreEntered_thenTransitionToJoinState() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ForkJoinStateMachineConfiguration.class);
StateMachine stateMachine = ctx.getBean(StateMachine.class);
stateMachine.start();
boolean success = stateMachine.sendEvent("E1");
assertTrue(success);
assertTrue(Arrays.asList("SFork", "Sub1-1", "Sub2-1").containsAll(stateMachine.getState().getIds()));
assertTrue(stateMachine.sendEvent("sub1"));
assertTrue(stateMachine.sendEvent("sub2"));
assertEquals("SJoin", stateMachine.getState().getId());
}
}
@@ -0,0 +1,37 @@
package com.baeldung.spring.stateMachine;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine;
import com.baeldung.spring.stateMachine.config.HierarchicalStateMachineConfiguration;
public class HierarchicalStateMachineTest {
@Test
public void whenTransitionToSubMachine_thenSubStateIsEntered() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(HierarchicalStateMachineConfiguration.class);
StateMachine stateMachine = ctx.getBean(StateMachine.class);
stateMachine.start();
assertEquals(Arrays.asList("SI", "SUB1"), stateMachine.getState().getIds());
stateMachine.sendEvent("se1");
assertEquals(Arrays.asList("SI", "SUB2"), stateMachine.getState().getIds());
stateMachine.sendEvent("s-end");
assertEquals(Arrays.asList("SI", "SUBEND"), stateMachine.getState().getIds());
stateMachine.sendEvent("end");
assertEquals(1, stateMachine.getState().getIds().size());
assertEquals("SF", stateMachine.getState().getId());
}
}
@@ -0,0 +1,24 @@
package com.baeldung.spring.stateMachine;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine;
import com.baeldung.spring.stateMachine.config.JunctionStateMachineConfiguration;
public class JunctionStateMachineTest {
@Test
public void whenTransitioningToJunction_thenArriveAtSubJunctionNode() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(JunctionStateMachineConfiguration.class);
StateMachine stateMachine = ctx.getBean(StateMachine.class);
stateMachine.start();
stateMachine.sendEvent("E1");
Assert.assertEquals("low", stateMachine.getState().getId());
stateMachine.sendEvent("end");
Assert.assertEquals("SF", stateMachine.getState().getId());
}
}
@@ -0,0 +1,33 @@
package com.baeldung.spring.stateMachine;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine;
import com.baeldung.spring.stateMachine.applicationReview.ApplicationReviewEvents;
import com.baeldung.spring.stateMachine.applicationReview.ApplicationReviewStates;
import com.baeldung.spring.stateMachine.config.SimpleEnumStateMachineConfiguration;
public class StateEnumMachineTest {
private StateMachine stateMachine;
@Before
public void setUp() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SimpleEnumStateMachineConfiguration.class);
stateMachine = ctx.getBean(StateMachine.class);
stateMachine.start();
}
@Test
public void whenStateMachineConfiguredWithEnums_thenStateMachineAcceptsEnumEvents() {
assertTrue(stateMachine.sendEvent(ApplicationReviewEvents.APPROVE));
assertEquals(ApplicationReviewStates.PRINCIPAL_REVIEW, stateMachine.getState().getId());
assertTrue(stateMachine.sendEvent(ApplicationReviewEvents.REJECT));
assertEquals(ApplicationReviewStates.REJECTED, stateMachine.getState().getId());
}
}
@@ -0,0 +1,35 @@
package com.baeldung.spring.stateMachine;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineBuilder;
public class StateMachineBuilderTest {
@Test
public void whenUseStateMachineBuilder_thenBuildSuccessAndMachineWorks() throws Exception {
StateMachineBuilder.Builder<String, String> builder = StateMachineBuilder.builder();
builder.configureStates().withStates()
.initial("SI")
.state("S1")
.end("SF");
builder.configureTransitions()
.withExternal()
.source("SI").target("S1").event("E1")
.and().withExternal()
.source("S1").target("SF").event("E2");
StateMachine machine = builder.build();
machine.start();
machine.sendEvent("E1");
assertEquals("S1", machine.getState().getId());
machine.sendEvent("E2");
assertEquals("SF", machine.getState().getId());
}
}
@@ -0,0 +1,51 @@
package com.baeldung.spring.stateMachine;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine;
import com.baeldung.spring.stateMachine.config.SimpleStateMachineConfiguration;
public class StateMachineTest {
private AnnotationConfigApplicationContext ctx;
private StateMachine stateMachine;
@Before
public void setUp() {
ctx = new AnnotationConfigApplicationContext(SimpleStateMachineConfiguration.class);
stateMachine = ctx.getBean(StateMachine.class);
stateMachine.start();
}
@Test
public void whenSimpleStringStateMachineEvents_thenEndState() {
assertEquals("SI", stateMachine.getState().getId());
stateMachine.sendEvent("E1");
assertEquals("S1", stateMachine.getState().getId());
stateMachine.sendEvent("E2");
assertEquals("S2", stateMachine.getState().getId());
}
@Test
public void whenSimpleStringMachineActionState_thenActionExecuted() {
stateMachine.sendEvent("E3");
assertEquals("S3", stateMachine.getState().getId());
boolean acceptedE4 = stateMachine.sendEvent("E4");
assertTrue(acceptedE4);
assertEquals("S4", stateMachine.getState().getId());
assertEquals(2, stateMachine.getExtendedState().getVariables().get("approvalCount"));
stateMachine.sendEvent("end");
assertEquals("SF", stateMachine.getState().getId());
}
}