Skip to content

Commit c5187bc

Browse files
committed
testing action entity, event, state
1 parent e6d7621 commit c5187bc

File tree

6 files changed

+165
-85
lines changed

6 files changed

+165
-85
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package statemachine.state.action;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import lombok.Setter;
8+
import lombok.ToString;
9+
10+
/**
11+
* @GitHub : https://github.com/zacscoding
12+
*/
13+
@Getter
14+
@Setter
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
@Builder
18+
@ToString
19+
public class ActionEntity {
20+
21+
private String actionId;
22+
23+
private String actionName;
24+
25+
private ActionState actionState;
26+
}
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
package statemachine.state.action;
22

3-
import lombok.Getter;
4-
53
/**
64
* @GitHub : https://github.com/zacscoding
75
*/
8-
@Getter
9-
public abstract class ActionEvent {
10-
11-
private final long actionId;
12-
private final ActionEventType actionEventType;
13-
private long timestamp;
14-
15-
public ActionEvent(long actionId, ActionEventType actionEventType) {
16-
this(actionId, actionEventType, -1L);
17-
}
6+
public enum ActionEvent {
187

19-
public ActionEvent(long actionId, ActionEventType actionEventType, long timestamp) {
20-
this.actionId = actionId;
21-
this.actionEventType = actionEventType;
22-
this.timestamp = timestamp;
23-
}
8+
ACTION_INIT, ACTION_IN_PROGRESS, ACTION_COMPLETE, ACTION_FAILED;
249
}

springboot-statemachine-demo/src/main/java/statemachine/state/action/ActionEventType.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

springboot-statemachine-demo/src/main/java/statemachine/state/action/ActionState.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* @GitHub : https://github.com/zacscoding
55
*/
66
public enum ActionState {
7-
INIT,
8-
IN_PROGRESS,
9-
COMPLETED,
10-
FAILED;
7+
8+
INIT, IN_PROGRESS, COMPLETED, FAILED;
119
}
Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
1-
package statemachine.state.action.fsm;
2-
3-
import java.util.EnumSet;
4-
import lombok.extern.slf4j.Slf4j;
5-
import org.springframework.statemachine.StateMachine;
6-
import org.springframework.statemachine.config.StateMachineBuilder;
7-
import org.springframework.statemachine.config.StateMachineBuilder.Builder;
8-
import statemachine.state.action.ActionEvent;
9-
import statemachine.state.action.ActionState;
10-
11-
/**
12-
* TODO :: will change to config
13-
*
14-
* @GitHub : https://github.com/zacscoding
15-
*/
16-
@Slf4j
17-
public class ActionStateMachine {
18-
19-
private static final ActionStateMachine INSTANCE = new ActionStateMachine();
20-
private StateMachine<ActionState, ActionEvent> machine;
21-
22-
private ActionStateMachine() {
23-
initialize();
24-
}
25-
26-
public StateMachine<ActionState, ActionEvent> getStateMachine() {
27-
return this.machine;
28-
}
29-
30-
private void initialize() {
31-
try {
32-
Builder<ActionState, ActionEvent> builder = StateMachineBuilder.builder();
33-
builder.configureStates()
34-
.withStates()
35-
.initial(ActionState.INIT)
36-
.states(EnumSet.allOf(ActionState.class));
37-
38-
builder.configureTransitions()
39-
.withExternal()
40-
.source(ActionState.INIT).target(ActionState.IN_PROGRESS).event(null)
41-
.source(null).target(null).event(null)
42-
.and()
43-
.withExternal()
44-
;
45-
46-
machine = builder.build();
47-
machine.start();
48-
} catch (Exception e) {
49-
logger.warn("Failed to initialize state machine", e);
50-
}
51-
}
52-
}
1+
//package statemachine.state.action.fsm;
2+
//
3+
//import java.util.EnumSet;
4+
//import lombok.extern.slf4j.Slf4j;
5+
//import org.springframework.statemachine.StateMachine;
6+
//import org.springframework.statemachine.config.StateMachineBuilder;
7+
//import org.springframework.statemachine.config.StateMachineBuilder.Builder;
8+
//import statemachine.state.action.ActionEvent;
9+
//import statemachine.state.action.ActionState;
10+
//
11+
///**
12+
// * TODO :: will change to config
13+
// *
14+
// * @GitHub : https://github.com/zacscoding
15+
// */
16+
//@Slf4j
17+
//public class ActionStateMachine {
18+
//
19+
// private static final ActionStateMachine INSTANCE = new ActionStateMachine();
20+
// private StateMachine<ActionState, ActionEvent> machine;
21+
//
22+
// private ActionStateMachine() {
23+
// initialize();
24+
// }
25+
//
26+
// public StateMachine<ActionState, ActionEvent> getStateMachine() {
27+
// return this.machine;
28+
// }
29+
//
30+
// private void initialize() {
31+
// try {
32+
// Builder<ActionState, ActionEvent> builder = StateMachineBuilder.builder();
33+
// builder.configureStates()
34+
// .withStates()
35+
// .initial(ActionState.INIT)
36+
// .states(EnumSet.allOf(ActionState.class));
37+
//
38+
// builder.configureTransitions()
39+
// .withExternal()
40+
// .source(ActionState.INIT).target(ActionState.IN_PROGRESS).event(null)
41+
// .source(null).target(null).event(null)
42+
// .and()
43+
// .withExternal()
44+
// ;
45+
//
46+
// machine = builder.build();
47+
// machine.start();
48+
// } catch (Exception e) {
49+
// logger.warn("Failed to initialize state machine", e);
50+
// }
51+
// }
52+
//}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package statemachine.demo.action;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.EnumSet;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.springframework.statemachine.StateMachine;
9+
import org.springframework.statemachine.config.StateMachineBuilder;
10+
import org.springframework.statemachine.config.StateMachineBuilder.Builder;
11+
import statemachine.state.action.ActionEvent;
12+
import statemachine.state.action.ActionState;
13+
14+
/**
15+
* @GitHub : https://github.com/zacscoding
16+
*/
17+
public class ActionStateMachineDemoTest {
18+
19+
StateMachine<ActionState, ActionEvent> actionStateMachine;
20+
21+
@Before
22+
public void setUp() throws Exception {
23+
Builder<ActionState, ActionEvent> builder = StateMachineBuilder.builder();
24+
25+
builder.configureStates()
26+
.withStates()
27+
.initial(ActionState.INIT)
28+
.states(EnumSet.allOf(ActionState.class));
29+
30+
builder.configureTransitions()
31+
.withExternal()
32+
.source(ActionState.INIT).target(ActionState.IN_PROGRESS).event(ActionEvent.ACTION_IN_PROGRESS)
33+
.and()
34+
.withExternal()
35+
.source(ActionState.INIT).target(ActionState.COMPLETED).event(ActionEvent.ACTION_COMPLETE)
36+
.and()
37+
.withExternal()
38+
.source(ActionState.INIT).target(ActionState.FAILED).event(ActionEvent.ACTION_FAILED)
39+
40+
.and()
41+
.withExternal()
42+
.source(ActionState.IN_PROGRESS).target(ActionState.IN_PROGRESS).event(ActionEvent.ACTION_IN_PROGRESS)
43+
.and()
44+
.withExternal()
45+
.source(ActionState.IN_PROGRESS).target(ActionState.COMPLETED).event(ActionEvent.ACTION_COMPLETE)
46+
.and()
47+
.withExternal()
48+
.source(ActionState.IN_PROGRESS).target(ActionState.FAILED).event(ActionEvent.ACTION_FAILED)
49+
50+
.and()
51+
.withExternal()
52+
.source(ActionState.COMPLETED).target(ActionState.INIT).event(ActionEvent.ACTION_INIT)
53+
54+
.and()
55+
.withExternal()
56+
.source(ActionState.FAILED).target(ActionState.INIT).event(ActionEvent.ACTION_INIT);
57+
58+
actionStateMachine = builder.build();
59+
actionStateMachine.start();
60+
}
61+
62+
@Test
63+
public void testTransitions() {
64+
assertThat(actionStateMachine.getState().getId()).isEqualTo(ActionState.INIT);
65+
66+
// INIT state -> (transition) ACTION_COMPLETE -> COMPLETED state
67+
actionStateMachine.sendEvent(ActionEvent.ACTION_COMPLETE);
68+
assertThat(actionStateMachine.getState().getId()).isEqualTo(ActionState.COMPLETED);
69+
70+
// COMPLETED state-> (transition) ACTION_INIT -> INIT state
71+
actionStateMachine.sendEvent(ActionEvent.ACTION_INIT);
72+
assertThat(actionStateMachine.getState().getId()).isEqualTo(ActionState.INIT);
73+
74+
75+
// INIT state -> (transition) ACTION_IN_PROGRESS -> IN_PROGRESS state
76+
actionStateMachine.sendEvent(ActionEvent.ACTION_IN_PROGRESS);
77+
assertThat(actionStateMachine.getState().getId()).isEqualTo(ActionState.IN_PROGRESS);
78+
79+
// IN_PROGRESS state -> (transition) ACTION_INIT -> IN_PROGRESS state
80+
actionStateMachine.sendEvent(ActionEvent.ACTION_INIT);
81+
assertThat(actionStateMachine.getState().getId()).isEqualTo(ActionState.IN_PROGRESS);
82+
}
83+
}

0 commit comments

Comments
 (0)