aboutsummaryrefslogtreecommitdiffstats
path: root/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/rest/ChartControllerTest.java
blob: 73c5c98a17ac185012149fd0c7ba342dd8c4c967 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021-2022 Nordix Foundation.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.acm.participant.kubernetes.rest;

import static org.hamcrest.CoreMatchers.is;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.onap.policy.clamp.acm.participant.kubernetes.controller.ChartController;
import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList;
import org.onap.policy.clamp.acm.participant.kubernetes.parameters.ParticipantK8sParameters;
import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartService;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;


@ExtendWith(SpringExtension.class)
@WebMvcTest(value = ChartController.class, properties = "chart.api.enabled=true")
@EnableConfigurationProperties(value = ParticipantK8sParameters.class)
class ChartControllerTest {

    private static final Coder CODER = new StandardCoder();
    private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
    private static List<ChartInfo> charts;
    private static String RETRIEVE_CHART_URL = "/helm/charts";
    private static String INSTALL_CHART_URL = "/helm/install";
    private static String UNINSTALL_CHART_URL = "/helm/uninstall/";
    private static String ONBOARD_CHART_URL = "/helm/onboard/chart";
    private static String DELETE_CHART_URL = "/helm/chart";
    private static String CONFIGURE_REPO_URL = "/helm/repo";

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private ChartService chartService;

    @Autowired
    private WebApplicationContext context;

    /**
     * Read input chart info json.
     * @throws Exception incase of error.
     */
    @BeforeAll
    static void setupParams() throws CoderException {
        charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
    }

    /**
     * Mock service layer in Controller.
     * @throws Exception incase of error.
     */
    @BeforeEach
    void mockServiceClass() {
        when(chartService.getAllCharts()).thenReturn(charts);
        when(chartService.getChart(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion()))
            .thenReturn(charts.get(0));

        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
    }

    /**
     * Test endpoint for retrieving all charts.
     * @throws Exception incase of error.
     */
    @Test
    void retrieveAllCharts() throws Exception {
        RequestBuilder requestBuilder;
        requestBuilder = MockMvcRequestBuilders.get(RETRIEVE_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE);

        mockMvc.perform(requestBuilder).andExpect(status().isOk())
            .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
            .andExpect(jsonPath("$.charts.[0].chartId.name", is("HelloWorld")));
    }

    /**
     * Test endpoint for installing a chart.
     * @throws Exception incase of error.
     */
    @Test
    void installChart() throws Exception {
        RequestBuilder requestBuilder;

        //Mocking successful installation for void install method
        doNothing().when(chartService).installChart(charts.get(0));

        requestBuilder = MockMvcRequestBuilders.post(INSTALL_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE)
            .content(getInstallationJson(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion()))
            .contentType(MediaType.APPLICATION_JSON_VALUE);

        mockMvc.perform(requestBuilder).andExpect(status().isCreated());

        //Install Invalid chart, expects HTTP status NOT_FOUND
        requestBuilder = MockMvcRequestBuilders.post(INSTALL_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE)
            .content(getInstallationJson("invalidName", "invalidVersion"))
            .contentType(MediaType.APPLICATION_JSON_VALUE);

        mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
    }

    /**
     * Test endpoint for uninstalling a chart.
     * @throws Exception incase of error.
     */
    @Test
    void uninstallChart() throws Exception {
        RequestBuilder requestBuilder;

        //Mocking successful scenario for void uninstall method
        doNothing().when(chartService).uninstallChart(charts.get(0));

        requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + charts.get(0)
            .getChartId().getName() + "/" + charts.get(0).getChartId().getVersion())
            .accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE);

        mockMvc.perform(requestBuilder).andExpect(status().isNoContent());

        //Invalid chart
        requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName"
            + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE)
            .contentType(MediaType.APPLICATION_JSON_VALUE);

        mockMvc.perform(requestBuilder).andExpect(status().isNotFound());
    }

    /**
     * Test endpoint for chart onboarding.
     * @throws Exception incase of error.
     */
    @Test
    void onboardChart() throws Exception {
        RequestBuilder requestBuilder;
        MockMultipartFile chartFile = new MockMultipartFile("chart", "hello.tgz",
            MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes());

        MockMultipartFile overrideFile = new MockMultipartFile("values", "values.yaml",
            MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes());

        //Mocking successful scenario for void uninstall method
        when(chartService.saveChart(charts.get(0), chartFile, null)).thenReturn(charts.get(0));

        requestBuilder = MockMvcRequestBuilders.multipart(ONBOARD_CHART_URL)
            .file(chartFile).file(overrideFile).param("info", getChartInfoJson());

        mockMvc.perform(requestBuilder).andExpect(status().isOk());
    }

    /**
     * Test endpoint for deleting a chart.
     * @throws Exception incase of error.
     */
    @Test
    void deleteChart() throws Exception {
        RequestBuilder requestBuilder;

        //Mocking successful scenario for void uninstall method
        doNothing().when(chartService).deleteChart(charts.get(0));

        requestBuilder = MockMvcRequestBuilders.delete(DELETE_CHART_URL + "/" + charts.get(0)
            .getChartId().getName() + "/" + charts.get(0).getChartId().getVersion())
            .accept(MediaType.APPLICATION_JSON_VALUE)
            .contentType(MediaType.APPLICATION_JSON_VALUE);

        mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
        //Invalid chart
        requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName"
            + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE)
            .contentType(MediaType.APPLICATION_JSON_VALUE);

        mockMvc.perform(requestBuilder).andExpect(status().isNotFound());

    }

    /**
     * Test endpoint for configuring a helm repository.
     * @throws Exception in case of error.
     */
    @Test
    void testConfigureRepo() throws Exception {
        RequestBuilder requestBuilder;

        requestBuilder = MockMvcRequestBuilders.post(CONFIGURE_REPO_URL).accept(MediaType.APPLICATION_JSON_VALUE)
            .content(getInstallationJson(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion()))
            .contentType(MediaType.APPLICATION_JSON_VALUE);

        mockMvc.perform(requestBuilder).andExpect(status().isCreated());

    }


    private String getInstallationJson(String name, String version) {
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("name", name);
        jsonObj.put("version", version);
        return jsonObj.toString();
    }

    private String getChartInfoJson() throws IOException {
        return FileUtils.readFileToString(new File(CHART_INFO_YAML), StandardCharsets.UTF_8);
    }

}