Spring State Machine (#1424)
* 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
This commit is contained in:
committed by
Zeger Hendrikse
parent
f673acbb47
commit
319dd2653a
+45
@@ -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());
|
||||
}
|
||||
}
|
||||
+37
@@ -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());
|
||||
}
|
||||
}
|
||||
+24
@@ -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());
|
||||
}
|
||||
}
|
||||
+33
@@ -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());
|
||||
}
|
||||
}
|
||||
+35
@@ -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());
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.spring.stateMachine;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
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 StateMachine stateMachine;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
AnnotationConfigApplicationContext 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());
|
||||
|
||||
stateMachine.sendEvent("end");
|
||||
assertEquals("SF", stateMachine.getState().getId());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSimpleStringMachineActionState_thenActionExecuted() {
|
||||
stateMachine.sendEvent("E3");
|
||||
assertEquals("S3", stateMachine.getState().getId());
|
||||
|
||||
stateMachine.sendEvent("E4");
|
||||
assertEquals("S4", stateMachine.getState().getId());
|
||||
assertEquals(2, stateMachine.getExtendedState().getVariables().get("approvalCount"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user