Merge "Add repository release details for ACM"
[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 com.oransc.rappmanager.sme.service.SmeLifecycleManager;
21 import java.util.HashMap;
22 import java.util.Map;
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;
32
33 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
34 @AutoConfigureMockMvc
35 public class RappInstanceControllerTest {
36
37     @Autowired
38     private MockMvc mockMvc;
39
40     @Autowired
41     private RappCacheService rappCacheService;
42
43     @Autowired
44     private RappInstanceStateMachine rappInstanceStateMachine;
45
46     @MockBean
47     AcmDeployer acmDeployer;
48
49     @MockBean
50     SmeDeployer smeDeployer;
51
52     @MockBean
53     SmeLifecycleManager smeLifecycleManager;
54
55     private final String validRappFile = "valid-rapp-package.csar";
56
57     private final String invalidRappFile = "invalid-rapp-package.csar";
58     private final String validCsarFileLocation = "src/test/resources/";
59
60     private final ObjectMapper objectMapper = new ObjectMapper();
61
62     @Test
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())
69                 .andExpect(
70                         jsonPath("$." + rappInstanceId.toString() + ".rappInstanceId").value(rappInstanceId.toString()))
71                 .andExpect(jsonPath("$." + rappInstanceId.toString() + ".state").value(
72                         RappInstanceState.UNDEPLOYED.name()));
73     }
74
75     @Test
76     void testGetAllRappInstancesFailure() throws Exception {
77         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance", UUID.randomUUID()))
78                 .andExpect(status().isNotFound());
79     }
80
81     @Test
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);
91         mockMvc.perform(
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));
96     }
97
98     @Test
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());
107     }
108
109     @Test
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()));
119     }
120
121     @Test
122     void testGetRappInstanceNoRappFailure() throws Exception {
123         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}/instance/{instance_id}", UUID.randomUUID(),
124                 UUID.randomUUID())).andExpect(status().isNotFound());
125     }
126
127     @Test
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());
136     }
137
138     @Test
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());
153     }
154
155
156     @Test
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());
171     }
172
173     @Test
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());
183     }
184
185     @Test
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());
201     }
202
203     @Test
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());
219     }
220
221     @Test
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());
233     }
234
235     @Test
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);
242         mockMvc.perform(
243                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
244                 .andExpect(status().isNoContent());
245     }
246
247     @Test
248     void testDeleteRappNoRappFailure() throws Exception {
249         UUID rappId = UUID.randomUUID();
250         UUID rappInstanceId = UUID.randomUUID();
251         mockMvc.perform(
252                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
253                 .andExpect(status().isNotFound());
254     }
255
256     @Test
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);
264         mockMvc.perform(
265                         MockMvcRequestBuilders.delete("/rapps/{rapp_id}/instance/{instance_id}", rappId, rappInstanceId))
266                 .andExpect(status().isNotFound());
267     }
268
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)
277                             .build();
278         return rapp;
279     }
280
281 }