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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.kubernetes.service;
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;
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;
54 @ExtendWith(SpringExtension.class)
55 class ChartServiceTest {
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;
63 private ChartService chartService = new ChartService();
66 private ChartStore chartStore;
69 private HelmClient helmClient;
72 static void init() throws CoderException {
73 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
77 void test_getAllCharts() {
78 assertThat(chartService.getAllCharts()).isEmpty();
80 doReturn(charts).when(chartStore).getAllCharts();
81 Collection<ChartInfo> result = chartService.getAllCharts();
82 assertNotNull(result);
83 assertThat(result).containsAll(charts);
87 void test_getChart() {
88 assertNull(chartService.getChart("dummyName", "dummyversion"));
90 doReturn(charts.get(0)).when(chartStore).getChart(any(), any());
91 ChartInfo chart = chartService.getChart(charts.get(0).getChartId().getName(),
92 charts.get(0).getChartId().getVersion());
94 assertThat(chart.getNamespace()).isEqualTo(charts.get(0).getNamespace());
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);
103 MockMultipartFile mockChartFile = new MockMultipartFile("chart", "dummy".getBytes());
104 MockMultipartFile mockOverrideFile = new MockMultipartFile("override", "dummy".getBytes());
106 doReturn(charts.get(0)).when(chartStore).saveChart(any(), any(), any());
108 ChartInfo chart = chartService.saveChart(charts.get(0), mockChartFile, mockOverrideFile);
109 assertNotNull(chart);
110 assertThat(chart.getChartId().getName()).isEqualTo(charts.get(0).getChartId().getName());
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);
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());
125 ChartInfo testChart = charts.get(1);
126 testChart.setRepository(null);
127 doReturn(null).when(chartService).findChartRepo(any());
128 chartService.installChart(charts.get(1));
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);
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)));
144 doThrow(ServiceException.class).when(helmClient).findChartRepository(any());
145 assertThatThrownBy(() -> chartService.findChartRepo(charts.get(0))).isInstanceOf(ServiceException.class);