fcac64dd629915c16580d69f1af17c7a569754c8
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.controlloop.participant.kubernetes.rest;
23
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.when;
27 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
28 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
29 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
30
31 import java.io.File;
32 import java.util.List;
33 import org.json.JSONObject;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.onap.policy.clamp.controlloop.participant.kubernetes.controller.ChartController;
39 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
40 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
41 import org.onap.policy.clamp.controlloop.participant.kubernetes.parameters.ParticipantK8sParameters;
42 import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService;
43 import org.onap.policy.common.utils.coder.Coder;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.context.properties.EnableConfigurationProperties;
48 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
49 import org.springframework.boot.test.mock.mockito.MockBean;
50 import org.springframework.http.MediaType;
51 import org.springframework.mock.web.MockMultipartFile;
52 import org.springframework.test.context.junit.jupiter.SpringExtension;
53 import org.springframework.test.web.servlet.MockMvc;
54 import org.springframework.test.web.servlet.RequestBuilder;
55 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
56 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
57 import org.springframework.web.context.WebApplicationContext;
58
59
60 @ExtendWith(SpringExtension.class)
61 @WebMvcTest(value = ChartController.class)
62 @EnableConfigurationProperties(value = ParticipantK8sParameters.class)
63 class ChartControllerTest {
64
65     private static final Coder CODER = new StandardCoder();
66     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
67     private static List<ChartInfo> charts;
68     private static String DEFAULT_CHART_URL = "/helm/charts";
69     private static String INSTALL_CHART_URL = "/helm/install";
70     private static String UNINSTALL_CHART_URL = "/helm/uninstall/";
71
72     @Autowired
73     private MockMvc mockMvc;
74
75     @MockBean
76     private ChartService chartService;
77
78     @Autowired
79     private WebApplicationContext context;
80
81     /**
82      * Read input chart info json.
83      * @throws Exception incase of error.
84      */
85     @BeforeAll
86     static void setupParams() throws CoderException {
87         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
88     }
89
90     /**
91      * Mock service layer in Controller.
92      * @throws Exception incase of error.
93      */
94     @BeforeEach
95     void mockServiceClass() {
96         when(chartService.getAllCharts()).thenReturn(charts);
97         when(chartService.getChart(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion()))
98             .thenReturn(charts.get(0));
99
100         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
101     }
102
103     /**
104      * Test endpoint for retrieving all charts.
105      * @throws Exception incase of error.
106      */
107     @Test
108     void retrieveAllCharts() throws Exception {
109         RequestBuilder requestBuilder;
110         requestBuilder = MockMvcRequestBuilders.get(DEFAULT_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE);
111
112         mockMvc.perform(requestBuilder).andExpect(status().isOk())
113             .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
114             .andExpect(jsonPath("$.charts.[0].chartId.name", is("HelloWorld")));
115     }
116
117     /**
118      * Test endpoint for installing a chart.
119      * @throws Exception incase of error.
120      */
121     @Test
122     void installChart() throws Exception {
123         RequestBuilder requestBuilder;
124
125         //Mocking successful installation for void install method
126         doNothing().when(chartService).installChart(charts.get(0));
127
128         requestBuilder = MockMvcRequestBuilders.post(INSTALL_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE)
129             .content(getInstallationJson(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion()))
130             .contentType(MediaType.APPLICATION_JSON_VALUE);
131
132         mockMvc.perform(requestBuilder).andExpect(status().isCreated());
133
134         //Install Invalid chart, expects HTTP status NOT_FOUND
135         requestBuilder = MockMvcRequestBuilders.post(INSTALL_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE)
136             .content(getInstallationJson("invalidName", "invalidVersion"))
137             .contentType(MediaType.APPLICATION_JSON_VALUE);
138
139         mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
140     }
141
142     /**
143      * Test endpoint for uninstalling a chart.
144      * @throws Exception incase of error.
145      */
146     @Test
147     void uninstallChart() throws Exception {
148         RequestBuilder requestBuilder;
149
150         //Mocking successful scenario for void uninstall method
151         doNothing().when(chartService).uninstallChart(charts.get(0));
152
153         requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + charts.get(0)
154             .getChartId().getName() + "/" + charts.get(0).getChartId().getVersion())
155             .accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE);
156
157         mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
158
159         //Invalid chart
160         requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName"
161             + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE)
162             .contentType(MediaType.APPLICATION_JSON_VALUE);
163
164         mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
165     }
166
167     /**
168      * Test endpoint for chart onboarding.
169      * @throws Exception incase of error.
170      */
171     @Test
172     void onboardChart() throws Exception {
173         RequestBuilder requestBuilder;
174         MockMultipartFile chartFile = new MockMultipartFile("chart", "hello.tgz",
175             MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes());
176
177         MockMultipartFile overrideFile = new MockMultipartFile("values", "values.yaml",
178             MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes());
179
180         //Mocking successful scenario for void uninstall method
181         when(chartService.saveChart(charts.get(0), chartFile, null)).thenReturn(charts.get(0));
182
183         requestBuilder = MockMvcRequestBuilders.multipart(DEFAULT_CHART_URL)
184             .file(chartFile).file(overrideFile).param("info", getChartInfoJson());
185
186         mockMvc.perform(requestBuilder).andExpect(status().isOk());
187     }
188
189     /**
190      * Test endpoint for deleting a chart.
191      * @throws Exception incase of error.
192      */
193     @Test
194     void deleteChart() throws Exception {
195         RequestBuilder requestBuilder;
196
197         //Mocking successful scenario for void uninstall method
198         doNothing().when(chartService).deleteChart(charts.get(0));
199
200         requestBuilder = MockMvcRequestBuilders.delete(DEFAULT_CHART_URL + "/" + charts.get(0)
201             .getChartId().getName() + "/" + charts.get(0).getChartId().getVersion())
202             .accept(MediaType.APPLICATION_JSON_VALUE)
203             .contentType(MediaType.APPLICATION_JSON_VALUE);
204
205         mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
206         //Invalid chart
207         requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName"
208             + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE)
209             .contentType(MediaType.APPLICATION_JSON_VALUE);
210
211         mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
212
213     }
214
215     private String getInstallationJson(String name, String version) {
216         JSONObject jsonObj = new JSONObject();
217         jsonObj.put("name", name);
218         jsonObj.put("version", version);
219         return jsonObj.toString();
220     }
221
222     private String getChartInfoJson() {
223         JSONObject jsonObj = new JSONObject();
224         jsonObj.put("chartName", charts.get(0).getChartId().getName());
225         jsonObj.put("version", charts.get(0).getChartId().getVersion());
226         jsonObj.put("namespace", charts.get(0).getNamespace());
227         jsonObj.put("repository", charts.get(0).getRepository());
228         jsonObj.put("releaseName", charts.get(0).getReleaseName());
229         return jsonObj.toString();
230     }
231
232 }