1 package com.oransc.rappmanager.rest;
3 import static org.junit.jupiter.api.Assertions.assertNotNull;
4 import static org.mockito.ArgumentMatchers.any;
5 import static org.mockito.Mockito.when;
6 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
7 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
9 import com.fasterxml.jackson.databind.ObjectMapper;
10 import com.oransc.rappmanager.acm.service.AcmDeployer;
11 import com.oransc.rappmanager.models.rappinstance.DeployOrder;
12 import com.oransc.rappmanager.models.rapp.Rapp;
13 import com.oransc.rappmanager.models.rappinstance.RappInstance;
14 import com.oransc.rappmanager.models.rappinstance.RappInstanceDeployOrder;
15 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
16 import com.oransc.rappmanager.models.rapp.RappState;
17 import com.oransc.rappmanager.models.cache.RappCacheService;
18 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
19 import com.oransc.rappmanager.sme.service.SmeDeployer;
20 import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
21 import java.util.HashMap;
23 import java.util.UUID;
24 import org.junit.jupiter.api.Test;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
27 import org.springframework.boot.test.context.SpringBootTest;
28 import org.springframework.boot.test.mock.mockito.MockBean;
29 import org.springframework.http.MediaType;
30 import org.springframework.test.web.servlet.MockMvc;
31 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
33 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
35 public class RappInstanceControllerTest {
38 private MockMvc mockMvc;
41 private RappCacheService rappCacheService;
44 private RappInstanceStateMachine rappInstanceStateMachine;
47 AcmDeployer acmDeployer;
50 SmeDeployer smeDeployer;
53 SmeLifecycleManager smeLifecycleManager;
55 private final String validRappFile = "valid-rapp-package.csar";
57 private final String invalidRappFile = "invalid-rapp-package.csar";
58 private final String validCsarFileLocation = "src/test/resources/";
60 private final ObjectMapper objectMapper = new ObjectMapper();
63 void testGetAllRappInstances() throws Exception {
64 UUID rappId = UUID.randomUUID();
65 UUID rappInstanceId = UUID.randomUUID();
66 Rapp rapp = getRapp(rappId, rappInstanceId);
67 rappCacheService.putRapp(rapp);
68 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance", rappId)).andExpect(status().isOk())
70 jsonPath("$." + rappInstanceId.toString() + ".rappInstanceId").value(rappInstanceId.toString()))
71 .andExpect(jsonPath("$." + rappInstanceId.toString() + ".state").value(
72 RappInstanceState.UNDEPLOYED.name()));
76 void testGetAllRappInstancesFailure() throws Exception {
77 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance", UUID.randomUUID()))
78 .andExpect(status().isNotFound());
82 void testCreateRappInstance() throws Exception {
83 UUID rappId = UUID.randomUUID();
84 UUID rappInstanceId = UUID.randomUUID();
85 RappInstance rappInstance = new RappInstance();
86 rappInstance.setRappInstanceId(rappInstanceId);
87 rappInstance.setState(RappInstanceState.UNDEPLOYED);
88 Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
89 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
90 rappCacheService.putRapp(rapp);
92 MockMvcRequestBuilders.post("/rapps/{rapp_id}/instance", rappId).contentType(MediaType.APPLICATION_JSON)
93 .content(objectMapper.writeValueAsString(rappInstance))).andExpect(status().isOk());
94 Rapp rappResult = rappCacheService.getRapp(String.valueOf(rappId)).get();
95 assertNotNull(rappResult.getRappInstances().get(rappInstanceId));
99 void testCreateRappInstanceFailure() throws Exception {
100 RappInstance rappInstance = new RappInstance();
101 rappInstance.setRappInstanceId(UUID.randomUUID());
102 rappInstance.setState(RappInstanceState.UNDEPLOYED);
103 mockMvc.perform(MockMvcRequestBuilders.post("/rapps/{rapp_id}/instance", UUID.randomUUID())
104 .contentType(MediaType.APPLICATION_JSON)
105 .content(objectMapper.writeValueAsString(rappInstance)))
106 .andExpect(status().isNotFound());
110 void testGetRappInstance() throws Exception {
111 UUID rappId = UUID.randomUUID();
112 UUID rappInstanceId = UUID.randomUUID();
113 Rapp rapp = getRapp(rappId, rappInstanceId);
114 rappCacheService.putRapp(rapp);
115 rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
116 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
117 .andExpect(status().isOk()).andExpect(jsonPath("$.rappInstanceId").value(rappInstanceId.toString()))
118 .andExpect(jsonPath("$.state").value(RappInstanceState.UNDEPLOYED.name()));
122 void testGetRappInstanceNoRappFailure() throws Exception {
123 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", UUID.randomUUID(),
124 UUID.randomUUID())).andExpect(status().isNotFound());
128 void testGetRappInstanceNoRappInstanceFailure() throws Exception {
129 UUID rappId = UUID.randomUUID();
130 UUID rappInstanceId = UUID.randomUUID();
131 Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
132 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
133 rappCacheService.putRapp(rapp);
134 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
135 .andExpect(status().isNotFound());
139 void testDeployRappInstance() throws Exception {
140 UUID rappId = UUID.randomUUID();
141 UUID rappInstanceId = UUID.randomUUID();
142 Rapp rapp = getRapp(rappId, rappInstanceId);
143 rappCacheService.putRapp(rapp);
144 rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
145 RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
146 rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
147 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
148 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
149 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
150 .contentType(MediaType.APPLICATION_JSON)
151 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
152 .andExpect(status().isAccepted());
157 void testDeployNoRappInstanceFailure() throws Exception {
158 UUID rappId = UUID.randomUUID();
159 UUID rappInstanceId = UUID.randomUUID();
160 Rapp rapp = getRapp(rappId, rappInstanceId);
161 rapp.setRappInstances(Map.of());
162 rappCacheService.putRapp(rapp);
163 RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
164 rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
165 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
166 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
167 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
168 .contentType(MediaType.APPLICATION_JSON)
169 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
170 .andExpect(status().isNotFound());
174 void testDeployNoRappFailure() throws Exception {
175 RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
176 rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
177 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
178 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
179 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", UUID.randomUUID(),
180 UUID.randomUUID()).contentType(MediaType.APPLICATION_JSON)
181 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
182 .andExpect(status().isNotFound());
186 void testUndeployRappInstance() throws Exception {
187 UUID rappId = UUID.randomUUID();
188 UUID rappInstanceId = UUID.randomUUID();
189 Rapp rapp = getRapp(rappId, rappInstanceId);
190 rapp.getRappInstances().forEach((uuid, rappInstance) -> rappInstance.setState(RappInstanceState.DEPLOYED));
191 rappCacheService.putRapp(rapp);
192 rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
193 RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
194 rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
195 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
196 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
197 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
198 .contentType(MediaType.APPLICATION_JSON)
199 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
200 .andExpect(status().isAccepted());
204 void testUndeployNoRappInstanceFailure() throws Exception {
205 UUID rappId = UUID.randomUUID();
206 UUID rappInstanceId = UUID.randomUUID();
207 Rapp rapp = getRapp(rappId, rappInstanceId);
208 rapp.setRappInstances(Map.of());
209 rappCacheService.putRapp(rapp);
210 rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
211 RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
212 rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
213 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
214 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
215 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
216 .contentType(MediaType.APPLICATION_JSON)
217 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
218 .andExpect(status().isNotFound());
222 void testUndeployNoRappFailure() throws Exception {
223 UUID rappId = UUID.randomUUID();
224 UUID rappInstanceId = UUID.randomUUID();
225 RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
226 rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
227 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
228 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
229 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
230 .contentType(MediaType.APPLICATION_JSON)
231 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
232 .andExpect(status().isNotFound());
236 void testDeleteRappInstance() throws Exception {
237 UUID rappId = UUID.randomUUID();
238 UUID rappInstanceId = UUID.randomUUID();
239 Rapp rapp = getRapp(rappId, rappInstanceId);
240 rappCacheService.putRapp(rapp);
241 rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
243 MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
244 .andExpect(status().isNoContent());
248 void testDeleteRappNoRappFailure() throws Exception {
249 UUID rappId = UUID.randomUUID();
250 UUID rappInstanceId = UUID.randomUUID();
252 MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
253 .andExpect(status().isNotFound());
257 void testDeleteRappNoInstanceFailure() throws Exception {
258 UUID rappId = UUID.randomUUID();
259 UUID rappInstanceId = UUID.randomUUID();
260 Rapp rapp = getRapp(rappId, rappInstanceId);
261 rapp.setRappInstances(Map.of());
262 rappCacheService.putRapp(rapp);
263 rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
265 MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
266 .andExpect(status().isNotFound());
269 Rapp getRapp(UUID rappId, UUID rappInstanceId) {
270 RappInstance rappInstance = new RappInstance();
271 rappInstance.setRappInstanceId(rappInstanceId);
272 rappInstance.setState(RappInstanceState.UNDEPLOYED);
273 Map<UUID, RappInstance> instances = new HashMap();
274 instances.put(rappInstanceId, rappInstance);
275 Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
276 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).rappInstances(instances)