2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4 * ===============================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END========================================================================
19 package com.oransc.rappmanager.statemachine;
21 import com.oransc.rappmanager.models.RappEvent;
22 import com.oransc.rappmanager.models.RappState;
23 import java.util.UUID;
24 import org.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.params.ParameterizedTest;
28 import org.junit.jupiter.params.provider.EnumSource;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.boot.test.context.SpringBootTest;
31 import org.springframework.statemachine.StateMachine;
32 import org.springframework.statemachine.config.StateMachineFactory;
33 import org.springframework.statemachine.test.StateMachineTestPlan;
34 import org.springframework.statemachine.test.StateMachineTestPlanBuilder;
37 public class RappStateMachineTest {
40 StateMachineFactory<RappState, RappEvent> stateMachineFactory;
42 StateMachine<RappState, RappEvent> stateMachine;
45 void getStateMachine() {
46 stateMachine = stateMachineFactory.getStateMachine(UUID.randomUUID());
47 stateMachine.startReactively().subscribe();
51 void stopStateMachine() {
52 stateMachine.stopReactively().subscribe();
56 void testOnboardedState() throws Exception {
57 StateMachineTestPlan plan =
58 StateMachineTestPlanBuilder.<RappState, RappEvent>builder().stateMachine(stateMachine).step()
59 .expectState(RappState.ONBOARDED).and().build();
64 void testDeployingState() throws Exception {
65 StateMachineTestPlan plan =
66 StateMachineTestPlanBuilder.<RappState, RappEvent>builder().stateMachine(stateMachine).step()
67 .expectState(RappState.ONBOARDED).and().step().sendEvent(RappEvent.DEPLOYING)
68 .expectState(RappState.DEPLOYING).expectStateChanged(1).and().build();
73 @EnumSource(value = RappEvent.class, names = {"ACMDEPLOYED", "SMEDEPLOYED"})
74 void testIndividualDeployedState(RappEvent rappEvent) throws Exception {
75 StateMachineTestPlan plan =
76 StateMachineTestPlanBuilder.<RappState, RappEvent>builder().stateMachine(stateMachine).step()
77 .expectState(RappState.ONBOARDED).and().step().sendEvent(RappEvent.DEPLOYING)
78 .expectState(RappState.DEPLOYING).expectStateChanged(1).and().step().sendEvent(rappEvent)
79 .expectState(RappState.DEPLOYING).and().build();
84 void testDeployedState() throws Exception {
85 StateMachineTestPlan plan =
86 StateMachineTestPlanBuilder.<RappState, RappEvent>builder().stateMachine(stateMachine).step()
87 .expectState(RappState.ONBOARDED).and().step().sendEvent(RappEvent.DEPLOYING)
88 .expectState(RappState.DEPLOYING).expectStateChanged(1).and().step()
89 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappState.DEPLOYING).and().step()
90 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappState.DEPLOYED).expectStateChanged(1).and()
96 void testDeployFailedState() throws Exception {
97 StateMachineTestPlan plan =
98 StateMachineTestPlanBuilder.<RappState, RappEvent>builder().stateMachine(stateMachine).step()
99 .expectState(RappState.ONBOARDED).and().step().sendEvent(RappEvent.DEPLOYING)
100 .expectState(RappState.DEPLOYING).expectStateChanged(1).and().step()
101 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappState.DEPLOYING).and().step()
102 .sendEvent(RappEvent.SMEDEPLOYFAILED).expectState(RappState.FAILED).expectStateChanged(1).and()
108 void testUndeployingState() throws Exception {
109 StateMachineTestPlan plan =
110 StateMachineTestPlanBuilder.<RappState, RappEvent>builder().stateMachine(stateMachine).step()
111 .expectState(RappState.ONBOARDED).and().step().sendEvent(RappEvent.DEPLOYING)
112 .expectState(RappState.DEPLOYING).expectStateChanged(1).and().step()
113 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappState.DEPLOYING).and().step()
114 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappState.DEPLOYED).expectStateChanged(1).and()
115 .step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappState.UNDEPLOYING)
116 .expectStateChanged(1).and().build();
121 @EnumSource(value = RappEvent.class, names = {"ACMUNDEPLOYED", "SMEUNDEPLOYED"})
122 void testIndividualUndeployedState(RappEvent rappEvent) throws Exception {
123 StateMachineTestPlan plan =
124 StateMachineTestPlanBuilder.<RappState, RappEvent>builder().stateMachine(stateMachine).step()
125 .expectState(RappState.ONBOARDED).and().step().sendEvent(RappEvent.DEPLOYING)
126 .expectState(RappState.DEPLOYING).expectStateChanged(1).and().step()
127 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappState.DEPLOYING).and().step()
128 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappState.DEPLOYED).expectStateChanged(1).and()
129 .step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappState.UNDEPLOYING)
130 .expectStateChanged(1).and().step().sendEvent(rappEvent).expectState(RappState.UNDEPLOYING)
136 void testUndeployedState() throws Exception {
137 StateMachineTestPlan plan =
138 StateMachineTestPlanBuilder.<RappState, RappEvent>builder().stateMachine(stateMachine).step()
139 .expectState(RappState.ONBOARDED).and().step().sendEvent(RappEvent.DEPLOYING)
140 .expectState(RappState.DEPLOYING).expectStateChanged(1).and().step()
141 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappState.DEPLOYING).and().step()
142 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappState.DEPLOYED).expectStateChanged(1).and()
143 .step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappState.UNDEPLOYING)
144 .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
145 .expectState(RappState.UNDEPLOYING).and().step().sendEvent(RappEvent.SMEUNDEPLOYED)
146 .expectState(RappState.UNDEPLOYED).expectStateChanged(1).and().build();
151 void testUndeployFailedState() throws Exception {
152 StateMachineTestPlan plan =
153 StateMachineTestPlanBuilder.<RappState, RappEvent>builder().stateMachine(stateMachine).step()
154 .expectState(RappState.ONBOARDED).and().step().sendEvent(RappEvent.DEPLOYING)
155 .expectState(RappState.DEPLOYING).expectStateChanged(1).and().step()
156 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappState.DEPLOYING).and().step()
157 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappState.DEPLOYED).expectStateChanged(1).and()
158 .step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappState.UNDEPLOYING)
159 .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
160 .expectState(RappState.UNDEPLOYING).and().step().sendEvent(RappEvent.SMEUNDEPLOYFAILED)
161 .expectState(RappState.FAILED).expectStateChanged(1).and().build();