Add installation mode to use snapshot image
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / test / java / com / oransc / rappmanager / models / statemachine / RappInstanceStateMachineConfigTest.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.models.statemachine;
20
21 import com.oransc.rappmanager.models.rapp.RappEvent;
22 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
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.api.extension.ExtendWith;
28 import org.junit.jupiter.params.ParameterizedTest;
29 import org.junit.jupiter.params.provider.EnumSource;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.boot.test.context.SpringBootTest;
32 import org.springframework.statemachine.StateMachine;
33 import org.springframework.statemachine.config.StateMachineFactory;
34 import org.springframework.statemachine.test.StateMachineTestPlan;
35 import org.springframework.statemachine.test.StateMachineTestPlanBuilder;
36 import org.springframework.test.annotation.DirtiesContext;
37 import org.springframework.test.context.junit.jupiter.SpringExtension;
38
39 @ExtendWith(SpringExtension.class)
40 @SpringBootTest(classes = {RappInstanceStateMachineConfig.class})
41 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
42 class RappInstanceStateMachineConfigTest {
43
44     @Autowired
45     StateMachineFactory<RappInstanceState, RappEvent> stateMachineFactory;
46
47     StateMachine<RappInstanceState, RappEvent> stateMachine;
48
49     @BeforeEach
50     void getStateMachine() {
51         stateMachine = stateMachineFactory.getStateMachine(UUID.randomUUID());
52         stateMachine.startReactively().subscribe();
53     }
54
55     @AfterEach
56     void stopStateMachine() {
57         stateMachine.stopReactively().subscribe();
58     }
59
60     @Test
61     void testOnboardedState() throws Exception {
62         StateMachineTestPlan plan =
63                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
64                         .expectState(RappInstanceState.UNDEPLOYED).and().build();
65         plan.test();
66     }
67
68     @Test
69     void testDeployingState() throws Exception {
70         StateMachineTestPlan plan =
71                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
72                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
73                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().build();
74         plan.test();
75     }
76
77     @ParameterizedTest
78     @EnumSource(value = RappEvent.class, names = {"ACMDEPLOYED", "SMEDEPLOYED", "DMEDEPLOYED" })
79     void testIndividualDeployedState(RappEvent rappEvent) throws Exception {
80         StateMachineTestPlan plan =
81                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
82                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
83                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
84                         .sendEvent(rappEvent).expectState(RappInstanceState.DEPLOYING).and().build();
85         plan.test();
86     }
87
88     @Test
89     void testDeployedState() throws Exception {
90         StateMachineTestPlan plan =
91                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
92                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
93                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
94                         .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
95                         .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
96                         .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
97                         .and().build();
98         plan.test();
99     }
100
101     @Test
102     void testAcmDeployFailedState() throws Exception {
103         StateMachineTestPlan plan =
104                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
105                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
106                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
107                         .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
108                         .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
109                         .sendEvent(RappEvent.ACMDEPLOYFAILED).expectState(RappInstanceState.UNDEPLOYED)
110                         .expectStateChanged(1).and().build();
111         plan.test();
112     }
113
114     @Test
115     void testSmeDeployFailedState() throws Exception {
116         StateMachineTestPlan plan =
117                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
118                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
119                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
120                         .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
121                         .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
122                         .sendEvent(RappEvent.SMEDEPLOYFAILED).expectState(RappInstanceState.UNDEPLOYED)
123                         .expectStateChanged(1).and().build();
124         plan.test();
125     }
126
127     @Test
128     void testDmeDeployFailedState() throws Exception {
129         StateMachineTestPlan plan =
130                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
131                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
132                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
133                         .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
134                         .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
135                         .sendEvent(RappEvent.DMEDEPLOYFAILED).expectState(RappInstanceState.UNDEPLOYED)
136                         .expectStateChanged(1).and().build();
137         plan.test();
138     }
139
140     @Test
141     void testUndeployingState() throws Exception {
142         StateMachineTestPlan plan =
143                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
144                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
145                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
146                         .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
147                         .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
148                         .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
149                         .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
150                         .expectStateChanged(1).and().build();
151         plan.test();
152     }
153
154     @ParameterizedTest
155     @EnumSource(value = RappEvent.class, names = {"ACMUNDEPLOYED", "SMEUNDEPLOYED", "DMEUNDEPLOYED" })
156     void testIndividualUndeployedState(RappEvent rappEvent) throws Exception {
157         StateMachineTestPlan plan =
158                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
159                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
160                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
161                         .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
162                         .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
163                         .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
164                         .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
165                         .expectStateChanged(1).and().step().sendEvent(rappEvent)
166                         .expectState(RappInstanceState.UNDEPLOYING).and().build();
167         plan.test();
168     }
169
170     @Test
171     void testUndeployedState() throws Exception {
172         StateMachineTestPlan plan =
173                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
174                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
175                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
176                         .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
177                         .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
178                         .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
179                         .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
180                         .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
181                         .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.SMEUNDEPLOYED)
182                         .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.DMEUNDEPLOYED)
183                         .expectStateChanged(1).and().build();
184         plan.test();
185     }
186
187     @Test
188     void testUndeployAcmFailedState() throws Exception {
189         StateMachineTestPlan plan =
190                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
191                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
192                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
193                         .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
194                         .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
195                         .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
196                         .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
197                         .expectStateChanged(1).and().step().sendEvent(RappEvent.SMEUNDEPLOYED)
198                         .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.DMEUNDEPLOYED)
199                         .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.ACMUNDEPLOYFAILED)
200                         .expectState(RappInstanceState.DEPLOYED).expectStateChanged(1).and().build();
201         plan.test();
202     }
203
204     @Test
205     void testUndeploySmeFailedState() throws Exception {
206         StateMachineTestPlan plan =
207                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
208                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
209                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
210                         .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
211                         .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
212                         .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
213                         .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
214                         .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
215                         .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.DMEUNDEPLOYED)
216                         .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.SMEUNDEPLOYFAILED)
217                         .expectState(RappInstanceState.DEPLOYED).expectStateChanged(1).and().build();
218         plan.test();
219     }
220
221     @Test
222     void testUndeployDmeFailedState() throws Exception {
223         StateMachineTestPlan plan =
224                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
225                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
226                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
227                         .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
228                         .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
229                         .sendEvent(RappEvent.DMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
230                         .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
231                         .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
232                         .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.SMEUNDEPLOYED)
233                         .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.DMEUNDEPLOYFAILED)
234                         .expectState(RappInstanceState.DEPLOYED).expectStateChanged(1).and().build();
235         plan.test();
236     }
237 }