1 package com.oransc.rappmanager.rest;
3 import static org.hamcrest.Matchers.hasSize;
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.rapp.PrimeOrder;
12 import com.oransc.rappmanager.models.rapp.Rapp;
13 import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
14 import com.oransc.rappmanager.models.rapp.RappState;
15 import com.oransc.rappmanager.models.cache.RappCacheService;
17 import java.io.FileInputStream;
18 import java.util.UUID;
19 import org.apache.http.entity.ContentType;
20 import org.junit.jupiter.api.Test;
21 import org.junit.jupiter.params.ParameterizedTest;
22 import org.junit.jupiter.params.provider.EnumSource;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
25 import org.springframework.boot.test.context.SpringBootTest;
26 import org.springframework.boot.test.mock.mockito.MockBean;
27 import org.springframework.http.MediaType;
28 import org.springframework.mock.web.MockMultipartFile;
29 import org.springframework.test.web.servlet.MockMvc;
30 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
32 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
34 public class RappControllerTest {
37 private MockMvc mockMvc;
40 private RappCacheService rappCacheService;
43 AcmDeployer acmDeployer;
45 private final String validRappFile = "valid-rapp-package.csar";
47 private final String invalidRappFile = "invalid-rapp-package.csar";
48 private final String validCsarFileLocation = "src/test/resources/";
50 private final ObjectMapper objectMapper = new ObjectMapper();
53 void testGetRapps() throws Exception {
54 mockMvc.perform(MockMvcRequestBuilders.get("/rapps")).andExpect(status().isOk())
55 .andExpect(jsonPath("$", hasSize(0)));
56 UUID rappId = UUID.randomUUID();
57 Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
58 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
59 rappCacheService.putRapp(rapp);
60 mockMvc.perform(MockMvcRequestBuilders.get("/rapps")).andExpect(status().isOk())
61 .andExpect(jsonPath("$", hasSize(1)));
65 void testGetRapp() throws Exception {
66 UUID rappId = UUID.randomUUID();
67 Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
68 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
69 rappCacheService.putRapp(rapp);
70 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", rappId)).andExpect(status().isOk())
71 .andExpect(jsonPath("$.rappId").value(rappId.toString()))
72 .andExpect(jsonPath("$.state").value(RappState.COMMISSIONED.name()));
76 void testGetInvalidRapp() throws Exception {
77 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", UUID.randomUUID()))
78 .andExpect(status().isBadRequest());
82 void testCreateRapp() throws Exception {
83 String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
84 MockMultipartFile multipartFile =
85 new MockMultipartFile("file", validRappFile, ContentType.MULTIPART_FORM_DATA.getMimeType(),
86 new FileInputStream(rappCsarPath));
87 mockMvc.perform(MockMvcRequestBuilders.multipart("/rapps/{rapp_id}", UUID.randomUUID()).file(multipartFile))
88 .andExpect(status().isAccepted());
92 void testCreateInvalidRapp() throws Exception {
93 String rappCsarPath = validCsarFileLocation + File.separator + invalidRappFile;
94 MockMultipartFile multipartFile =
95 new MockMultipartFile("file", invalidRappFile, ContentType.MULTIPART_FORM_DATA.getMimeType(),
96 new FileInputStream(rappCsarPath));
97 mockMvc.perform(MockMvcRequestBuilders.multipart("/rapps/{rapp_id}", UUID.randomUUID()).file(multipartFile))
98 .andExpect(status().isBadRequest());
102 void testPrimeRapp() throws Exception {
103 UUID rappId = UUID.randomUUID();
104 Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
105 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
106 rappCacheService.putRapp(rapp);
107 when(acmDeployer.primeRapp(any())).thenReturn(true);
108 RappPrimeOrder rappPrimeOrder = new RappPrimeOrder();
109 rappPrimeOrder.setPrimeOrder(PrimeOrder.PRIME);
110 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}", rappId).contentType(MediaType.APPLICATION_JSON)
111 .content(objectMapper.writeValueAsString(rappPrimeOrder))).andExpect(status().isOk());
115 @EnumSource(value = PrimeOrder.class, names = {"PRIME", "DEPRIME"})
116 void testPrimeRappFailure(PrimeOrder primeOrder) throws Exception {
117 RappPrimeOrder rappPrimeOrder = new RappPrimeOrder();
118 rappPrimeOrder.setPrimeOrder(primeOrder);
119 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}", UUID.randomUUID())
120 .contentType(MediaType.APPLICATION_JSON)
121 .content(objectMapper.writeValueAsString(rappPrimeOrder)))
122 .andExpect(status().isNotFound());
126 void testDeprimeRapp() throws Exception {
127 UUID rappId = UUID.randomUUID();
128 Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
129 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
130 rappCacheService.putRapp(rapp);
131 when(acmDeployer.deprimeRapp(any())).thenReturn(true);
132 RappPrimeOrder rappPrimeOrder = new RappPrimeOrder();
133 rappPrimeOrder.setPrimeOrder(PrimeOrder.DEPRIME);
134 mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}", rappId).contentType(MediaType.APPLICATION_JSON)
135 .content(objectMapper.writeValueAsString(rappPrimeOrder))).andExpect(status().isOk());
139 void testDeleteRapp() throws Exception {
140 UUID rappId = UUID.randomUUID();
141 Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
142 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
143 rappCacheService.putRapp(rapp);
144 mockMvc.perform(MockMvcRequestBuilders.delete("/rapps/{rapp_id}", rappId)).andExpect(status().isOk());
148 void testDeleteRappFailure() throws Exception {
149 mockMvc.perform(MockMvcRequestBuilders.delete("/rapps/{rapp_id}", UUID.randomUUID()))
150 .andExpect(status().isNotFound());