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 RappHandlerException rappHandlerException =
106 assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp));
107 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
108 assertEquals(RappState.COMMISSIONED, rapp.getState());
112 void testPrimeRappDmeFailure() {
113 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
114 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
115 when(acmDeployer.primeRapp(any())).thenReturn(true);
116 when(dmeDeployer.primeRapp(any())).thenReturn(false);
117 RappHandlerException rappHandlerException =
118 assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp));
119 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
120 assertEquals(RappState.COMMISSIONED, rapp.getState());
125 void testDeprimeRapp() {
126 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
127 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
128 when(acmDeployer.deprimeRapp(any())).thenReturn(true);
129 when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
130 when(smeDeployer.deprimeRapp(any())).thenReturn(true);
131 assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode());
132 assertEquals(RappState.COMMISSIONED, rapp.getState());
136 void testDeprimeRappAcmFailure() {
137 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
138 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
139 when(acmDeployer.deprimeRapp(any())).thenReturn(false);
140 when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
141 RappHandlerException rappHandlerException =
142 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
143 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
144 assertEquals(RappState.PRIMED, rapp.getState());
148 void testDeprimeRappDmeFailure() {
149 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
150 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
151 when(acmDeployer.deprimeRapp(any())).thenReturn(true);
152 when(dmeDeployer.deprimeRapp(any())).thenReturn(false);
153 RappHandlerException rappHandlerException =
154 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
155 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
156 assertEquals(RappState.PRIMED, rapp.getState());
160 void testDeprimeRappInvalidState() {
161 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
162 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
163 RappHandlerException rappHandlerException =
164 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
165 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
166 assertEquals(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED, RappState.COMMISSIONED),
167 rappHandlerException.getMessage());
168 assertEquals(RappState.COMMISSIONED, rapp.getState());
172 void testDeprimeRappActiveInstances() {
173 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
174 .packageLocation(validCsarFileLocation).state(RappState.PRIMED)
175 .rappInstances(Map.of(UUID.randomUUID(), new RappInstance())).build();
176 RappHandlerException rappHandlerException =
177 assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
178 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
179 assertEquals(RappState.PRIMED, rapp.getState());
183 void testDeployRappInstance() {
184 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
185 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
186 RappInstance rappInstance = new RappInstance();
187 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
188 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
189 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
190 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
191 assertEquals(HttpStatus.ACCEPTED, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
195 void testDeployRappInstanceFailure() {
196 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
197 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
198 RappInstance rappInstance = new RappInstance();
199 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
200 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
201 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(false);
202 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
203 RappHandlerException rappHandlerException =
204 assertThrows(RappHandlerException.class, () -> rappService.deployRappInstance(rapp, rappInstance));
205 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
209 void testDeployRappInstanceFailureWithState() {
210 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
211 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
212 RappInstance rappInstance = new RappInstance();
213 rappInstance.setState(RappInstanceState.DEPLOYED);
214 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
215 RappHandlerException rappHandlerException =
216 assertThrows(RappHandlerException.class, () -> rappService.deployRappInstance(rapp, rappInstance));
217 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
218 assertEquals(String.format("Unable to deploy rApp instance %s as it is not in UNDEPLOYED state",
219 rappInstance.getRappInstanceId()), rappHandlerException.getMessage());
220 assertEquals(RappState.PRIMED, rapp.getState());
225 void testUndeployRappInstance() {
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 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
232 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
233 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
234 assertEquals(HttpStatus.ACCEPTED, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
238 void testUndeployRappInstanceFailure() {
239 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
240 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
241 RappInstance rappInstance = new RappInstance();
242 rappInstance.setState(RappInstanceState.DEPLOYED);
243 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
244 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
245 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
246 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
247 RappHandlerException rappHandlerException =
248 assertThrows(RappHandlerException.class, () -> rappService.undeployRappInstance(rapp, rappInstance));
249 assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
253 void testUndeployRappInstanceInvalidStateFailure() {
254 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
255 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
256 RappInstance rappInstance = new RappInstance();
257 rappInstance.setState(RappInstanceState.DEPLOYING);
258 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
259 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
260 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
261 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
262 RappHandlerException rappHandlerException =
263 assertThrows(RappHandlerException.class, () -> rappService.undeployRappInstance(rapp, rappInstance));
264 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
268 void testDeleteRappInstance() {
269 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
270 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
271 RappInstance rappInstance = new RappInstance();
272 rappInstance.setState(RappInstanceState.UNDEPLOYED);
273 HashMap<UUID, RappInstance> rAppInstanceMap = new HashMap<>();
274 rAppInstanceMap.put(rappInstance.getRappInstanceId(), rappInstance);
275 rapp.setRappInstances(rAppInstanceMap);
276 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
277 assertEquals(HttpStatus.NO_CONTENT,
278 rappService.deleteRappInstance(rapp, rappInstance.getRappInstanceId()).getStatusCode());
282 void testDeleteRappInstanceFailure() {
283 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
284 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
285 RappInstance rappInstance = new RappInstance();
286 rappInstance.setState(RappInstanceState.DEPLOYED);
287 UUID rappInstanceId = rappInstance.getRappInstanceId();
288 HashMap<UUID, RappInstance> rAppInstanceMap = new HashMap<>();
289 rAppInstanceMap.put(rappInstanceId, rappInstance);
290 rapp.setRappInstances(rAppInstanceMap);
291 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
292 RappHandlerException rappHandlerException =
293 assertThrows(RappHandlerException.class, () -> rappService.deleteRappInstance(rapp, rappInstanceId));
294 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
295 assertEquals(String.format("Unable to delete rApp instance %s as it is not in UNDEPLOYED state",
296 rappInstance.getRappInstanceId()), rappHandlerException.getMessage());
297 assertEquals(RappState.PRIMED, rapp.getState());
301 void testDeleteRappSuccess() {
302 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
303 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
304 assertEquals(HttpStatus.OK, rappService.deleteRapp(rApp).getStatusCode());
308 void testDeleteRappFailureWithState() {
309 String rAppName = "rAppInPrimed";
310 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
311 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
312 RappHandlerException rappHandlerException =
313 assertThrows(RappHandlerException.class, () -> rappService.deleteRapp(rApp));
314 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
315 assertEquals(String.format("Unable to delete %s as the rApp is not in COMMISSIONED state.", rAppName),
316 rappHandlerException.getMessage());
317 assertEquals(RappState.PRIMED, rApp.getState());
321 void testDeleteRappFailureWithInstances() {
322 String rAppName = "rAppWithInstances";
323 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
324 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
325 RappInstance rappInstance = new RappInstance();
326 rappInstance.setState(RappInstanceState.DEPLOYED);
327 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
328 rApp.setRappInstances(Map.of(rappInstance.getRappInstanceId(), rappInstance));
329 RappHandlerException rappHandlerException =
330 assertThrows(RappHandlerException.class, () -> rappService.deleteRapp(rApp));
331 assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode());
332 assertEquals(String.format("Unable to delete %s as there are active rApp instances.", rAppName),
333 rappHandlerException.getMessage());
334 assertEquals(RappState.PRIMED, rApp.getState());