f5b6093d3be5fc52853424a4d02eed0aa35704ac
[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.acm.participant.kubernetes.service;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.Mockito.doNothing;
32 import static org.mockito.Mockito.doReturn;
33 import static org.mockito.Mockito.doThrow;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.util.Collection;
38 import java.util.List;
39 import org.junit.jupiter.api.BeforeAll;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.extension.ExtendWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.Spy;
45 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
46 import org.onap.policy.clamp.acm.participant.kubernetes.helm.HelmClient;
47 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
48 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList;
49 import org.onap.policy.common.utils.coder.Coder;
50 import org.onap.policy.common.utils.coder.CoderException;
51 import org.onap.policy.common.utils.coder.StandardCoder;
52 import org.springframework.mock.web.MockMultipartFile;
53 import org.springframework.test.context.junit.jupiter.SpringExtension;
54
55 @ExtendWith(SpringExtension.class)
56 class ChartServiceTest {
57
58     private static final Coder CODER = new StandardCoder();
59     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
60     private static List<ChartInfo> charts;
61
62     @InjectMocks
63     @Spy
64     private ChartService chartService = new ChartService();
65
66     @Mock
67     private ChartStore chartStore;
68
69     @Mock
70     private HelmClient helmClient;
71
72     @BeforeAll
73     static void init() throws CoderException {
74         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
75     }
76
77     @Test
78     void test_getAllCharts() {
79         assertThat(chartService.getAllCharts()).isEmpty();
80
81         doReturn(charts).when(chartStore).getAllCharts();
82         Collection<ChartInfo> result = chartService.getAllCharts();
83         assertNotNull(result);
84         assertThat(result).containsAll(charts);
85     }
86
87     @Test
88     void test_getChart() {
89         assertNull(chartService.getChart("dummyName", "dummyversion"));
90
91         doReturn(charts.get(0)).when(chartStore).getChart(any(), any());
92         ChartInfo chart = chartService.getChart(charts.get(0).getChartId().getName(),
93             charts.get(0).getChartId().getVersion());
94         assertNotNull(chart);
95         assertThat(chart.getNamespace()).isEqualTo(charts.get(0).getNamespace());
96     }
97
98     @Test
99     void test_saveChart() throws IOException, ServiceException {
100         doThrow(IOException.class).when(chartStore).saveChart(charts.get(0), null, null);
101         assertThatThrownBy(() -> chartService.saveChart(charts.get(0), null, null))
102             .isInstanceOf(IOException.class);
103
104         MockMultipartFile mockChartFile = new MockMultipartFile("chart", "dummy".getBytes());
105         MockMultipartFile mockOverrideFile = new MockMultipartFile("override", "dummy".getBytes());
106
107         doReturn(charts.get(0)).when(chartStore).saveChart(any(), any(), any());
108
109         ChartInfo chart = chartService.saveChart(charts.get(0), mockChartFile, mockOverrideFile);
110         assertNotNull(chart);
111         assertThat(chart.getChartId().getName()).isEqualTo(charts.get(0).getChartId().getName());
112
113     }
114
115     @Test
116     void test_installChart() throws IOException, ServiceException {
117         assertDoesNotThrow(() -> chartService.installChart(charts.get(0)));
118         doThrow(ServiceException.class).when(helmClient).installChart(any());
119         assertThatThrownBy(() -> chartService.installChart(charts.get(0))).isInstanceOf(ServiceException.class);
120
121         doReturn("dummyRepoName").when(chartService).findChartRepo(any());
122         doNothing().when(helmClient).installChart(any());
123         chartService.installChart(charts.get(1));
124         assertEquals("dummyRepoName", charts.get(1).getRepository().getRepoName());
125
126         ChartInfo testChart = charts.get(1);
127         testChart.setRepository(null);
128         doReturn(null).when(chartService).findChartRepo(any());
129         chartService.installChart(charts.get(1));
130     }
131
132     @Test
133     void test_UninstallChart() throws ServiceException {
134         assertDoesNotThrow(() -> chartService.uninstallChart(charts.get(0)));
135         doThrow(ServiceException.class).when(helmClient).uninstallChart(any());
136         assertThatThrownBy(() -> chartService.uninstallChart(charts.get(0))).isInstanceOf(ServiceException.class);
137     }
138
139     @Test
140     void test_findChartRepo() throws IOException, ServiceException {
141         assertDoesNotThrow(() -> chartService.findChartRepo(charts.get(0)));
142         doReturn("dummyRepoName").when(helmClient).findChartRepository(any());
143         assertEquals("dummyRepoName", chartService.findChartRepo(charts.get(1)));
144
145         doThrow(ServiceException.class).when(helmClient).findChartRepository(any());
146         assertThatThrownBy(() -> chartService.findChartRepo(charts.get(0))).isInstanceOf(ServiceException.class);
147     }
148 }