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
9 * http://www.apache.org/licenses/LICENSE-2.0
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========================================================================
19 package com.oransc.rappmanager.rest;
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;
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;
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;
52 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
54 class RappInstanceControllerTest {
57 private MockMvc mockMvc;
60 private RappCacheService rappCacheService;
63 private RappInstanceStateMachine rappInstanceStateMachine;
66 AcmDeployer acmDeployer;
69 SmeDeployer smeDeployer;
72 DmeDeployer dmeDeployer;
75 SmeLifecycleManager smeLifecycleManager;
77 private final String validRappFile = "valid-rapp-package.csar";
79 private final String validCsarFileLocation = "src/test/resources/";
81 private final ObjectMapper objectMapper = new ObjectMapper();
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())
91 jsonPath("$." + rappInstanceId.toString() + ".rappInstanceId").value(rappInstanceId.toString()))
92 .andExpect(jsonPath("$." + rappInstanceId.toString() + ".state").value(
93 RappInstanceState.UNDEPLOYED.name()));
97 void testGetAllRappInstancesFailure() throws Exception {
98 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance", UUID.randomUUID()))
99 .andExpect(status().isNotFound());
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);
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));
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());
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()));
143 void testGetRappInstanceNoRappFailure() throws Exception {
144 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", UUID.randomUUID(),
145 UUID.randomUUID())).andExpect(status().isNotFound());
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());
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());
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());
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());
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());
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());
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());
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);
270 MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
271 .andExpect(status().isNoContent());
275 void testDeleteRappNoRappFailure() throws Exception {
276 UUID rappId = UUID.randomUUID();
277 UUID rappInstanceId = UUID.randomUUID();
279 MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
280 .andExpect(status().isNotFound());
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);
292 MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
293 .andExpect(status().isNotFound());
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)