2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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.acm.participant.kubernetes.rest;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doNothing;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.when;
29 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
30 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
31 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
34 import java.io.IOException;
35 import java.nio.charset.StandardCharsets;
36 import java.util.List;
37 import org.apache.commons.io.FileUtils;
38 import org.json.JSONObject;
39 import org.junit.jupiter.api.BeforeAll;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42 import org.junit.jupiter.api.extension.ExtendWith;
43 import org.onap.policy.clamp.acm.participant.kubernetes.controller.ChartController;
44 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
45 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList;
46 import org.onap.policy.clamp.acm.participant.kubernetes.parameters.ParticipantK8sParameters;
47 import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartService;
48 import org.onap.policy.common.utils.coder.Coder;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.common.utils.coder.StandardCoder;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.boot.context.properties.EnableConfigurationProperties;
53 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
54 import org.springframework.boot.test.mock.mockito.MockBean;
55 import org.springframework.http.MediaType;
56 import org.springframework.mock.web.MockMultipartFile;
57 import org.springframework.test.context.junit.jupiter.SpringExtension;
58 import org.springframework.test.web.servlet.MockMvc;
59 import org.springframework.test.web.servlet.RequestBuilder;
60 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
61 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
62 import org.springframework.web.context.WebApplicationContext;
65 @ExtendWith(SpringExtension.class)
66 @WebMvcTest(value = ChartController.class, properties = "chart.api.enabled=true")
67 @EnableConfigurationProperties(value = ParticipantK8sParameters.class)
68 class ChartControllerTest {
70 private static final Coder CODER = new StandardCoder();
71 private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
72 private static List<ChartInfo> charts;
73 private static String RETRIEVE_CHART_URL = "/helm/charts";
74 private static String INSTALL_CHART_URL = "/helm/install";
75 private static String UNINSTALL_CHART_URL = "/helm/uninstall/";
76 private static String ONBOARD_CHART_URL = "/helm/onboard/chart";
77 private static String DELETE_CHART_URL = "/helm/chart";
78 private static String CONFIGURE_REPO_URL = "/helm/repo";
81 private MockMvc mockMvc;
84 private ChartService chartService;
87 private WebApplicationContext context;
90 * Read input chart info json.
91 * @throws Exception incase of error.
94 static void setupParams() throws CoderException {
95 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
99 * Mock service layer in Controller.
100 * @throws Exception incase of error.
103 void mockServiceClass() {
104 when(chartService.getAllCharts()).thenReturn(charts);
105 when(chartService.getChart(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion()))
106 .thenReturn(charts.get(0));
108 this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
112 * Test endpoint for retrieving all charts.
113 * @throws Exception incase of error.
116 void retrieveAllCharts() throws Exception {
117 RequestBuilder requestBuilder;
118 requestBuilder = MockMvcRequestBuilders.get(RETRIEVE_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE);
120 mockMvc.perform(requestBuilder).andExpect(status().isOk())
121 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
122 .andExpect(jsonPath("$.charts.[0].chartId.name", is("HelloWorld")));
126 * Test endpoint for installing a chart.
127 * @throws Exception incase of error.
130 void installChart() throws Exception {
131 RequestBuilder requestBuilder;
133 //Mocking successful installation for void install method
134 doReturn(true).when(chartService).installChart(charts.get(0));
136 requestBuilder = MockMvcRequestBuilders.post(INSTALL_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE)
137 .content(getInstallationJson(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion()))
138 .contentType(MediaType.APPLICATION_JSON_VALUE);
140 mockMvc.perform(requestBuilder).andExpect(status().isCreated());
142 //Install Invalid chart, expects HTTP status NOT_FOUND
143 requestBuilder = MockMvcRequestBuilders.post(INSTALL_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE)
144 .content(getInstallationJson("invalidName", "invalidVersion"))
145 .contentType(MediaType.APPLICATION_JSON_VALUE);
147 mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
151 * Test endpoint for uninstalling a chart.
152 * @throws Exception incase of error.
155 void uninstallChart() throws Exception {
156 RequestBuilder requestBuilder;
158 //Mocking successful scenario for void uninstall method
159 doNothing().when(chartService).uninstallChart(charts.get(0));
161 requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + charts.get(0)
162 .getChartId().getName() + "/" + charts.get(0).getChartId().getVersion())
163 .accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE);
165 mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
168 requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName"
169 + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE)
170 .contentType(MediaType.APPLICATION_JSON_VALUE);
172 mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
176 * Test endpoint for chart onboarding.
177 * @throws Exception incase of error.
180 void onboardChart() throws Exception {
181 RequestBuilder requestBuilder;
182 MockMultipartFile chartFile = new MockMultipartFile("chart", "hello.tgz",
183 MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes());
185 MockMultipartFile overrideFile = new MockMultipartFile("values", "values.yaml",
186 MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes());
188 //Mocking successful scenario for void uninstall method
189 when(chartService.saveChart(charts.get(0), chartFile, null)).thenReturn(charts.get(0));
191 requestBuilder = MockMvcRequestBuilders.multipart(ONBOARD_CHART_URL)
192 .file(chartFile).file(overrideFile).param("info", getChartInfoJson());
194 mockMvc.perform(requestBuilder).andExpect(status().isOk());
198 * Test endpoint for deleting a chart.
199 * @throws Exception incase of error.
202 void deleteChart() throws Exception {
203 RequestBuilder requestBuilder;
205 //Mocking successful scenario for void uninstall method
206 doNothing().when(chartService).deleteChart(charts.get(0));
208 requestBuilder = MockMvcRequestBuilders.delete(DELETE_CHART_URL + "/" + charts.get(0)
209 .getChartId().getName() + "/" + charts.get(0).getChartId().getVersion())
210 .accept(MediaType.APPLICATION_JSON_VALUE)
211 .contentType(MediaType.APPLICATION_JSON_VALUE);
213 mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
215 requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName"
216 + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE)
217 .contentType(MediaType.APPLICATION_JSON_VALUE);
219 mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
224 * Test endpoint for configuring a helm repository.
225 * @throws Exception in case of error.
228 void testConfigureRepo() throws Exception {
229 RequestBuilder requestBuilder;
230 when(chartService.configureRepository(any())).thenReturn(true);
232 requestBuilder = MockMvcRequestBuilders.post(CONFIGURE_REPO_URL).accept(MediaType.APPLICATION_JSON_VALUE)
233 .content(getInstallationJson(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion()))
234 .contentType(MediaType.APPLICATION_JSON_VALUE);
236 mockMvc.perform(requestBuilder).andExpect(status().isCreated());
241 void testConfigureRepoAlreadyExist() throws Exception {
242 RequestBuilder requestBuilder;
243 when(chartService.configureRepository(any())).thenReturn(false);
245 requestBuilder = MockMvcRequestBuilders.post(CONFIGURE_REPO_URL).accept(MediaType.APPLICATION_JSON_VALUE)
246 .content(getInstallationJson(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion()))
247 .contentType(MediaType.APPLICATION_JSON_VALUE);
249 mockMvc.perform(requestBuilder).andExpect(status().isConflict());
254 private String getInstallationJson(String name, String version) {
255 JSONObject jsonObj = new JSONObject();
256 jsonObj.put("name", name);
257 jsonObj.put("version", version);
258 return jsonObj.toString();
261 private String getChartInfoJson() throws IOException {
262 return FileUtils.readFileToString(new File(CHART_INFO_YAML), StandardCharsets.UTF_8);