Update response format of get all rapps endpoint
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / test / java / com / oransc / rappmanager / rest / RappControllerTest.java
1 package com.oransc.rappmanager.rest;
2
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;
8
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;
16 import java.io.File;
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;
31
32 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
33 @AutoConfigureMockMvc
34 public class RappControllerTest {
35
36     @Autowired
37     private MockMvc mockMvc;
38
39     @Autowired
40     private RappCacheService rappCacheService;
41
42     @MockBean
43     AcmDeployer acmDeployer;
44
45     private final String validRappFile = "valid-rapp-package.csar";
46
47     private final String invalidRappFile = "invalid-rapp-package.csar";
48     private final String validCsarFileLocation = "src/test/resources/";
49
50     private final ObjectMapper objectMapper = new ObjectMapper();
51
52     @Test
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)));
62     }
63
64     @Test
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()));
73     }
74
75     @Test
76     void testGetInvalidRapp() throws Exception {
77         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", UUID.randomUUID()))
78                 .andExpect(status().isBadRequest());
79     }
80
81     @Test
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());
89     }
90
91     @Test
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());
99     }
100
101     @Test
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());
112     }
113
114     @ParameterizedTest
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());
123     }
124
125     @Test
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());
136     }
137
138     @Test
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());
145     }
146
147     @Test
148     void testDeleteRappFailure() throws Exception {
149         mockMvc.perform(MockMvcRequestBuilders.delete("/rapps/{rapp_id}", UUID.randomUUID()))
150                 .andExpect(status().isNotFound());
151     }
152 }