957a69a08f1d4a1faeeea3f451148ee846f41704
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.kubernetes.service;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.doNothing;
31 import static org.mockito.Mockito.doReturn;
32 import static org.mockito.Mockito.doThrow;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.Collection;
37 import java.util.List;
38 import org.junit.jupiter.api.BeforeAll;
39 import org.junit.jupiter.api.Test;
40 import org.junit.jupiter.api.extension.ExtendWith;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mock;
43 import org.mockito.Spy;
44 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
45 import org.onap.policy.clamp.controlloop.participant.kubernetes.helm.HelmClient;
46 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
47 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
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.mock.web.MockMultipartFile;
52 import org.springframework.test.context.junit.jupiter.SpringExtension;
53
54 @ExtendWith(SpringExtension.class)
55 class ChartServiceTest {
56
57     private static final Coder CODER = new StandardCoder();
58     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
59     private static List<ChartInfo> charts;
60
61     @InjectMocks
62     @Spy
63     private ChartService chartService = new ChartService();
64
65     @Mock
66     private ChartStore chartStore;
67
68     @Mock
69     private HelmClient helmClient;
70
71     @BeforeAll
72     static void init() throws CoderException {
73         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
74     }
75
76     @Test
77     void test_getAllCharts() {
78         assertThat(chartService.getAllCharts()).isEmpty();
79
80         doReturn(charts).when(chartStore).getAllCharts();
81         Collection<ChartInfo> result = chartService.getAllCharts();
82         assertNotNull(result);
83         assertThat(result).containsAll(charts);
84     }
85
86     @Test
87     void test_getChart() {
88         assertNull(chartService.getChart("dummyName", "dummyversion"));
89
90         doReturn(charts.get(0)).when(chartStore).getChart(any(), any());
91         ChartInfo chart = chartService.getChart(charts.get(0).getChartName(),
92             charts.get(0).getVersion());
93         assertNotNull(chart);
94         assertThat(chart.getNamespace()).isEqualTo(charts.get(0).getNamespace());
95     }
96
97     @Test
98     void test_saveChart() throws IOException, ServiceException {
99         doThrow(IOException.class).when(chartStore).saveChart(charts.get(0), null, null);
100         assertThatThrownBy(() -> chartService.saveChart(charts.get(0), null, null))
101             .isInstanceOf(IOException.class);
102
103         MockMultipartFile mockChartFile = new MockMultipartFile("chart", "dummy".getBytes());
104         MockMultipartFile mockOverrideFile = new MockMultipartFile("override", "dummy".getBytes());
105
106         doReturn(charts.get(0)).when(chartStore).saveChart(any(), any(), any());
107
108         ChartInfo chart = chartService.saveChart(charts.get(0), mockChartFile, mockOverrideFile);
109         assertNotNull(chart);
110         assertThat(chart.getChartName()).isEqualTo(charts.get(0).getChartName());
111
112     }
113
114     @Test
115     void test_installChart() throws IOException, ServiceException {
116         assertDoesNotThrow(() -> chartService.installChart(charts.get(0)));
117         doThrow(ServiceException.class).when(helmClient).installChart(any());
118         assertThatThrownBy(() -> chartService.installChart(charts.get(0))).isInstanceOf(ServiceException.class);
119
120         doReturn("dummyRepoName").when(chartService).findChartRepo(any());
121         doNothing().when(helmClient).installChart(any());
122         chartService.installChart(charts.get(1));
123         assertEquals("dummyRepoName", charts.get(1).getRepository());
124
125         ChartInfo testChart = charts.get(1);
126         testChart.setRepository(null);
127         doReturn(null).when(chartService).findChartRepo(any());
128         chartService.installChart(charts.get(1));
129     }
130
131     @Test
132     void test_UninstallChart() throws ServiceException {
133         assertDoesNotThrow(() -> chartService.uninstallChart(charts.get(0)));
134         doThrow(ServiceException.class).when(helmClient).uninstallChart(any());
135         assertThatThrownBy(() -> chartService.uninstallChart(charts.get(0))).isInstanceOf(ServiceException.class);
136     }
137
138     @Test
139     void test_findChartRepo() throws IOException, ServiceException {
140         assertDoesNotThrow(() -> chartService.findChartRepo(charts.get(0)));
141         doReturn("dummyRepoName").when(helmClient).findChartRepository(any());
142         assertEquals("dummyRepoName", chartService.findChartRepo(charts.get(1)));
143
144         doThrow(ServiceException.class).when(helmClient).findChartRepository(any());
145         assertThatThrownBy(() -> chartService.findChartRepo(charts.get(0))).isInstanceOf(ServiceException.class);
146     }
147 }