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.dme.service.DmeDeployer;
12 import com.oransc.rappmanager.models.rappinstance.DeployOrder;
13 import com.oransc.rappmanager.models.rapp.Rapp;
14 import com.oransc.rappmanager.models.rappinstance.RappInstance;
15 import com.oransc.rappmanager.models.rappinstance.RappInstanceDeployOrder;
16 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
17 import com.oransc.rappmanager.models.rapp.RappState;
18 import com.oransc.rappmanager.models.cache.RappCacheService;
19 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
20 import com.oransc.rappmanager.sme.service.SmeDeployer;
21 import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
22 import java.util.HashMap;
24 import java.util.UUID;
25 import org.junit.jupiter.api.Test;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
28 import org.springframework.boot.test.context.SpringBootTest;
29 import org.springframework.boot.test.mock.mockito.MockBean;
30 import org.springframework.http.MediaType;
31 import org.springframework.test.web.servlet.MockMvc;
32 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
34 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
36 class RappInstanceControllerTest {
39 private MockMvc mockMvc;
42 private RappCacheService rappCacheService;
45 private RappInstanceStateMachine rappInstanceStateMachine;
48 AcmDeployer acmDeployer;
51 SmeDeployer smeDeployer;
54 DmeDeployer dmeDeployer;
57 SmeLifecycleManager smeLifecycleManager;
59 private final String validRappFile = "valid-rapp-package.csar";
61 private final String invalidRappFile = "invalid-rapp-package.csar";
62 private final String validCsarFileLocation = "src/test/resources/";
64 private final ObjectMapper objectMapper = new ObjectMapper();
67 void testGetAllRappInstances() throws Exception {
68 UUID rappId = UUID.randomUUID();
69 UUID rappInstanceId = UUID.randomUUID();
70 Rapp rapp = getRapp(rappId, rappInstanceId);
71 rappCacheService.putRapp(rapp);
72 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance", rappId)).andExpect(status().isOk())
74 jsonPath("$." + rappInstanceId.toString() + ".rappInstanceId").value(rappInstanceId.toString()))
75 .andExpect(jsonPath("$." + rappInstanceId.toString() + ".state").value(
76 RappInstanceState.UNDEPLOYED.name()));
80 void testGetAllRappInstancesFailure() throws Exception {
81 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance", UUID.randomUUID()))
82 .andExpect(status().isNotFound());
86 void testCreateRappInstance() throws Exception {
87 UUID rappId = UUID.randomUUID();
88 UUID rappInstanceId = UUID.randomUUID();
89 RappInstance rappInstance = new RappInstance();
90 rappInstance.setRappInstanceId(rappInstanceId);
91 rappInstance.setState(RappInstanceState.UNDEPLOYED);
92 Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
93 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
94 rappCacheService.putRapp(rapp);
96 MockMvcRequestBuilders.post("/rapps/{rapp_id}/instance", rappId).contentType(MediaType.APPLICATION_JSON)
97 .content(objectMapper.writeValueAsString(rappInstance))).andExpect(status().isOk());
98 Rapp rappResult = rappCacheService.getRapp(String.valueOf(rappId)).get();
99 assertNotNull(rappResult.getRappInstances().get(rappInstanceId));
103 void testCreateRappInstanceFailure() throws Exception {
104 RappInstance rappInstance = new RappInstance();
105 rappInstance.setRappInstanceId(UUID.randomUUID());
106 rappInstance.setState(RappInstanceState.UNDEPLOYED);
107 mockMvc.perform(MockMvcRequestBuilders.post("/rapps/{rapp_id}/instance", UUID.randomUUID())
108 .contentType(MediaType.APPLICATION_JSON)
109 .content(objectMapper.writeValueAsString(rappInstance)))
110 .andExpect(status().isNotFound());
114 void testGetRappInstance() throws Exception {
115 UUID rappId = UUID.randomUUID();
116 UUID rappInstanceId = UUID.randomUUID();
117 Rapp rapp = getRapp(rappId, rappInstanceId);
118 rappCacheService.putRapp(rapp);
119 rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
120 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
121 .andExpect(status().isOk()).andExpect(jsonPath("$.rappInstanceId").value(rappInstanceId.toString()))
122 .andExpect(jsonPath("$.state").value(RappInstanceState.UNDEPLOYED.name()));
126 void testGetRappInstanceNoRappFailure() throws Exception {
127 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", UUID.randomUUID(),
128 UUID.randomUUID())).andExpect(status().isNotFound());
132 void testGetRappInstanceNoRappInstanceFailure() throws Exception {
133 UUID rappId = UUID.randomUUID();
134 UUID rappInstanceId = UUID.randomUUID();
135 Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
136 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
137 rappCacheService.putRapp(rapp);
138 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
139 .andExpect(status().isNotFound());
143 void testDeployRappInstance() throws Exception {
144 UUID rappId = UUID.randomUUID();
145 UUID rappInstanceId = UUID.randomUUID();
146 Rapp rapp = getRapp(rappId, rappInstanceId);
147 rappCacheService.putRapp(rapp);
148 rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
149 RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
150 rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
151 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
152 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
153 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
154 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
155 .contentType(MediaType.APPLICATION_JSON)
156 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
157 .andExpect(status().isAccepted());
162 void testDeployNoRappInstanceFailure() throws Exception {
163 UUID rappId = UUID.randomUUID();
164 UUID rappInstanceId = UUID.randomUUID();
165 Rapp rapp = getRapp(rappId, rappInstanceId);
166 rapp.setRappInstances(Map.of());
167 rappCacheService.putRapp(rapp);
168 RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
169 rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
170 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
171 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
172 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
173 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
174 .contentType(MediaType.APPLICATION_JSON)
175 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
176 .andExpect(status().isNotFound());
180 void testDeployNoRappFailure() throws Exception {
181 RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
182 rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
183 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
184 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
185 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
186 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", UUID.randomUUID(),
187 UUID.randomUUID()).contentType(MediaType.APPLICATION_JSON)
188 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
189 .andExpect(status().isNotFound());
193 void testUndeployRappInstance() throws Exception {
194 UUID rappId = UUID.randomUUID();
195 UUID rappInstanceId = UUID.randomUUID();
196 Rapp rapp = getRapp(rappId, rappInstanceId);
197 rapp.getRappInstances().forEach((uuid, rappInstance) -> rappInstance.setState(RappInstanceState.DEPLOYED));
198 rappCacheService.putRapp(rapp);
199 rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
200 RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
201 rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
202 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
203 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
204 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
205 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
206 .contentType(MediaType.APPLICATION_JSON)
207 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
208 .andExpect(status().isAccepted());
212 void testUndeployNoRappInstanceFailure() throws Exception {
213 UUID rappId = UUID.randomUUID();
214 UUID rappInstanceId = UUID.randomUUID();
215 Rapp rapp = getRapp(rappId, rappInstanceId);
216 rapp.setRappInstances(Map.of());
217 rappCacheService.putRapp(rapp);
218 rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
219 RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
220 rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
221 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
222 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
223 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
224 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
225 .contentType(MediaType.APPLICATION_JSON)
226 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
227 .andExpect(status().isNotFound());
231 void testUndeployNoRappFailure() throws Exception {
232 UUID rappId = UUID.randomUUID();
233 UUID rappInstanceId = UUID.randomUUID();
234 RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
235 rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
236 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
237 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
238 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
239 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
240 .contentType(MediaType.APPLICATION_JSON)
241 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
242 .andExpect(status().isNotFound());
246 void testDeleteRappInstance() throws Exception {
247 UUID rappId = UUID.randomUUID();
248 UUID rappInstanceId = UUID.randomUUID();
249 Rapp rapp = getRapp(rappId, rappInstanceId);
250 rappCacheService.putRapp(rapp);
251 rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
253 MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
254 .andExpect(status().isNoContent());
258 void testDeleteRappNoRappFailure() throws Exception {
259 UUID rappId = UUID.randomUUID();
260 UUID rappInstanceId = UUID.randomUUID();
262 MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
263 .andExpect(status().isNotFound());
267 void testDeleteRappNoInstanceFailure() throws Exception {
268 UUID rappId = UUID.randomUUID();
269 UUID rappInstanceId = UUID.randomUUID();
270 Rapp rapp = getRapp(rappId, rappInstanceId);
271 rapp.setRappInstances(Map.of());
272 rappCacheService.putRapp(rapp);
273 rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
275 MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
276 .andExpect(status().isNotFound());
279 Rapp getRapp(UUID rappId, UUID rappInstanceId) {
280 RappInstance rappInstance = new RappInstance();
281 rappInstance.setRappInstanceId(rappInstanceId);
282 rappInstance.setState(RappInstanceState.UNDEPLOYED);
283 Map<UUID, RappInstance> instances = new HashMap();
284 instances.put(rappInstanceId, rappInstance);
285 Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
286 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).rappInstances(instances)