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.io.IOException;
33 import java.nio.charset.StandardCharsets;
34 import java.util.List;
35 import org.apache.commons.io.FileUtils;
36 import org.json.JSONObject;
37 import org.junit.jupiter.api.BeforeAll;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.junit.jupiter.api.extension.ExtendWith;
41 import org.onap.policy.clamp.controlloop.participant.kubernetes.controller.ChartController;
42 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
43 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
44 import org.onap.policy.clamp.controlloop.participant.kubernetes.parameters.ParticipantK8sParameters;
45 import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.boot.context.properties.EnableConfigurationProperties;
51 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
52 import org.springframework.boot.test.mock.mockito.MockBean;
53 import org.springframework.http.MediaType;
54 import org.springframework.mock.web.MockMultipartFile;
55 import org.springframework.test.context.junit.jupiter.SpringExtension;
56 import org.springframework.test.web.servlet.MockMvc;
57 import org.springframework.test.web.servlet.RequestBuilder;
58 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
59 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
60 import org.springframework.web.context.WebApplicationContext;
63 @ExtendWith(SpringExtension.class)
64 @WebMvcTest(value = ChartController.class)
65 @EnableConfigurationProperties(value = ParticipantK8sParameters.class)
66 class ChartControllerTest {
68 private static final Coder CODER = new StandardCoder();
69 private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
70 private static List<ChartInfo> charts;
71 private static String RETRIEVE_CHART_URL = "/helm/charts";
72 private static String INSTALL_CHART_URL = "/helm/install";
73 private static String UNINSTALL_CHART_URL = "/helm/uninstall/";
74 private static String ONBOARD_CHART_URL = "/helm/onboard/chart";
75 private static String DELETE_CHART_URL = "/helm/chart";
78 private MockMvc mockMvc;
81 private ChartService chartService;
84 private WebApplicationContext context;
87 * Read input chart info json.
88 * @throws Exception incase of error.
91 static void setupParams() throws CoderException {
92 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
96 * Mock service layer in Controller.
97 * @throws Exception incase of error.
100 void mockServiceClass() {
101 when(chartService.getAllCharts()).thenReturn(charts);
102 when(chartService.getChart(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion()))
103 .thenReturn(charts.get(0));
105 this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
109 * Test endpoint for retrieving all charts.
110 * @throws Exception incase of error.
113 void retrieveAllCharts() throws Exception {
114 RequestBuilder requestBuilder;
115 requestBuilder = MockMvcRequestBuilders.get(RETRIEVE_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE);
117 mockMvc.perform(requestBuilder).andExpect(status().isOk())
118 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
119 .andExpect(jsonPath("$.charts.[0].chartId.name", is("HelloWorld")));
123 * Test endpoint for installing a chart.
124 * @throws Exception incase of error.
127 void installChart() throws Exception {
128 RequestBuilder requestBuilder;
130 //Mocking successful installation for void install method
131 doNothing().when(chartService).installChart(charts.get(0));
133 requestBuilder = MockMvcRequestBuilders.post(INSTALL_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE)
134 .content(getInstallationJson(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion()))
135 .contentType(MediaType.APPLICATION_JSON_VALUE);
137 mockMvc.perform(requestBuilder).andExpect(status().isCreated());
139 //Install Invalid chart, expects HTTP status NOT_FOUND
140 requestBuilder = MockMvcRequestBuilders.post(INSTALL_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE)
141 .content(getInstallationJson("invalidName", "invalidVersion"))
142 .contentType(MediaType.APPLICATION_JSON_VALUE);
144 mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
148 * Test endpoint for uninstalling a chart.
149 * @throws Exception incase of error.
152 void uninstallChart() throws Exception {
153 RequestBuilder requestBuilder;
155 //Mocking successful scenario for void uninstall method
156 doNothing().when(chartService).uninstallChart(charts.get(0));
158 requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + charts.get(0)
159 .getChartId().getName() + "/" + charts.get(0).getChartId().getVersion())
160 .accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE);
162 mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
165 requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName"
166 + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE)
167 .contentType(MediaType.APPLICATION_JSON_VALUE);
169 mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
173 * Test endpoint for chart onboarding.
174 * @throws Exception incase of error.
177 void onboardChart() throws Exception {
178 RequestBuilder requestBuilder;
179 MockMultipartFile chartFile = new MockMultipartFile("chart", "hello.tgz",
180 MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes());
182 MockMultipartFile overrideFile = new MockMultipartFile("values", "values.yaml",
183 MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes());
185 //Mocking successful scenario for void uninstall method
186 when(chartService.saveChart(charts.get(0), chartFile, null)).thenReturn(charts.get(0));
188 requestBuilder = MockMvcRequestBuilders.multipart(ONBOARD_CHART_URL)
189 .file(chartFile).file(overrideFile).param("info", getChartInfoJson());
191 mockMvc.perform(requestBuilder).andExpect(status().isOk());
195 * Test endpoint for deleting a chart.
196 * @throws Exception incase of error.
199 void deleteChart() throws Exception {
200 RequestBuilder requestBuilder;
202 //Mocking successful scenario for void uninstall method
203 doNothing().when(chartService).deleteChart(charts.get(0));
205 requestBuilder = MockMvcRequestBuilders.delete(DELETE_CHART_URL + "/" + charts.get(0)
206 .getChartId().getName() + "/" + charts.get(0).getChartId().getVersion())
207 .accept(MediaType.APPLICATION_JSON_VALUE)
208 .contentType(MediaType.APPLICATION_JSON_VALUE);
210 mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
212 requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName"
213 + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE)
214 .contentType(MediaType.APPLICATION_JSON_VALUE);
216 mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
220 private String getInstallationJson(String name, String version) {
221 JSONObject jsonObj = new JSONObject();
222 jsonObj.put("name", name);
223 jsonObj.put("version", version);
224 return jsonObj.toString();
227 private String getChartInfoJson() throws IOException {
228 return FileUtils.readFileToString(new File(CHART_INFO_YAML), StandardCharsets.UTF_8);