Add validation for asd descriptor and invariant id
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / test / java / com / oransc / rappmanager / rest / RappInstanceControllerTest.java
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 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.rappinstance.DeployOrder;
31 import com.oransc.rappmanager.models.rapp.Rapp;
32 import com.oransc.rappmanager.models.rappinstance.RappInstance;
33 import com.oransc.rappmanager.models.rappinstance.RappInstanceDeployOrder;
34 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
35 import com.oransc.rappmanager.models.rapp.RappState;
36 import com.oransc.rappmanager.models.cache.RappCacheService;
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 invalidRappFile = "invalid-rapp-package.csar";
80     private final String validCsarFileLocation = "src/test/resources/";
81
82     private final ObjectMapper objectMapper = new ObjectMapper();
83
84     @Test
85     void testGetAllRappInstances() throws Exception {
86         UUID rappId = UUID.randomUUID();
87         UUID rappInstanceId = UUID.randomUUID();
88         Rapp rapp = getRapp(rappId, rappInstanceId);
89         rappCacheService.putRapp(rapp);
90         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance", rappId)).andExpect(status().isOk())
91                 .andExpect(
92                         jsonPath("$." + rappInstanceId.toString() + ".rappInstanceId").value(rappInstanceId.toString()))
93                 .andExpect(jsonPath("$." + rappInstanceId.toString() + ".state").value(
94                         RappInstanceState.UNDEPLOYED.name()));
95     }
96
97     @Test
98     void testGetAllRappInstancesFailure() throws Exception {
99         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance", UUID.randomUUID()))
100                 .andExpect(status().isNotFound());
101     }
102
103     @Test
104     void testCreateRappInstance() throws Exception {
105         UUID rappId = UUID.randomUUID();
106         UUID rappInstanceId = UUID.randomUUID();
107         RappInstance rappInstance = new RappInstance();
108         rappInstance.setRappInstanceId(rappInstanceId);
109         rappInstance.setState(RappInstanceState.UNDEPLOYED);
110         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
111                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
112         rappCacheService.putRapp(rapp);
113         mockMvc.perform(
114                 MockMvcRequestBuilders.post("/rapps/{rapp_id}/instance", rappId).contentType(MediaType.APPLICATION_JSON)
115                         .content(objectMapper.writeValueAsString(rappInstance))).andExpect(status().isOk());
116         Rapp rappResult = rappCacheService.getRapp(String.valueOf(rappId)).get();
117         assertNotNull(rappResult.getRappInstances().get(rappInstanceId));
118     }
119
120     @Test
121     void testCreateRappInstanceFailure() throws Exception {
122         RappInstance rappInstance = new RappInstance();
123         rappInstance.setRappInstanceId(UUID.randomUUID());
124         rappInstance.setState(RappInstanceState.UNDEPLOYED);
125         mockMvc.perform(MockMvcRequestBuilders.post("/rapps/{rapp_id}/instance", UUID.randomUUID())
126                                 .contentType(MediaType.APPLICATION_JSON)
127                                 .content(objectMapper.writeValueAsString(rappInstance)))
128                 .andExpect(status().isNotFound());
129     }
130
131     @Test
132     void testGetRappInstance() throws Exception {
133         UUID rappId = UUID.randomUUID();
134         UUID rappInstanceId = UUID.randomUUID();
135         Rapp rapp = getRapp(rappId, rappInstanceId);
136         rappCacheService.putRapp(rapp);
137         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
138         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
139                 .andExpect(status().isOk()).andExpect(jsonPath("$.rappInstanceId").value(rappInstanceId.toString()))
140                 .andExpect(jsonPath("$.state").value(RappInstanceState.UNDEPLOYED.name()));
141     }
142
143     @Test
144     void testGetRappInstanceNoRappFailure() throws Exception {
145         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", UUID.randomUUID(),
146                 UUID.randomUUID())).andExpect(status().isNotFound());
147     }
148
149     @Test
150     void testGetRappInstanceNoRappInstanceFailure() throws Exception {
151         UUID rappId = UUID.randomUUID();
152         UUID rappInstanceId = UUID.randomUUID();
153         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
154                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
155         rappCacheService.putRapp(rapp);
156         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
157                 .andExpect(status().isNotFound());
158     }
159
160     @Test
161     void testDeployRappInstance() throws Exception {
162         UUID rappId = UUID.randomUUID();
163         UUID rappInstanceId = UUID.randomUUID();
164         Rapp rapp = getRapp(rappId, rappInstanceId);
165         rappCacheService.putRapp(rapp);
166         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
167         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
168         rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
169         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
170         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
171         when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
172         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
173                                 .contentType(MediaType.APPLICATION_JSON)
174                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
175                 .andExpect(status().isAccepted());
176     }
177
178
179     @Test
180     void testDeployNoRappInstanceFailure() throws Exception {
181         UUID rappId = UUID.randomUUID();
182         UUID rappInstanceId = UUID.randomUUID();
183         Rapp rapp = getRapp(rappId, rappInstanceId);
184         rapp.setRappInstances(Map.of());
185         rappCacheService.putRapp(rapp);
186         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
187         rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
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         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
192                                 .contentType(MediaType.APPLICATION_JSON)
193                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
194                 .andExpect(status().isNotFound());
195     }
196
197     @Test
198     void testDeployNoRappFailure() throws Exception {
199         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
200         rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
201         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
202         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
203         when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
204         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", UUID.randomUUID(),
205                                 UUID.randomUUID()).contentType(MediaType.APPLICATION_JSON)
206                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
207                 .andExpect(status().isNotFound());
208     }
209
210     @Test
211     void testUndeployRappInstance() throws Exception {
212         UUID rappId = UUID.randomUUID();
213         UUID rappInstanceId = UUID.randomUUID();
214         Rapp rapp = getRapp(rappId, rappInstanceId);
215         rapp.getRappInstances().forEach((uuid, rappInstance) -> rappInstance.setState(RappInstanceState.DEPLOYED));
216         rappCacheService.putRapp(rapp);
217         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
218         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
219         rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
220         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
221         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
222         when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
223         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
224                                 .contentType(MediaType.APPLICATION_JSON)
225                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
226                 .andExpect(status().isAccepted());
227     }
228
229     @Test
230     void testUndeployNoRappInstanceFailure() throws Exception {
231         UUID rappId = UUID.randomUUID();
232         UUID rappInstanceId = UUID.randomUUID();
233         Rapp rapp = getRapp(rappId, rappInstanceId);
234         rapp.setRappInstances(Map.of());
235         rappCacheService.putRapp(rapp);
236         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
237         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
238         rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
239         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
240         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
241         when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
242         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
243                                 .contentType(MediaType.APPLICATION_JSON)
244                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
245                 .andExpect(status().isNotFound());
246     }
247
248     @Test
249     void testUndeployNoRappFailure() throws Exception {
250         UUID rappId = UUID.randomUUID();
251         UUID rappInstanceId = UUID.randomUUID();
252         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
253         rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
254         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
255         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
256         when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
257         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
258                                 .contentType(MediaType.APPLICATION_JSON)
259                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
260                 .andExpect(status().isNotFound());
261     }
262
263     @Test
264     void testDeleteRappInstance() throws Exception {
265         UUID rappId = UUID.randomUUID();
266         UUID rappInstanceId = UUID.randomUUID();
267         Rapp rapp = getRapp(rappId, rappInstanceId);
268         rappCacheService.putRapp(rapp);
269         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
270         mockMvc.perform(
271                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
272                 .andExpect(status().isNoContent());
273     }
274
275     @Test
276     void testDeleteRappNoRappFailure() throws Exception {
277         UUID rappId = UUID.randomUUID();
278         UUID rappInstanceId = UUID.randomUUID();
279         mockMvc.perform(
280                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
281                 .andExpect(status().isNotFound());
282     }
283
284     @Test
285     void testDeleteRappNoInstanceFailure() throws Exception {
286         UUID rappId = UUID.randomUUID();
287         UUID rappInstanceId = UUID.randomUUID();
288         Rapp rapp = getRapp(rappId, rappInstanceId);
289         rapp.setRappInstances(Map.of());
290         rappCacheService.putRapp(rapp);
291         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
292         mockMvc.perform(
293                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
294                 .andExpect(status().isNotFound());
295     }
296
297     Rapp getRapp(UUID rappId, UUID rappInstanceId) {
298         RappInstance rappInstance = new RappInstance();
299         rappInstance.setRappInstanceId(rappInstanceId);
300         rappInstance.setState(RappInstanceState.UNDEPLOYED);
301         Map<UUID, RappInstance> instances = new HashMap();
302         instances.put(rappInstanceId, rappInstance);
303         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
304                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).rappInstances(instances)
305                             .build();
306         return rapp;
307     }
308
309 }