bbb57c3206c66310d8c344f491c7b182c7fb83f6
[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.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;
61
62
63 @ExtendWith(SpringExtension.class)
64 @WebMvcTest(value = ChartController.class)
65 @EnableConfigurationProperties(value = ParticipantK8sParameters.class)
66 class ChartControllerTest {
67
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";
76
77     @Autowired
78     private MockMvc mockMvc;
79
80     @MockBean
81     private ChartService chartService;
82
83     @Autowired
84     private WebApplicationContext context;
85
86     /**
87      * Read input chart info json.
88      * @throws Exception incase of error.
89      */
90     @BeforeAll
91     static void setupParams() throws CoderException {
92         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
93     }
94
95     /**
96      * Mock service layer in Controller.
97      * @throws Exception incase of error.
98      */
99     @BeforeEach
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));
104
105         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
106     }
107
108     /**
109      * Test endpoint for retrieving all charts.
110      * @throws Exception incase of error.
111      */
112     @Test
113     void retrieveAllCharts() throws Exception {
114         RequestBuilder requestBuilder;
115         requestBuilder = MockMvcRequestBuilders.get(RETRIEVE_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE);
116
117         mockMvc.perform(requestBuilder).andExpect(status().isOk())
118             .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
119             .andExpect(jsonPath("$.charts.[0].chartId.name", is("HelloWorld")));
120     }
121
122     /**
123      * Test endpoint for installing a chart.
124      * @throws Exception incase of error.
125      */
126     @Test
127     void installChart() throws Exception {
128         RequestBuilder requestBuilder;
129
130         //Mocking successful installation for void install method
131         doNothing().when(chartService).installChart(charts.get(0));
132
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);
136
137         mockMvc.perform(requestBuilder).andExpect(status().isCreated());
138
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);
143
144         mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
145     }
146
147     /**
148      * Test endpoint for uninstalling a chart.
149      * @throws Exception incase of error.
150      */
151     @Test
152     void uninstallChart() throws Exception {
153         RequestBuilder requestBuilder;
154
155         //Mocking successful scenario for void uninstall method
156         doNothing().when(chartService).uninstallChart(charts.get(0));
157
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);
161
162         mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
163
164         //Invalid chart
165         requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName"
166             + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE)
167             .contentType(MediaType.APPLICATION_JSON_VALUE);
168
169         mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
170     }
171
172     /**
173      * Test endpoint for chart onboarding.
174      * @throws Exception incase of error.
175      */
176     @Test
177     void onboardChart() throws Exception {
178         RequestBuilder requestBuilder;
179         MockMultipartFile chartFile = new MockMultipartFile("chart", "hello.tgz",
180             MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes());
181
182         MockMultipartFile overrideFile = new MockMultipartFile("values", "values.yaml",
183             MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes());
184
185         //Mocking successful scenario for void uninstall method
186         when(chartService.saveChart(charts.get(0), chartFile, null)).thenReturn(charts.get(0));
187
188         requestBuilder = MockMvcRequestBuilders.multipart(ONBOARD_CHART_URL)
189             .file(chartFile).file(overrideFile).param("info", getChartInfoJson());
190
191         mockMvc.perform(requestBuilder).andExpect(status().isOk());
192     }
193
194     /**
195      * Test endpoint for deleting a chart.
196      * @throws Exception incase of error.
197      */
198     @Test
199     void deleteChart() throws Exception {
200         RequestBuilder requestBuilder;
201
202         //Mocking successful scenario for void uninstall method
203         doNothing().when(chartService).deleteChart(charts.get(0));
204
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);
209
210         mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
211         //Invalid chart
212         requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName"
213             + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE)
214             .contentType(MediaType.APPLICATION_JSON_VALUE);
215
216         mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
217
218     }
219
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();
225     }
226
227     private String getChartInfoJson() throws IOException {
228         return FileUtils.readFileToString(new File(CHART_INFO_YAML), StandardCharsets.UTF_8);
229     }
230
231 }