Generate rApp packages for unit test
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / test / java / com / oransc / rappmanager / rest / RappInstanceControllerTest.java
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023-2024 OpenInfra Foundation Europe. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END========================================================================
17  */
18
19 package com.oransc.rappmanager.rest;
20
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.when;
24 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
25 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
26
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.oransc.rappmanager.acm.service.AcmDeployer;
29 import com.oransc.rappmanager.dme.service.DmeDeployer;
30 import com.oransc.rappmanager.models.cache.RappCacheService;
31 import com.oransc.rappmanager.models.rapp.Rapp;
32 import com.oransc.rappmanager.models.rapp.RappState;
33 import com.oransc.rappmanager.models.rappinstance.DeployOrder;
34 import com.oransc.rappmanager.models.rappinstance.RappInstance;
35 import com.oransc.rappmanager.models.rappinstance.RappInstanceDeployOrder;
36 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
37 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
38 import com.oransc.rappmanager.sme.service.SmeDeployer;
39 import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
40 import java.util.HashMap;
41 import java.util.Map;
42 import java.util.UUID;
43 import org.junit.jupiter.api.Test;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.boot.test.mock.mockito.MockBean;
48 import org.springframework.http.MediaType;
49 import org.springframework.test.web.servlet.MockMvc;
50 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
51
52 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
53 @AutoConfigureMockMvc
54 class RappInstanceControllerTest {
55
56     @Autowired
57     private MockMvc mockMvc;
58
59     @Autowired
60     private RappCacheService rappCacheService;
61
62     @Autowired
63     private RappInstanceStateMachine rappInstanceStateMachine;
64
65     @MockBean
66     AcmDeployer acmDeployer;
67
68     @MockBean
69     SmeDeployer smeDeployer;
70
71     @MockBean
72     DmeDeployer dmeDeployer;
73
74     @MockBean
75     SmeLifecycleManager smeLifecycleManager;
76
77     private final String validRappFile = "valid-rapp-package.csar";
78
79     private final String validCsarFileLocation = "src/test/resources/";
80
81     private final ObjectMapper objectMapper = new ObjectMapper();
82
83     @Test
84     void testGetAllRappInstances() throws Exception {
85         UUID rappId = UUID.randomUUID();
86         UUID rappInstanceId = UUID.randomUUID();
87         Rapp rapp = getRapp(rappId, rappInstanceId);
88         rappCacheService.putRapp(rapp);
89         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance", rappId)).andExpect(status().isOk())
90                 .andExpect(
91                         jsonPath("$." + rappInstanceId.toString() + ".rappInstanceId").value(rappInstanceId.toString()))
92                 .andExpect(jsonPath("$." + rappInstanceId.toString() + ".state").value(
93                         RappInstanceState.UNDEPLOYED.name()));
94     }
95
96     @Test
97     void testGetAllRappInstancesFailure() throws Exception {
98         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance", UUID.randomUUID()))
99                 .andExpect(status().isNotFound());
100     }
101
102     @Test
103     void testCreateRappInstance() throws Exception {
104         UUID rappId = UUID.randomUUID();
105         UUID rappInstanceId = UUID.randomUUID();
106         RappInstance rappInstance = new RappInstance();
107         rappInstance.setRappInstanceId(rappInstanceId);
108         rappInstance.setState(RappInstanceState.UNDEPLOYED);
109         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
110                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
111         rappCacheService.putRapp(rapp);
112         mockMvc.perform(
113                 MockMvcRequestBuilders.post("/rapps/{rapp_id}/instance", rappId).contentType(MediaType.APPLICATION_JSON)
114                         .content(objectMapper.writeValueAsString(rappInstance))).andExpect(status().isOk());
115         Rapp rappResult = rappCacheService.getRapp(String.valueOf(rappId)).get();
116         assertNotNull(rappResult.getRappInstances().get(rappInstanceId));
117     }
118
119     @Test
120     void testCreateRappInstanceFailure() throws Exception {
121         RappInstance rappInstance = new RappInstance();
122         rappInstance.setRappInstanceId(UUID.randomUUID());
123         rappInstance.setState(RappInstanceState.UNDEPLOYED);
124         mockMvc.perform(MockMvcRequestBuilders.post("/rapps/{rapp_id}/instance", UUID.randomUUID())
125                                 .contentType(MediaType.APPLICATION_JSON)
126                                 .content(objectMapper.writeValueAsString(rappInstance)))
127                 .andExpect(status().isNotFound());
128     }
129
130     @Test
131     void testGetRappInstance() throws Exception {
132         UUID rappId = UUID.randomUUID();
133         UUID rappInstanceId = UUID.randomUUID();
134         Rapp rapp = getRapp(rappId, rappInstanceId);
135         rappCacheService.putRapp(rapp);
136         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
137         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
138                 .andExpect(status().isOk()).andExpect(jsonPath("$.rappInstanceId").value(rappInstanceId.toString()))
139                 .andExpect(jsonPath("$.state").value(RappInstanceState.UNDEPLOYED.name()));
140     }
141
142     @Test
143     void testGetRappInstanceNoRappFailure() throws Exception {
144         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", UUID.randomUUID(),
145                 UUID.randomUUID())).andExpect(status().isNotFound());
146     }
147
148     @Test
149     void testGetRappInstanceNoRappInstanceFailure() throws Exception {
150         UUID rappId = UUID.randomUUID();
151         UUID rappInstanceId = UUID.randomUUID();
152         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
153                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
154         rappCacheService.putRapp(rapp);
155         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
156                 .andExpect(status().isNotFound());
157     }
158
159     @Test
160     void testDeployRappInstance() throws Exception {
161         UUID rappId = UUID.randomUUID();
162         UUID rappInstanceId = UUID.randomUUID();
163         Rapp rapp = getRapp(rappId, rappInstanceId);
164         rappCacheService.putRapp(rapp);
165         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
166         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
167         rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
168         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
169         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
170         when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
171         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
172                                 .contentType(MediaType.APPLICATION_JSON)
173                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
174                 .andExpect(status().isAccepted());
175     }
176
177
178     @Test
179     void testDeployNoRappInstanceFailure() throws Exception {
180         UUID rappId = UUID.randomUUID();
181         UUID rappInstanceId = UUID.randomUUID();
182         Rapp rapp = getRapp(rappId, rappInstanceId);
183         rapp.setRappInstances(Map.of());
184         rappCacheService.putRapp(rapp);
185         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
186         rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
187         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
188         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
189         when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
190         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
191                                 .contentType(MediaType.APPLICATION_JSON)
192                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
193                 .andExpect(status().isNotFound());
194     }
195
196     @Test
197     void testDeployNoRappFailure() throws Exception {
198         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
199         rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
200         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
201         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
202         when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
203         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", UUID.randomUUID(),
204                                 UUID.randomUUID()).contentType(MediaType.APPLICATION_JSON)
205                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
206                 .andExpect(status().isNotFound());
207     }
208
209     @Test
210     void testUndeployRappInstance() throws Exception {
211         UUID rappId = UUID.randomUUID();
212         UUID rappInstanceId = UUID.randomUUID();
213         Rapp rapp = getRapp(rappId, rappInstanceId);
214         rapp.getRappInstances().forEach((uuid, rappInstance) -> rappInstance.setState(RappInstanceState.DEPLOYED));
215         rappCacheService.putRapp(rapp);
216         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
217         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
218         rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
219         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
220         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
221         when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
222         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
223                                 .contentType(MediaType.APPLICATION_JSON)
224                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
225                 .andExpect(status().isAccepted());
226     }
227
228     @Test
229     void testUndeployNoRappInstanceFailure() throws Exception {
230         UUID rappId = UUID.randomUUID();
231         UUID rappInstanceId = UUID.randomUUID();
232         Rapp rapp = getRapp(rappId, rappInstanceId);
233         rapp.setRappInstances(Map.of());
234         rappCacheService.putRapp(rapp);
235         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
236         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
237         rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
238         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
239         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
240         when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
241         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
242                                 .contentType(MediaType.APPLICATION_JSON)
243                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
244                 .andExpect(status().isNotFound());
245     }
246
247     @Test
248     void testUndeployNoRappFailure() throws Exception {
249         UUID rappId = UUID.randomUUID();
250         UUID rappInstanceId = UUID.randomUUID();
251         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
252         rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
253         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
254         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
255         when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
256         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
257                                 .contentType(MediaType.APPLICATION_JSON)
258                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
259                 .andExpect(status().isNotFound());
260     }
261
262     @Test
263     void testDeleteRappInstance() throws Exception {
264         UUID rappId = UUID.randomUUID();
265         UUID rappInstanceId = UUID.randomUUID();
266         Rapp rapp = getRapp(rappId, rappInstanceId);
267         rappCacheService.putRapp(rapp);
268         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
269         mockMvc.perform(
270                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
271                 .andExpect(status().isNoContent());
272     }
273
274     @Test
275     void testDeleteRappNoRappFailure() throws Exception {
276         UUID rappId = UUID.randomUUID();
277         UUID rappInstanceId = UUID.randomUUID();
278         mockMvc.perform(
279                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
280                 .andExpect(status().isNotFound());
281     }
282
283     @Test
284     void testDeleteRappNoInstanceFailure() throws Exception {
285         UUID rappId = UUID.randomUUID();
286         UUID rappInstanceId = UUID.randomUUID();
287         Rapp rapp = getRapp(rappId, rappInstanceId);
288         rapp.setRappInstances(Map.of());
289         rappCacheService.putRapp(rapp);
290         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
291         mockMvc.perform(
292                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
293                 .andExpect(status().isNotFound());
294     }
295
296     Rapp getRapp(UUID rappId, UUID rappInstanceId) {
297         RappInstance rappInstance = new RappInstance();
298         rappInstance.setRappInstanceId(rappInstanceId);
299         rappInstance.setState(RappInstanceState.UNDEPLOYED);
300         Map<UUID, RappInstance> instances = new HashMap();
301         instances.put(rappInstanceId, rappInstance);
302         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
303                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).rappInstances(instances)
304                             .build();
305         return rapp;
306     }
307
308 }