Update flow and architecture images
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / test / java / com / oransc / rappmanager / rest / RappInstanceControllerTest.java
1 package com.oransc.rappmanager.rest;
2
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;
8
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;
23 import java.util.Map;
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;
33
34 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
35 @AutoConfigureMockMvc
36 class RappInstanceControllerTest {
37
38     @Autowired
39     private MockMvc mockMvc;
40
41     @Autowired
42     private RappCacheService rappCacheService;
43
44     @Autowired
45     private RappInstanceStateMachine rappInstanceStateMachine;
46
47     @MockBean
48     AcmDeployer acmDeployer;
49
50     @MockBean
51     SmeDeployer smeDeployer;
52
53     @MockBean
54     DmeDeployer dmeDeployer;
55
56     @MockBean
57     SmeLifecycleManager smeLifecycleManager;
58
59     private final String validRappFile = "valid-rapp-package.csar";
60
61     private final String invalidRappFile = "invalid-rapp-package.csar";
62     private final String validCsarFileLocation = "src/test/resources/";
63
64     private final ObjectMapper objectMapper = new ObjectMapper();
65
66     @Test
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())
73                 .andExpect(
74                         jsonPath("$." + rappInstanceId.toString() + ".rappInstanceId").value(rappInstanceId.toString()))
75                 .andExpect(jsonPath("$." + rappInstanceId.toString() + ".state").value(
76                         RappInstanceState.UNDEPLOYED.name()));
77     }
78
79     @Test
80     void testGetAllRappInstancesFailure() throws Exception {
81         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance", UUID.randomUUID()))
82                 .andExpect(status().isNotFound());
83     }
84
85     @Test
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);
95         mockMvc.perform(
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));
100     }
101
102     @Test
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());
111     }
112
113     @Test
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()));
123     }
124
125     @Test
126     void testGetRappInstanceNoRappFailure() throws Exception {
127         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", UUID.randomUUID(),
128                 UUID.randomUUID())).andExpect(status().isNotFound());
129     }
130
131     @Test
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());
140     }
141
142     @Test
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());
158     }
159
160
161     @Test
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());
177     }
178
179     @Test
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());
190     }
191
192     @Test
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());
209     }
210
211     @Test
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());
228     }
229
230     @Test
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());
243     }
244
245     @Test
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);
252         mockMvc.perform(
253                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
254                 .andExpect(status().isNoContent());
255     }
256
257     @Test
258     void testDeleteRappNoRappFailure() throws Exception {
259         UUID rappId = UUID.randomUUID();
260         UUID rappInstanceId = UUID.randomUUID();
261         mockMvc.perform(
262                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
263                 .andExpect(status().isNotFound());
264     }
265
266     @Test
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);
274         mockMvc.perform(
275                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
276                 .andExpect(status().isNotFound());
277     }
278
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)
287                             .build();
288         return rapp;
289     }
290
291 }