Add support to upload deployment helm artifacts to chartmuseum
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / test / java / com / oransc / rappmanager / service / RappServiceTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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========================================================================
18  */
19
20 package com.oransc.rappmanager.service;
21
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;
26
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;
38 import java.util.Map;
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;
46
47 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
48 @AutoConfigureMockMvc
49 class RappServiceTest {
50
51     @Autowired
52     RappService rappService;
53
54     @MockBean
55     AcmDeployer acmDeployer;
56
57     @MockBean
58     SmeDeployer smeDeployer;
59
60     @MockBean
61     DmeDeployer dmeDeployer;
62
63     @MockBean
64     DeploymentArtifactsService deploymentArtifactsService;
65
66     @MockBean
67     SmeLifecycleManager smeLifecycleManager;
68
69     @Autowired
70     RappInstanceStateMachine rappInstanceStateMachine;
71
72     String validCsarFileLocation = "src/test/resources/";
73
74     private final String validRappFile = "valid-rapp-package.csar";
75
76     private final String STATE_TRANSITION_NOT_PERMITTED = "State transition from %s to %s is not permitted.";
77
78
79     @Test
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());
89     }
90
91     @Test
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());
101     }
102
103     @Test
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());
114     }
115
116     @Test
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());
127     }
128
129     @Test
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());
138     }
139
140     @Test
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());
149     }
150
151     @Test
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());
161     }
162
163     @Test
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());
173     }
174
175     @Test
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());
185     }
186
187     @Test
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());
196     }
197
198     @Test
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());
208     }
209
210     @Test
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());
222     }
223
224     @Test
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());
237
238     }
239
240     @Test
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());
251     }
252
253     @Test
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());
266     }
267
268     @Test
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());
281     }
282
283     @Test
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());
295     }
296
297     @Test
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());
314     }
315
316     @Test
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());
321     }
322
323     @Test
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());
334     }
335
336     @Test
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());
351     }
352 }