2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4 * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
5 * ===============================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END========================================================================
20 package com.oransc.rappmanager.models.statemachine;
22 import com.oransc.rappmanager.models.rapp.RappEvent;
23 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
24 import java.util.UUID;
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.extension.ExtendWith;
29 import org.junit.jupiter.params.ParameterizedTest;
30 import org.junit.jupiter.params.provider.EnumSource;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.boot.test.context.SpringBootTest;
33 import org.springframework.statemachine.StateMachine;
34 import org.springframework.statemachine.config.StateMachineFactory;
35 import org.springframework.statemachine.test.StateMachineTestPlan;
36 import org.springframework.statemachine.test.StateMachineTestPlanBuilder;
37 import org.springframework.test.annotation.DirtiesContext;
38 import org.springframework.test.context.junit.jupiter.SpringExtension;
40 @ExtendWith(SpringExtension.class)
41 @SpringBootTest(classes = {RappInstanceStateMachineConfig.class})
42 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
43 class RappInstanceStateMachineConfigTest {
46 StateMachineFactory<RappInstanceState, RappEvent> stateMachineFactory;
48 StateMachine<RappInstanceState, RappEvent> stateMachine;
51 void getStateMachine() {
52 stateMachine = stateMachineFactory.getStateMachine(UUID.randomUUID());
53 stateMachine.getExtendedState().getVariables().put("sme", true);
54 stateMachine.getExtendedState().getVariables().put("dme", true);
55 stateMachine.startReactively().subscribe();
59 void stopStateMachine() {
60 stateMachine.stopReactively().subscribe();
64 void testOnboardedState() throws Exception {
65 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
66 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
67 .expectState(RappInstanceState.UNDEPLOYED).and().build();
72 void testDeployingState() throws Exception {
73 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
74 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
75 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
76 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().build();
81 @EnumSource(value = RappEvent.class, names = {"ACMDEPLOYED", "SMEDEPLOYED", "DMEDEPLOYED"})
82 void testIndividualDeployedState(RappEvent rappEvent) throws Exception {
83 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
84 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
85 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
86 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
87 .sendEvent(rappEvent).expectState(RappInstanceState.DEPLOYING).and().build();
92 void testDeployedState() throws Exception {
93 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
94 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
95 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
96 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
97 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
98 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
99 .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
105 void testDeployedStateAcmOnly() throws Exception {
106 stateMachine.getExtendedState().getVariables().put("sme", false);
107 stateMachine.getExtendedState().getVariables().put("dme", false);
108 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
109 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
110 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
111 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
112 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
118 void testDeployedStateAcmOnlyWithNoKeyReference() throws Exception {
119 stateMachine.getExtendedState().getVariables().remove("sme");
120 stateMachine.getExtendedState().getVariables().remove("dme");
121 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
122 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
123 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
124 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
125 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
131 void testDeployedStateAcmAndSmeOnly() throws Exception {
132 stateMachine.getExtendedState().getVariables().put("dme", false);
133 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
134 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
135 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
136 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
137 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
138 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
144 void testDeployedStateAcmAndDmeOnly() throws Exception {
145 stateMachine.getExtendedState().getVariables().put("sme", false);
146 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
147 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
148 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
149 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
150 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
151 .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
157 void testAcmDeployFailedState() throws Exception {
158 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
159 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
160 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
161 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
162 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
163 .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
164 .sendEvent(RappEvent.ACMDEPLOYFAILED).expectState(RappInstanceState.UNDEPLOYED)
165 .expectStateChanged(1).and().build();
170 void testSmeDeployFailedState() throws Exception {
171 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
172 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
173 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
174 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
175 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
176 .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
177 .sendEvent(RappEvent.SMEDEPLOYFAILED).expectState(RappInstanceState.UNDEPLOYED)
178 .expectStateChanged(1).and().build();
183 void testDmeDeployFailedState() throws Exception {
184 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
185 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
186 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
187 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
188 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
189 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
190 .sendEvent(RappEvent.DMEDEPLOYFAILED).expectState(RappInstanceState.UNDEPLOYED)
191 .expectStateChanged(1).and().build();
196 void testUndeployingState() throws Exception {
197 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
198 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
199 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
200 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
201 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
202 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
203 .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
204 .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
205 .expectStateChanged(1).and().build();
210 @EnumSource(value = RappEvent.class, names = {"ACMUNDEPLOYED", "SMEUNDEPLOYED", "DMEUNDEPLOYED"})
211 void testIndividualUndeployedState(RappEvent rappEvent) throws Exception {
212 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
213 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
214 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
215 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
216 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
217 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
218 .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
219 .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
220 .expectStateChanged(1).and().step().sendEvent(rappEvent)
221 .expectState(RappInstanceState.UNDEPLOYING).and().build();
226 void testUndeployedState() throws Exception {
227 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
228 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
229 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
230 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
231 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
232 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
233 .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
234 .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
235 .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
236 .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.SMEUNDEPLOYED)
237 .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.DMEUNDEPLOYED)
238 .expectState(RappInstanceState.UNDEPLOYED).expectStateChanged(1).and().build();
244 void testUndeployedStateAcmOnly() throws Exception {
245 stateMachine.getExtendedState().getVariables().put("sme", false);
246 stateMachine.getExtendedState().getVariables().put("dme", false);
247 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
248 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
249 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
250 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
251 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
252 .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
253 .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
254 .expectState(RappInstanceState.UNDEPLOYED).expectStateChanged(1).and().build();
259 void testUndeployedStateAcmOnlyWithNoKeyReference() throws Exception {
260 stateMachine.getExtendedState().getVariables().remove("sme");
261 stateMachine.getExtendedState().getVariables().remove("dme");
262 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
263 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
264 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
265 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
266 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
267 .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
268 .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
269 .expectState(RappInstanceState.UNDEPLOYED).expectStateChanged(1).and().build();
274 void testUndeployedStateAcmAndSmeOnly() throws Exception {
275 stateMachine.getExtendedState().getVariables().put("dme", false);
276 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
277 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
278 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
279 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
280 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
281 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
282 .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
283 .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
284 .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.SMEUNDEPLOYED)
285 .expectState(RappInstanceState.UNDEPLOYED).expectStateChanged(1).and().build();
290 void testUndeployedStateAcmAndDmeOnly() throws Exception {
291 stateMachine.getExtendedState().getVariables().put("sme", false);
292 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
293 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
294 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
295 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
296 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
297 .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
298 .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
299 .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
300 .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.DMEUNDEPLOYED)
301 .expectState(RappInstanceState.UNDEPLOYED).expectStateChanged(1).and().build();
306 void testUndeployAcmFailedState() throws Exception {
307 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
308 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
309 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
310 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
311 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
312 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
313 .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
314 .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
315 .expectStateChanged(1).and().step().sendEvent(RappEvent.SMEUNDEPLOYED)
316 .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.DMEUNDEPLOYED)
317 .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.ACMUNDEPLOYFAILED)
318 .expectState(RappInstanceState.DEPLOYED).expectStateChanged(1).and().build();
323 void testUndeploySmeFailedState() throws Exception {
324 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
325 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
326 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
327 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
328 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
329 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
330 .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
331 .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
332 .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
333 .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.DMEUNDEPLOYED)
334 .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.SMEUNDEPLOYFAILED)
335 .expectState(RappInstanceState.DEPLOYED).expectStateChanged(1).and().build();
340 void testUndeployDmeFailedState() throws Exception {
341 StateMachineTestPlan<RappInstanceState, RappEvent> plan =
342 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
343 .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
344 .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
345 .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
346 .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
347 .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
348 .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
349 .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
350 .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.SMEUNDEPLOYED)
351 .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.DMEUNDEPLOYFAILED)
352 .expectState(RappInstanceState.DEPLOYED).expectStateChanged(1).and().build();