Update flow and architecture images
[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.dme.service.DmeDeployer;
12 import com.oransc.rappmanager.models.cache.RappCacheService;
13 import com.oransc.rappmanager.models.rapp.PrimeOrder;
14 import com.oransc.rappmanager.models.rapp.Rapp;
15 import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
16 import com.oransc.rappmanager.models.rapp.RappState;
17 import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.util.UUID;
21 import org.apache.http.entity.ContentType;
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.params.ParameterizedTest;
24 import org.junit.jupiter.params.provider.EnumSource;
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.mock.web.MockMultipartFile;
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 RappControllerTest {
37
38     @Autowired
39     private MockMvc mockMvc;
40
41     @Autowired
42     private RappCacheService rappCacheService;
43
44     @MockBean
45     AcmDeployer acmDeployer;
46
47     @MockBean
48     DmeDeployer dmeDeployer;
49
50     @MockBean
51     SmeLifecycleManager smeLifecycleManager;
52
53     private final String validRappFile = "valid-rapp-package.csar";
54
55     private final String invalidRappFile = "invalid-rapp-package.csar";
56     private final String validCsarFileLocation = "src/test/resources/";
57
58     private final ObjectMapper objectMapper = new ObjectMapper();
59
60     @Test
61     void testGetRapps() throws Exception {
62         mockMvc.perform(MockMvcRequestBuilders.get("/rapps")).andExpect(status().isOk())
63                 .andExpect(jsonPath("$", hasSize(0)));
64         UUID rappId = UUID.randomUUID();
65         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
66                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
67         rappCacheService.putRapp(rapp);
68         mockMvc.perform(MockMvcRequestBuilders.get("/rapps")).andExpect(status().isOk())
69                 .andExpect(jsonPath("$", hasSize(1)));
70     }
71
72     @Test
73     void testGetRapp() throws Exception {
74         UUID rappId = UUID.randomUUID();
75         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
76                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
77         rappCacheService.putRapp(rapp);
78         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", rappId)).andExpect(status().isOk())
79                 .andExpect(jsonPath("$.rappId").value(rappId.toString()))
80                 .andExpect(jsonPath("$.state").value(RappState.COMMISSIONED.name()));
81     }
82
83     @Test
84     void testGetInvalidRapp() throws Exception {
85         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", UUID.randomUUID()))
86                 .andExpect(status().isNotFound());
87     }
88
89     @Test
90     void testCreateRapp() throws Exception {
91         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
92         MockMultipartFile multipartFile =
93                 new MockMultipartFile("file", validRappFile, ContentType.MULTIPART_FORM_DATA.getMimeType(),
94                         new FileInputStream(rappCsarPath));
95         mockMvc.perform(MockMvcRequestBuilders.multipart("/rapps/{rapp_id}", UUID.randomUUID()).file(multipartFile))
96                 .andExpect(status().isAccepted());
97     }
98
99     @Test
100     void testCreateInvalidRapp() throws Exception {
101         String rappCsarPath = validCsarFileLocation + File.separator + invalidRappFile;
102         MockMultipartFile multipartFile =
103                 new MockMultipartFile("file", invalidRappFile, ContentType.MULTIPART_FORM_DATA.getMimeType(),
104                         new FileInputStream(rappCsarPath));
105         mockMvc.perform(MockMvcRequestBuilders.multipart("/rapps/{rapp_id}", UUID.randomUUID()).file(multipartFile))
106                 .andExpect(status().isBadRequest());
107     }
108
109     @Test
110     void testPrimeRapp() throws Exception {
111         UUID rappId = UUID.randomUUID();
112         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
113                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
114         rappCacheService.putRapp(rapp);
115         when(acmDeployer.primeRapp(any())).thenReturn(true);
116         when(dmeDeployer.primeRapp(any())).thenReturn(true);
117         RappPrimeOrder rappPrimeOrder = new RappPrimeOrder();
118         rappPrimeOrder.setPrimeOrder(PrimeOrder.PRIME);
119         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}", rappId).contentType(MediaType.APPLICATION_JSON)
120                                 .content(objectMapper.writeValueAsString(rappPrimeOrder))).andExpect(status().isOk());
121     }
122
123     @ParameterizedTest
124     @EnumSource(value = PrimeOrder.class, names = {"PRIME", "DEPRIME"})
125     void testPrimeRappFailure(PrimeOrder primeOrder) throws Exception {
126         RappPrimeOrder rappPrimeOrder = new RappPrimeOrder();
127         rappPrimeOrder.setPrimeOrder(primeOrder);
128         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}", UUID.randomUUID())
129                                 .contentType(MediaType.APPLICATION_JSON)
130                                 .content(objectMapper.writeValueAsString(rappPrimeOrder)))
131                 .andExpect(status().isNotFound());
132     }
133
134     @Test
135     void testDeprimeRapp() throws Exception {
136         UUID rappId = UUID.randomUUID();
137         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
138                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
139         rappCacheService.putRapp(rapp);
140         when(acmDeployer.deprimeRapp(any())).thenReturn(true);
141         when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
142         RappPrimeOrder rappPrimeOrder = new RappPrimeOrder();
143         rappPrimeOrder.setPrimeOrder(PrimeOrder.DEPRIME);
144         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}", rappId).contentType(MediaType.APPLICATION_JSON)
145                                 .content(objectMapper.writeValueAsString(rappPrimeOrder))).andExpect(status().isOk());
146     }
147
148     @Test
149     void testDeleteRapp() throws Exception {
150         UUID rappId = UUID.randomUUID();
151         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
152                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
153         rappCacheService.putRapp(rapp);
154         mockMvc.perform(MockMvcRequestBuilders.delete("/rapps/{rapp_id}", rappId)).andExpect(status().isOk());
155     }
156
157     @Test
158     void testDeleteRappFailure() throws Exception {
159         mockMvc.perform(MockMvcRequestBuilders.delete("/rapps/{rapp_id}", UUID.randomUUID()))
160                 .andExpect(status().isNotFound());
161     }
162 }