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 DeploymentArtifactsService deploymentArtifactsService;
67 SmeLifecycleManager smeLifecycleManager;
70 RappInstanceStateMachine rappInstanceStateMachine;
72 String validCsarFileLocation = "src/test/resources/";
74 private final String validRappFile = "valid-rapp-package.csar";
76 private final String STATE_TRANSITION_NOT_PERMITTED = "State transition from %s to %s is not permitted.";
80 void testPrimeRapp() {
81 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
82 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
83 when(acmDeployer.primeRapp(any())).thenReturn(true);
84 when(dmeDeployer.primeRapp(any())).thenReturn(true);
85 when(smeDeployer.primeRapp(any())).thenReturn(true);
86 when(deploymentArtifactsService.configureDeploymentArtifacts(any())).thenReturn(true);
87 assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode());
88 assertEquals(RappState.PRIMED, rapp.getState());
92 void testPrimeRappInvalidState() {
93 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
94 .packageLocation(validCsarFileLocation).state(RappState.PRIMING).build();
95 RappHandlerException rappHandlerException =
96 assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp));
97 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
98 assertEquals(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.PRIMING, RappState.PRIMED),
99 rappHandlerException.getMessage());
100 assertEquals(RappState.PRIMING, rapp.getState());
104 void testPrimeRappAcmFailure() {
105 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
106 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
107 when(deploymentArtifactsService.configureDeploymentArtifacts(any())).thenReturn(true);
108 when(acmDeployer.primeRapp(any())).thenReturn(false);
109 when(dmeDeployer.primeRapp(any())).thenReturn(true);
110 RappHandlerException rappHandlerException =
111 assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp));
112 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
113 assertEquals(RappState.COMMISSIONED, rapp.getState());
117 void testPrimeRappDmeFailure() {
118 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
119 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
120 when(deploymentArtifactsService.configureDeploymentArtifacts(any())).thenReturn(true);
121 when(acmDeployer.primeRapp(any())).thenReturn(true);
122 when(dmeDeployer.primeRapp(any())).thenReturn(false);
123 RappHandlerException rappHandlerException =
124 assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp));
125 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
126 assertEquals(RappState.COMMISSIONED, rapp.getState());
130 void testPrimeRappDeployArtifactFailure() {
131 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
132 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
133 when(deploymentArtifactsService.configureDeploymentArtifacts(any())).thenReturn(false);
134 RappHandlerException rappHandlerException =
135 assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp));
136 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
137 assertEquals(RappState.COMMISSIONED, rapp.getState());
141 void testDeprimeRapp() {
142 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
143 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
144 when(acmDeployer.deprimeRapp(any())).thenReturn(true);
145 when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
146 when(smeDeployer.deprimeRapp(any())).thenReturn(true);
147 assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode());
148 assertEquals(RappState.COMMISSIONED, rapp.getState());
152 void testDeprimeRappAcmFailure() {
153 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
154 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
155 when(acmDeployer.deprimeRapp(any())).thenReturn(false);
156 when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
157 RappHandlerException rappHandlerException =
158 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
159 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
160 assertEquals(RappState.PRIMED, rapp.getState());
164 void testDeprimeRappDmeFailure() {
165 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
166 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
167 when(acmDeployer.deprimeRapp(any())).thenReturn(true);
168 when(dmeDeployer.deprimeRapp(any())).thenReturn(false);
169 RappHandlerException rappHandlerException =
170 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
171 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
172 assertEquals(RappState.PRIMED, rapp.getState());
176 void testDeprimeRappInvalidState() {
177 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
178 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
179 RappHandlerException rappHandlerException =
180 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
181 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
182 assertEquals(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED, RappState.COMMISSIONED),
183 rappHandlerException.getMessage());
184 assertEquals(RappState.COMMISSIONED, rapp.getState());
188 void testDeprimeRappActiveInstances() {
189 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
190 .packageLocation(validCsarFileLocation).state(RappState.PRIMED)
191 .rappInstances(Map.of(UUID.randomUUID(), new RappInstance())).build();
192 RappHandlerException rappHandlerException =
193 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
194 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
195 assertEquals(RappState.PRIMED, rapp.getState());
199 void testDeployRappInstance() {
200 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
201 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
202 RappInstance rappInstance = new RappInstance();
203 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
204 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
205 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
206 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
207 assertEquals(HttpStatus.ACCEPTED, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
211 void testDeployRappInstanceFailure() {
212 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
213 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
214 RappInstance rappInstance = new RappInstance();
215 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
216 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
217 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(false);
218 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
219 RappHandlerException rappHandlerException =
220 assertThrows(RappHandlerException.class, () -> rappService.deployRappInstance(rapp, rappInstance));
221 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
225 void testDeployRappInstanceFailureWithState() {
226 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
227 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
228 RappInstance rappInstance = new RappInstance();
229 rappInstance.setState(RappInstanceState.DEPLOYED);
230 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
231 RappHandlerException rappHandlerException =
232 assertThrows(RappHandlerException.class, () -> rappService.deployRappInstance(rapp, rappInstance));
233 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
234 assertEquals(String.format("Unable to deploy rApp instance %s as it is not in UNDEPLOYED state",
235 rappInstance.getRappInstanceId()), rappHandlerException.getMessage());
236 assertEquals(RappState.PRIMED, rapp.getState());
241 void testUndeployRappInstance() {
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.DEPLOYED);
246 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
247 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
248 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
249 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
250 assertEquals(HttpStatus.ACCEPTED, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
254 void testUndeployRappInstanceFailure() {
255 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
256 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
257 RappInstance rappInstance = new RappInstance();
258 rappInstance.setState(RappInstanceState.DEPLOYED);
259 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
260 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
261 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
262 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
263 RappHandlerException rappHandlerException =
264 assertThrows(RappHandlerException.class, () -> rappService.undeployRappInstance(rapp, rappInstance));
265 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
269 void testUndeployRappInstanceInvalidStateFailure() {
270 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
271 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
272 RappInstance rappInstance = new RappInstance();
273 rappInstance.setState(RappInstanceState.DEPLOYING);
274 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
275 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
276 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
277 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
278 RappHandlerException rappHandlerException =
279 assertThrows(RappHandlerException.class, () -> rappService.undeployRappInstance(rapp, rappInstance));
280 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
284 void testDeleteRappInstance() {
285 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
286 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
287 RappInstance rappInstance = new RappInstance();
288 rappInstance.setState(RappInstanceState.UNDEPLOYED);
289 HashMap<UUID, RappInstance> rAppInstanceMap = new HashMap<>();
290 rAppInstanceMap.put(rappInstance.getRappInstanceId(), rappInstance);
291 rapp.setRappInstances(rAppInstanceMap);
292 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
293 assertEquals(HttpStatus.NO_CONTENT,
294 rappService.deleteRappInstance(rapp, rappInstance.getRappInstanceId()).getStatusCode());
298 void testDeleteRappInstanceFailure() {
299 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
300 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
301 RappInstance rappInstance = new RappInstance();
302 rappInstance.setState(RappInstanceState.DEPLOYED);
303 UUID rappInstanceId = rappInstance.getRappInstanceId();
304 HashMap<UUID, RappInstance> rAppInstanceMap = new HashMap<>();
305 rAppInstanceMap.put(rappInstanceId, rappInstance);
306 rapp.setRappInstances(rAppInstanceMap);
307 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
308 RappHandlerException rappHandlerException =
309 assertThrows(RappHandlerException.class, () -> rappService.deleteRappInstance(rapp, rappInstanceId));
310 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
311 assertEquals(String.format("Unable to delete rApp instance %s as it is not in UNDEPLOYED state",
312 rappInstance.getRappInstanceId()), rappHandlerException.getMessage());
313 assertEquals(RappState.PRIMED, rapp.getState());
317 void testDeleteRappSuccess() {
318 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
319 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
320 assertEquals(HttpStatus.OK, rappService.deleteRapp(rApp).getStatusCode());
324 void testDeleteRappFailureWithState() {
325 String rAppName = "rAppInPrimed";
326 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
327 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
328 RappHandlerException rappHandlerException =
329 assertThrows(RappHandlerException.class, () -> rappService.deleteRapp(rApp));
330 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
331 assertEquals(String.format("Unable to delete %s as the rApp is not in COMMISSIONED state.", rAppName),
332 rappHandlerException.getMessage());
333 assertEquals(RappState.PRIMED, rApp.getState());
337 void testDeleteRappFailureWithInstances() {
338 String rAppName = "rAppWithInstances";
339 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
340 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
341 RappInstance rappInstance = new RappInstance();
342 rappInstance.setState(RappInstanceState.DEPLOYED);
343 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
344 rApp.setRappInstances(Map.of(rappInstance.getRappInstanceId(), rappInstance));
345 RappHandlerException rappHandlerException =
346 assertThrows(RappHandlerException.class, () -> rappService.deleteRapp(rApp));
347 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
348 assertEquals(String.format("Unable to delete %s as there are active rApp instances.", rAppName),
349 rappHandlerException.getMessage());
350 assertEquals(RappState.PRIMED, rApp.getState());