2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4 * Copyright (C) 2023-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.service;
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.when;
27 import com.oransc.rappmanager.acm.service.AcmDeployer;
28 import com.oransc.rappmanager.dme.service.DmeDeployer;
29 import com.oransc.rappmanager.models.exception.RappHandlerException;
30 import com.oransc.rappmanager.models.rapp.Rapp;
31 import com.oransc.rappmanager.models.rapp.RappState;
32 import com.oransc.rappmanager.models.rappinstance.RappInstance;
33 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
34 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
35 import com.oransc.rappmanager.sme.service.SmeDeployer;
36 import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
37 import java.util.HashMap;
39 import java.util.UUID;
40 import org.junit.jupiter.api.Test;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.boot.test.mock.mockito.MockBean;
45 import org.springframework.http.HttpStatus;
47 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
49 class RappServiceTest {
52 RappService rappService;
55 AcmDeployer acmDeployer;
58 SmeDeployer smeDeployer;
61 DmeDeployer dmeDeployer;
64 SmeLifecycleManager smeLifecycleManager;
67 RappInstanceStateMachine rappInstanceStateMachine;
69 String validCsarFileLocation = "src/test/resources/";
71 private final String validRappFile = "valid-rapp-package.csar";
73 private final String STATE_TRANSITION_NOT_PERMITTED = "State transition from %s to %s is not permitted.";
77 void testPrimeRapp() {
78 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
79 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
80 when(acmDeployer.primeRapp(any())).thenReturn(true);
81 when(dmeDeployer.primeRapp(any())).thenReturn(true);
82 when(smeDeployer.primeRapp(any())).thenReturn(true);
83 assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode());
84 assertEquals(RappState.PRIMED, rapp.getState());
88 void testPrimeRappInvalidState() {
89 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
90 .packageLocation(validCsarFileLocation).state(RappState.PRIMING).build();
91 RappHandlerException rappHandlerException =
92 assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp));
93 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
94 assertEquals(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.PRIMING, RappState.PRIMED),
95 rappHandlerException.getMessage());
96 assertEquals(RappState.PRIMING, rapp.getState());
100 void testPrimeRappAcmFailure() {
101 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
102 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
103 when(acmDeployer.primeRapp(any())).thenReturn(false);
104 when(dmeDeployer.primeRapp(any())).thenReturn(true);
105 assertEquals(HttpStatus.BAD_GATEWAY, rappService.primeRapp(rapp).getStatusCode());
106 assertEquals(RappState.COMMISSIONED, rapp.getState());
110 void testPrimeRappDmeFailure() {
111 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
112 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
113 when(acmDeployer.primeRapp(any())).thenReturn(true);
114 when(dmeDeployer.primeRapp(any())).thenReturn(false);
115 assertEquals(HttpStatus.BAD_GATEWAY, rappService.primeRapp(rapp).getStatusCode());
116 assertEquals(RappState.COMMISSIONED, rapp.getState());
121 void testDeprimeRapp() {
122 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
123 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
124 when(acmDeployer.deprimeRapp(any())).thenReturn(true);
125 when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
126 when(smeDeployer.deprimeRapp(any())).thenReturn(true);
127 assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode());
128 assertEquals(RappState.COMMISSIONED, rapp.getState());
132 void testDeprimeRappAcmFailure() {
133 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
134 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
135 when(acmDeployer.deprimeRapp(any())).thenReturn(false);
136 when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
137 assertEquals(HttpStatus.BAD_GATEWAY, rappService.deprimeRapp(rapp).getStatusCode());
138 assertEquals(RappState.PRIMED, rapp.getState());
142 void testDeprimeRappDmeFailure() {
143 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
144 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
145 when(acmDeployer.deprimeRapp(any())).thenReturn(true);
146 when(dmeDeployer.deprimeRapp(any())).thenReturn(false);
147 assertEquals(HttpStatus.BAD_GATEWAY, rappService.deprimeRapp(rapp).getStatusCode());
148 assertEquals(RappState.PRIMED, rapp.getState());
152 void testDeprimeRappInvalidState() {
153 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
154 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
155 RappHandlerException rappHandlerException =
156 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
157 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
158 assertEquals(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED, RappState.COMMISSIONED),
159 rappHandlerException.getMessage());
160 assertEquals(RappState.COMMISSIONED, rapp.getState());
164 void testDeprimeRappActiveInstances() {
165 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
166 .packageLocation(validCsarFileLocation).state(RappState.PRIMED)
167 .rappInstances(Map.of(UUID.randomUUID(), new RappInstance())).build();
168 RappHandlerException rappHandlerException =
169 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
170 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
171 assertEquals(RappState.PRIMED, rapp.getState());
175 void testDeployRappInstance() {
176 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
177 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
178 RappInstance rappInstance = new RappInstance();
179 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
180 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
181 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
182 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
183 assertEquals(HttpStatus.ACCEPTED, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
187 void testDeployRappInstanceFailure() {
188 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
189 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
190 RappInstance rappInstance = new RappInstance();
191 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
192 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
193 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(false);
194 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
195 assertEquals(HttpStatus.BAD_GATEWAY, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
199 void testDeployRappInstanceFailureWithState() {
200 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
201 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
202 RappInstance rappInstance = new RappInstance();
203 rappInstance.setState(RappInstanceState.DEPLOYED);
204 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
205 RappHandlerException rappHandlerException =
206 assertThrows(RappHandlerException.class, () -> rappService.deployRappInstance(rapp, rappInstance));
207 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
208 assertEquals(String.format("Unable to deploy rApp instance %s as it is not in UNDEPLOYED state",
209 rappInstance.getRappInstanceId()), rappHandlerException.getMessage());
210 assertEquals(RappState.PRIMED, rapp.getState());
215 void testUndeployRappInstance() {
216 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
217 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
218 RappInstance rappInstance = new RappInstance();
219 rappInstance.setState(RappInstanceState.DEPLOYED);
220 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
221 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
222 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
223 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
224 assertEquals(HttpStatus.ACCEPTED, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
228 void testUndeployRappInstanceFailure() {
229 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
230 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
231 RappInstance rappInstance = new RappInstance();
232 rappInstance.setState(RappInstanceState.DEPLOYED);
233 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
234 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
235 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
236 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
237 assertEquals(HttpStatus.BAD_GATEWAY, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
241 void testUndeployRappInstanceInvalidStateFailure() {
242 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
243 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
244 RappInstance rappInstance = new RappInstance();
245 rappInstance.setState(RappInstanceState.DEPLOYING);
246 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
247 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
248 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
249 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
250 RappHandlerException rappHandlerException =
251 assertThrows(RappHandlerException.class, () -> rappService.undeployRappInstance(rapp, rappInstance));
252 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
256 void testDeleteRappInstance() {
257 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
258 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
259 RappInstance rappInstance = new RappInstance();
260 rappInstance.setState(RappInstanceState.UNDEPLOYED);
261 HashMap<UUID, RappInstance> rAppInstanceMap = new HashMap<>();
262 rAppInstanceMap.put(rappInstance.getRappInstanceId(), rappInstance);
263 rapp.setRappInstances(rAppInstanceMap);
264 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
265 assertEquals(HttpStatus.NO_CONTENT,
266 rappService.deleteRappInstance(rapp, rappInstance.getRappInstanceId()).getStatusCode());
270 void testDeleteRappInstanceFailure() {
271 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
272 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
273 RappInstance rappInstance = new RappInstance();
274 rappInstance.setState(RappInstanceState.DEPLOYED);
275 UUID rappInstanceId = rappInstance.getRappInstanceId();
276 HashMap<UUID, RappInstance> rAppInstanceMap = new HashMap<>();
277 rAppInstanceMap.put(rappInstanceId, rappInstance);
278 rapp.setRappInstances(rAppInstanceMap);
279 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
280 RappHandlerException rappHandlerException =
281 assertThrows(RappHandlerException.class, () -> rappService.deleteRappInstance(rapp, rappInstanceId));
282 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
283 assertEquals(String.format("Unable to delete rApp instance %s as it is not in UNDEPLOYED state",
284 rappInstance.getRappInstanceId()), rappHandlerException.getMessage());
285 assertEquals(RappState.PRIMED, rapp.getState());
289 void testDeleteRappSuccess() {
290 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
291 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
292 assertEquals(HttpStatus.OK, rappService.deleteRapp(rApp).getStatusCode());
296 void testDeleteRappFailureWithState() {
297 String rAppName = "rAppInPrimed";
298 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
299 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
300 RappHandlerException rappHandlerException =
301 assertThrows(RappHandlerException.class, () -> rappService.deleteRapp(rApp));
302 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
303 assertEquals(String.format("Unable to delete %s as the rApp is not in COMMISSIONED state.", rAppName),
304 rappHandlerException.getMessage());
305 assertEquals(RappState.PRIMED, rApp.getState());
309 void testDeleteRappFailureWithInstances() {
310 String rAppName = "rAppWithInstances";
311 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
312 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
313 RappInstance rappInstance = new RappInstance();
314 rappInstance.setState(RappInstanceState.DEPLOYED);
315 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
316 rApp.setRappInstances(Map.of(rappInstance.getRappInstanceId(), rappInstance));
317 RappHandlerException rappHandlerException =
318 assertThrows(RappHandlerException.class, () -> rappService.deleteRapp(rApp));
319 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
320 assertEquals(String.format("Unable to delete %s as there are active rApp instances.", rAppName),
321 rappHandlerException.getMessage());
322 assertEquals(RappState.PRIMED, rApp.getState());