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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.participant.kubernetes.rest;
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;
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;
60 @ExtendWith(SpringExtension.class)
61 @WebMvcTest(value = ChartController.class)
62 @EnableConfigurationProperties(value = ParticipantK8sParameters.class)
63 class ChartControllerTest {
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/";
73 private MockMvc mockMvc;
76 private ChartService chartService;
79 private WebApplicationContext context;
82 * Read input chart info json.
83 * @throws Exception incase of error.
86 static void setupParams() throws CoderException {
87 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
91 * Mock service layer in Controller.
92 * @throws Exception incase of error.
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));
100 this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
104 * Test endpoint for retrieving all charts.
105 * @throws Exception incase of error.
108 void retrieveAllCharts() throws Exception {
109 RequestBuilder requestBuilder;
110 requestBuilder = MockMvcRequestBuilders.get(DEFAULT_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE);
112 mockMvc.perform(requestBuilder).andExpect(status().isOk())
113 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
114 .andExpect(jsonPath("$.charts.[0].chartId.name", is("HelloWorld")));
118 * Test endpoint for installing a chart.
119 * @throws Exception incase of error.
122 void installChart() throws Exception {
123 RequestBuilder requestBuilder;
125 //Mocking successful installation for void install method
126 doNothing().when(chartService).installChart(charts.get(0));
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);
132 mockMvc.perform(requestBuilder).andExpect(status().isCreated());
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);
139 mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
143 * Test endpoint for uninstalling a chart.
144 * @throws Exception incase of error.
147 void uninstallChart() throws Exception {
148 RequestBuilder requestBuilder;
150 //Mocking successful scenario for void uninstall method
151 doNothing().when(chartService).uninstallChart(charts.get(0));
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);
157 mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
160 requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName"
161 + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE)
162 .contentType(MediaType.APPLICATION_JSON_VALUE);
164 mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
168 * Test endpoint for chart onboarding.
169 * @throws Exception incase of error.
172 void onboardChart() throws Exception {
173 RequestBuilder requestBuilder;
174 MockMultipartFile chartFile = new MockMultipartFile("chart", "hello.tgz",
175 MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes());
177 MockMultipartFile overrideFile = new MockMultipartFile("values", "values.yaml",
178 MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes());
180 //Mocking successful scenario for void uninstall method
181 when(chartService.saveChart(charts.get(0), chartFile, null)).thenReturn(charts.get(0));
183 requestBuilder = MockMvcRequestBuilders.multipart(DEFAULT_CHART_URL)
184 .file(chartFile).file(overrideFile).param("info", getChartInfoJson());
186 mockMvc.perform(requestBuilder).andExpect(status().isOk());
190 * Test endpoint for deleting a chart.
191 * @throws Exception incase of error.
194 void deleteChart() throws Exception {
195 RequestBuilder requestBuilder;
197 //Mocking successful scenario for void uninstall method
198 doNothing().when(chartService).deleteChart(charts.get(0));
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);
205 mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
207 requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName"
208 + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE)
209 .contentType(MediaType.APPLICATION_JSON_VALUE);
211 mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
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();
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();