Add Rapp Instances
[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.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 java.util.HashMap;
21 import java.util.Map;
22 import java.util.UUID;
23 import org.junit.jupiter.api.Test;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
26 import org.springframework.boot.test.context.SpringBootTest;
27 import org.springframework.boot.test.mock.mockito.MockBean;
28 import org.springframework.http.MediaType;
29 import org.springframework.test.web.servlet.MockMvc;
30 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
31
32 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
33 @AutoConfigureMockMvc
34 public class RappInstanceControllerTest {
35
36     @Autowired
37     private MockMvc mockMvc;
38
39     @Autowired
40     private RappCacheService rappCacheService;
41
42     @Autowired
43     private RappInstanceStateMachine rappInstanceStateMachine;
44
45     @MockBean
46     AcmDeployer acmDeployer;
47
48     @MockBean
49     SmeDeployer smeDeployer;
50
51     private final String validRappFile = "valid-rapp-package.csar";
52
53     private final String invalidRappFile = "invalid-rapp-package.csar";
54     private final String validCsarFileLocation = "src/test/resources/";
55
56     private final ObjectMapper objectMapper = new ObjectMapper();
57
58     @Test
59     void testGetAllRappInstances() throws Exception {
60         UUID rappId = UUID.randomUUID();
61         UUID rappInstanceId = UUID.randomUUID();
62         Rapp rapp = getRapp(rappId, rappInstanceId);
63         rappCacheService.putRapp(rapp);
64         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance", rappId)).andExpect(status().isOk())
65                 .andExpect(
66                         jsonPath("$." + rappInstanceId.toString() + ".rappInstanceId").value(rappInstanceId.toString()))
67                 .andExpect(jsonPath("$." + rappInstanceId.toString() + ".state").value(
68                         RappInstanceState.UNDEPLOYED.name()));
69     }
70
71     @Test
72     void testGetAllRappInstancesFailure() throws Exception {
73         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance", UUID.randomUUID()))
74                 .andExpect(status().isNotFound());
75     }
76
77     @Test
78     void testCreateRappInstance() throws Exception {
79         UUID rappId = UUID.randomUUID();
80         UUID rappInstanceId = UUID.randomUUID();
81         RappInstance rappInstance = new RappInstance();
82         rappInstance.setRappInstanceId(rappInstanceId);
83         rappInstance.setState(RappInstanceState.UNDEPLOYED);
84         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
85                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
86         rappCacheService.putRapp(rapp);
87         mockMvc.perform(
88                 MockMvcRequestBuilders.post("/rapps/{rapp_id}/instance", rappId).contentType(MediaType.APPLICATION_JSON)
89                         .content(objectMapper.writeValueAsString(rappInstance))).andExpect(status().isOk());
90         Rapp rappResult = rappCacheService.getRapp(String.valueOf(rappId)).get();
91         assertNotNull(rappResult.getRappInstances().get(rappInstanceId));
92     }
93
94     @Test
95     void testCreateRappInstanceFailure() throws Exception {
96         RappInstance rappInstance = new RappInstance();
97         rappInstance.setRappInstanceId(UUID.randomUUID());
98         rappInstance.setState(RappInstanceState.UNDEPLOYED);
99         mockMvc.perform(MockMvcRequestBuilders.post("/rapps/{rapp_id}/instance", UUID.randomUUID())
100                                 .contentType(MediaType.APPLICATION_JSON)
101                                 .content(objectMapper.writeValueAsString(rappInstance)))
102                 .andExpect(status().isNotFound());
103     }
104
105     @Test
106     void testGetRappInstance() throws Exception {
107         UUID rappId = UUID.randomUUID();
108         UUID rappInstanceId = UUID.randomUUID();
109         Rapp rapp = getRapp(rappId, rappInstanceId);
110         rappCacheService.putRapp(rapp);
111         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
112         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
113                 .andExpect(status().isOk()).andExpect(jsonPath("$.rappInstanceId").value(rappInstanceId.toString()))
114                 .andExpect(jsonPath("$.state").value(RappInstanceState.UNDEPLOYED.name()));
115     }
116
117     @Test
118     void testGetRappInstanceNoRappFailure() throws Exception {
119         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", UUID.randomUUID(),
120                 UUID.randomUUID())).andExpect(status().isNotFound());
121     }
122
123     @Test
124     void testGetRappInstanceNoRappInstanceFailure() throws Exception {
125         UUID rappId = UUID.randomUUID();
126         UUID rappInstanceId = UUID.randomUUID();
127         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
128                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
129         rappCacheService.putRapp(rapp);
130         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
131                 .andExpect(status().isNotFound());
132     }
133
134     @Test
135     void testDeployRappInstance() throws Exception {
136         UUID rappId = UUID.randomUUID();
137         UUID rappInstanceId = UUID.randomUUID();
138         Rapp rapp = getRapp(rappId, rappInstanceId);
139         rappCacheService.putRapp(rapp);
140         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
141         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
142         rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
143         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
144         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
145         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
146                                 .contentType(MediaType.APPLICATION_JSON)
147                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
148                 .andExpect(status().isAccepted());
149     }
150
151
152     @Test
153     void testDeployNoRappInstanceFailure() throws Exception {
154         UUID rappId = UUID.randomUUID();
155         UUID rappInstanceId = UUID.randomUUID();
156         Rapp rapp = getRapp(rappId, rappInstanceId);
157         rapp.setRappInstances(Map.of());
158         rappCacheService.putRapp(rapp);
159         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
160         rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
161         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
162         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
163         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
164                                 .contentType(MediaType.APPLICATION_JSON)
165                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
166                 .andExpect(status().isNotFound());
167     }
168
169     @Test
170     void testDeployNoRappFailure() throws Exception {
171         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
172         rappInstanceDeployOrder.setDeployOrder(DeployOrder.DEPLOY);
173         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
174         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
175         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", UUID.randomUUID(),
176                                 UUID.randomUUID()).contentType(MediaType.APPLICATION_JSON)
177                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
178                 .andExpect(status().isNotFound());
179     }
180
181     @Test
182     void testUndeployRappInstance() throws Exception {
183         UUID rappId = UUID.randomUUID();
184         UUID rappInstanceId = UUID.randomUUID();
185         Rapp rapp = getRapp(rappId, rappInstanceId);
186         rapp.getRappInstances().forEach((uuid, rappInstance) -> rappInstance.setState(RappInstanceState.DEPLOYED));
187         rappCacheService.putRapp(rapp);
188         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
189         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
190         rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
191         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
192         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
193         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
194                                 .contentType(MediaType.APPLICATION_JSON)
195                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
196                 .andExpect(status().isAccepted());
197     }
198
199     @Test
200     void testUndeployNoRappInstanceFailure() throws Exception {
201         UUID rappId = UUID.randomUUID();
202         UUID rappInstanceId = UUID.randomUUID();
203         Rapp rapp = getRapp(rappId, rappInstanceId);
204         rapp.setRappInstances(Map.of());
205         rappCacheService.putRapp(rapp);
206         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
207         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
208         rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
209         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
210         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
211         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
212                                 .contentType(MediaType.APPLICATION_JSON)
213                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
214                 .andExpect(status().isNotFound());
215     }
216
217     @Test
218     void testUndeployNoRappFailure() throws Exception {
219         UUID rappId = UUID.randomUUID();
220         UUID rappInstanceId = UUID.randomUUID();
221         RappInstanceDeployOrder rappInstanceDeployOrder = new RappInstanceDeployOrder();
222         rappInstanceDeployOrder.setDeployOrder(DeployOrder.UNDEPLOY);
223         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
224         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
225         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId)
226                                 .contentType(MediaType.APPLICATION_JSON)
227                                 .content(objectMapper.writeValueAsString(rappInstanceDeployOrder)))
228                 .andExpect(status().isNotFound());
229     }
230
231     @Test
232     void testDeleteRappInstance() throws Exception {
233         UUID rappId = UUID.randomUUID();
234         UUID rappInstanceId = UUID.randomUUID();
235         Rapp rapp = getRapp(rappId, rappInstanceId);
236         rappCacheService.putRapp(rapp);
237         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
238         mockMvc.perform(
239                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
240                 .andExpect(status().isNoContent());
241     }
242
243     @Test
244     void testDeleteRappNoRappFailure() throws Exception {
245         UUID rappId = UUID.randomUUID();
246         UUID rappInstanceId = UUID.randomUUID();
247         mockMvc.perform(
248                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
249                 .andExpect(status().isNotFound());
250     }
251
252     @Test
253     void testDeleteRappNoInstanceFailure() throws Exception {
254         UUID rappId = UUID.randomUUID();
255         UUID rappInstanceId = UUID.randomUUID();
256         Rapp rapp = getRapp(rappId, rappInstanceId);
257         rapp.setRappInstances(Map.of());
258         rappCacheService.putRapp(rapp);
259         rappInstanceStateMachine.onboardRappInstance(rappInstanceId);
260         mockMvc.perform(
261                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
262                 .andExpect(status().isNotFound());
263     }
264
265     Rapp getRapp(UUID rappId, UUID rappInstanceId) {
266         RappInstance rappInstance = new RappInstance();
267         rappInstance.setRappInstanceId(rappInstanceId);
268         rappInstance.setState(RappInstanceState.UNDEPLOYED);
269         Map<UUID, RappInstance> instances = new HashMap();
270         instances.put(rappInstanceId, rappInstance);
271         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
272                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).rappInstances(instances)
273                             .build();
274         return rapp;
275     }
276
277 }