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.doThrow;
26 import static org.mockito.Mockito.when;
28 import com.oransc.rappmanager.acm.service.AcmDeployer;
29 import com.oransc.rappmanager.dme.service.DmeDeployer;
30 import com.oransc.rappmanager.models.exception.RappHandlerException;
31 import com.oransc.rappmanager.models.exception.RappValidationException;
32 import com.oransc.rappmanager.models.rapp.Rapp;
33 import com.oransc.rappmanager.models.rapp.RappState;
34 import com.oransc.rappmanager.models.rappinstance.RappInstance;
35 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
36 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
37 import com.oransc.rappmanager.sme.service.SmeDeployer;
38 import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
39 import java.util.HashMap;
41 import java.util.UUID;
42 import org.junit.jupiter.api.Test;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.boot.test.mock.mockito.MockBean;
47 import org.springframework.http.HttpStatus;
49 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
51 class RappServiceTest {
54 RappService rappService;
57 AcmDeployer acmDeployer;
60 SmeDeployer smeDeployer;
63 DmeDeployer dmeDeployer;
66 DeploymentArtifactsService deploymentArtifactsService;
69 SmeLifecycleManager smeLifecycleManager;
72 RappInstanceStateMachine rappInstanceStateMachine;
74 String validCsarFileLocation = "src/test/resources/";
76 private final String validRappFile = "valid-rapp-package.csar";
78 private final String STATE_TRANSITION_NOT_PERMITTED = "State transition from %s to %s is not permitted.";
82 void testPrimeRapp() {
83 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
84 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
85 when(acmDeployer.primeRapp(any())).thenReturn(true);
86 when(dmeDeployer.primeRapp(any())).thenReturn(true);
87 when(smeDeployer.primeRapp(any())).thenReturn(true);
88 when(deploymentArtifactsService.configureDeploymentArtifacts(any())).thenReturn(true);
89 assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode());
90 assertEquals(RappState.PRIMED, rapp.getState());
94 void testPrimeRappInvalidState() {
95 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
96 .packageLocation(validCsarFileLocation).state(RappState.PRIMING).build();
97 RappHandlerException rappHandlerException =
98 assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp));
99 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
100 assertEquals(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.PRIMING, RappState.PRIMED),
101 rappHandlerException.getMessage());
102 assertEquals(RappState.PRIMING, rapp.getState());
106 void testPrimeRappAcmFailure() {
107 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
108 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
109 when(deploymentArtifactsService.configureDeploymentArtifacts(any())).thenReturn(true);
110 when(acmDeployer.primeRapp(any())).thenReturn(false);
111 when(dmeDeployer.primeRapp(any())).thenReturn(true);
112 RappHandlerException rappHandlerException =
113 assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp));
114 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
115 assertEquals(RappState.COMMISSIONED, rapp.getState());
119 void testPrimeRappDmeFailure() {
120 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
121 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
122 when(deploymentArtifactsService.configureDeploymentArtifacts(any())).thenReturn(true);
123 when(acmDeployer.primeRapp(any())).thenReturn(true);
124 when(dmeDeployer.primeRapp(any())).thenReturn(false);
125 RappHandlerException rappHandlerException =
126 assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp));
127 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
128 assertEquals(RappState.COMMISSIONED, rapp.getState());
132 void testPrimeRappHelmUploadFailure() {
133 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
134 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
135 doThrow(new RappValidationException("")).when(deploymentArtifactsService).configureDeploymentArtifacts(any());
136 when(acmDeployer.primeRapp(any())).thenReturn(true);
137 when(dmeDeployer.primeRapp(any())).thenReturn(false);
138 RappValidationException rappValidationException =
139 assertThrows(RappValidationException.class, () -> rappService.primeRapp(rapp));
140 assertEquals(HttpStatus.BAD_REQUEST, rappValidationException.getStatusCode());
141 assertEquals(RappState.COMMISSIONED, rapp.getState());
145 void testPrimeRappDeployArtifactFailure() {
146 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
147 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
148 when(deploymentArtifactsService.configureDeploymentArtifacts(any())).thenReturn(false);
149 RappHandlerException rappHandlerException =
150 assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp));
151 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
152 assertEquals(RappState.COMMISSIONED, rapp.getState());
156 void testDeprimeRapp() {
157 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
158 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
159 when(acmDeployer.deprimeRapp(any())).thenReturn(true);
160 when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
161 when(smeDeployer.deprimeRapp(any())).thenReturn(true);
162 assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode());
163 assertEquals(RappState.COMMISSIONED, rapp.getState());
167 void testDeprimeRappAcmFailure() {
168 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
169 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
170 when(acmDeployer.deprimeRapp(any())).thenReturn(false);
171 when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
172 RappHandlerException rappHandlerException =
173 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
174 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
175 assertEquals(RappState.PRIMED, rapp.getState());
179 void testDeprimeRappDmeFailure() {
180 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
181 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
182 when(acmDeployer.deprimeRapp(any())).thenReturn(true);
183 when(dmeDeployer.deprimeRapp(any())).thenReturn(false);
184 RappHandlerException rappHandlerException =
185 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
186 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
187 assertEquals(RappState.PRIMED, rapp.getState());
191 void testDeprimeRappInvalidState() {
192 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
193 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
194 RappHandlerException rappHandlerException =
195 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
196 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
197 assertEquals(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED, RappState.COMMISSIONED),
198 rappHandlerException.getMessage());
199 assertEquals(RappState.COMMISSIONED, rapp.getState());
203 void testDeprimeRappActiveInstances() {
204 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
205 .packageLocation(validCsarFileLocation).state(RappState.PRIMED)
206 .rappInstances(Map.of(UUID.randomUUID(), new RappInstance())).build();
207 RappHandlerException rappHandlerException =
208 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
209 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
210 assertEquals(RappState.PRIMED, rapp.getState());
214 void testDeployRappInstance() {
215 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
216 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
217 RappInstance rappInstance = new RappInstance();
218 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
219 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
220 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
221 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
222 assertEquals(HttpStatus.ACCEPTED, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
226 void testDeployRappInstanceFailure() {
227 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
228 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
229 RappInstance rappInstance = new RappInstance();
230 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
231 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
232 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(false);
233 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
234 RappHandlerException rappHandlerException =
235 assertThrows(RappHandlerException.class, () -> rappService.deployRappInstance(rapp, rappInstance));
236 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
240 void testDeployRappInstanceFailureWithState() {
241 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
242 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
243 RappInstance rappInstance = new RappInstance();
244 rappInstance.setState(RappInstanceState.DEPLOYED);
245 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
246 RappHandlerException rappHandlerException =
247 assertThrows(RappHandlerException.class, () -> rappService.deployRappInstance(rapp, rappInstance));
248 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
249 assertEquals(String.format("Unable to deploy rApp instance %s as it is not in UNDEPLOYED state",
250 rappInstance.getRappInstanceId()), rappHandlerException.getMessage());
251 assertEquals(RappState.PRIMED, rapp.getState());
256 void testUndeployRappInstance() {
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.DEPLOYED);
261 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
262 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
263 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
264 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
265 assertEquals(HttpStatus.ACCEPTED, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
269 void testUndeployRappInstanceFailure() {
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.DEPLOYED);
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_GATEWAY, rappHandlerException.getStatusCode());
284 void testUndeployRappInstanceInvalidStateFailure() {
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.DEPLOYING);
289 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
290 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
291 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
292 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
293 RappHandlerException rappHandlerException =
294 assertThrows(RappHandlerException.class, () -> rappService.undeployRappInstance(rapp, rappInstance));
295 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
299 void testDeleteRappInstance() {
300 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
301 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
302 RappInstance rappInstance = new RappInstance();
303 rappInstance.setState(RappInstanceState.UNDEPLOYED);
304 HashMap<UUID, RappInstance> rAppInstanceMap = new HashMap<>();
305 rAppInstanceMap.put(rappInstance.getRappInstanceId(), rappInstance);
306 rapp.setRappInstances(rAppInstanceMap);
307 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
308 assertEquals(HttpStatus.NO_CONTENT,
309 rappService.deleteRappInstance(rapp, rappInstance.getRappInstanceId()).getStatusCode());
313 void testDeleteRappInstanceFailure() {
314 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
315 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
316 RappInstance rappInstance = new RappInstance();
317 rappInstance.setState(RappInstanceState.DEPLOYED);
318 UUID rappInstanceId = rappInstance.getRappInstanceId();
319 HashMap<UUID, RappInstance> rAppInstanceMap = new HashMap<>();
320 rAppInstanceMap.put(rappInstanceId, rappInstance);
321 rapp.setRappInstances(rAppInstanceMap);
322 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
323 RappHandlerException rappHandlerException =
324 assertThrows(RappHandlerException.class, () -> rappService.deleteRappInstance(rapp, rappInstanceId));
325 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
326 assertEquals(String.format("Unable to delete rApp instance %s as it is not in UNDEPLOYED state",
327 rappInstance.getRappInstanceId()), rappHandlerException.getMessage());
328 assertEquals(RappState.PRIMED, rapp.getState());
332 void testDeleteRappSuccess() {
333 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
334 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
335 assertEquals(HttpStatus.OK, rappService.deleteRapp(rApp).getStatusCode());
339 void testDeleteRappFailureWithState() {
340 String rAppName = "rAppInPrimed";
341 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
342 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
343 RappHandlerException rappHandlerException =
344 assertThrows(RappHandlerException.class, () -> rappService.deleteRapp(rApp));
345 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
346 assertEquals(String.format("Unable to delete %s as the rApp is not in COMMISSIONED state.", rAppName),
347 rappHandlerException.getMessage());
348 assertEquals(RappState.PRIMED, rApp.getState());
352 void testDeleteRappFailureWithInstances() {
353 String rAppName = "rAppWithInstances";
354 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
355 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
356 RappInstance rappInstance = new RappInstance();
357 rappInstance.setState(RappInstanceState.DEPLOYED);
358 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
359 rApp.setRappInstances(Map.of(rappInstance.getRappInstanceId(), rappInstance));
360 RappHandlerException rappHandlerException =
361 assertThrows(RappHandlerException.class, () -> rappService.deleteRapp(rApp));
362 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
363 assertEquals(String.format("Unable to delete %s as there are active rApp instances.", rAppName),
364 rappHandlerException.getMessage());
365 assertEquals(RappState.PRIMED, rApp.getState());