Add initial version of code
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / test / java / com / oransc / rappmanager / statemachine / RappStateMachineTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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========================================================================
17  */
18
19 package com.oransc.rappmanager.statemachine;
20
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;
35
36 @SpringBootTest
37 public class RappStateMachineTest {
38
39     @Autowired
40     StateMachineFactory<RappState, RappEvent> stateMachineFactory;
41
42     StateMachine<RappState, RappEvent> stateMachine;
43
44     @BeforeEach
45     void getStateMachine() {
46         stateMachine = stateMachineFactory.getStateMachine(UUID.randomUUID());
47         stateMachine.startReactively().subscribe();
48     }
49
50     @AfterEach
51     void stopStateMachine() {
52         stateMachine.stopReactively().subscribe();
53     }
54
55     @Test
56     void testOnboardedState() throws Exception {
57         StateMachineTestPlan plan =
58                 StateMachineTestPlanBuilder.<RappState, RappEvent>builder().stateMachine(stateMachine).step()
59                         .expectState(RappState.ONBOARDED).and().build();
60         plan.test();
61     }
62
63     @Test
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();
69         plan.test();
70     }
71
72     @ParameterizedTest
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();
80         plan.test();
81     }
82
83     @Test
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()
91                         .build();
92         plan.test();
93     }
94
95     @Test
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()
103                         .build();
104         plan.test();
105     }
106
107     @Test
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();
117         plan.test();
118     }
119
120     @ParameterizedTest
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)
131                         .and().build();
132         plan.test();
133     }
134
135     @Test
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();
147         plan.test();
148     }
149
150     @Test
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();
162         plan.test();
163     }
164
165
166 }