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