Add Rapp Instances
[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 public 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"})
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.DEPLOYED).expectStateChanged(1)
96                         .and().build();
97         plan.test();
98     }
99
100     @Test
101     void testDeployFailedState() throws Exception {
102         StateMachineTestPlan plan =
103                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
104                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
105                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
106                         .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
107                         .sendEvent(RappEvent.SMEDEPLOYFAILED).expectState(RappInstanceState.UNDEPLOYED)
108                         .expectStateChanged(1).and().build();
109         plan.test();
110     }
111
112     @Test
113     void testUndeployingState() throws Exception {
114         StateMachineTestPlan plan =
115                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
116                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
117                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
118                         .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
119                         .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
120                         .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
121                         .expectStateChanged(1).and().build();
122         plan.test();
123     }
124
125     @ParameterizedTest
126     @EnumSource(value = RappEvent.class, names = {"ACMUNDEPLOYED", "SMEUNDEPLOYED"})
127     void testIndividualUndeployedState(RappEvent rappEvent) throws Exception {
128         StateMachineTestPlan plan =
129                 StateMachineTestPlanBuilder.<RappInstanceState, RappEvent>builder().stateMachine(stateMachine).step()
130                         .expectState(RappInstanceState.UNDEPLOYED).and().step().sendEvent(RappEvent.DEPLOYING)
131                         .expectState(RappInstanceState.DEPLOYING).expectStateChanged(1).and().step()
132                         .sendEvent(RappEvent.ACMDEPLOYED).expectState(RappInstanceState.DEPLOYING).and().step()
133                         .sendEvent(RappEvent.SMEDEPLOYED).expectState(RappInstanceState.DEPLOYED).expectStateChanged(1)
134                         .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
135                         .expectStateChanged(1).and().step().sendEvent(rappEvent)
136                         .expectState(RappInstanceState.UNDEPLOYING).and().build();
137         plan.test();
138     }
139
140     @Test
141     void testUndeployedState() 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.DEPLOYED).expectStateChanged(1)
148                         .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
149                         .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
150                         .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.SMEUNDEPLOYED)
151                         .expectState(RappInstanceState.UNDEPLOYED).expectStateChanged(1).and().build();
152         plan.test();
153     }
154
155     @Test
156     void testUndeployFailedState() 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.DEPLOYED).expectStateChanged(1)
163                         .and().step().sendEvent(RappEvent.UNDEPLOYING).expectState(RappInstanceState.UNDEPLOYING)
164                         .expectStateChanged(1).and().step().sendEvent(RappEvent.ACMUNDEPLOYED)
165                         .expectState(RappInstanceState.UNDEPLOYING).and().step().sendEvent(RappEvent.SMEUNDEPLOYFAILED)
166                         .expectState(RappInstanceState.DEPLOYED).expectStateChanged(1).and().build();
167         plan.test();
168     }
169 }