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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.participant.kubernetes.service;
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;
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.controlloop.participant.kubernetes.exception.ServiceException;
46 import org.onap.policy.clamp.controlloop.participant.kubernetes.helm.HelmClient;
47 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
48 import org.onap.policy.clamp.controlloop.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;
55 @ExtendWith(SpringExtension.class)
56 class ChartServiceTest {
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;
64 private ChartService chartService = new ChartService();
67 private ChartStore chartStore;
70 private HelmClient helmClient;
73 static void init() throws CoderException {
74 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
78 void test_getAllCharts() {
79 assertThat(chartService.getAllCharts()).isEmpty();
81 doReturn(charts).when(chartStore).getAllCharts();
82 Collection<ChartInfo> result = chartService.getAllCharts();
83 assertNotNull(result);
84 assertThat(result).containsAll(charts);
88 void test_getChart() {
89 assertNull(chartService.getChart("dummyName", "dummyversion"));
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());
95 assertThat(chart.getNamespace()).isEqualTo(charts.get(0).getNamespace());
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);
104 MockMultipartFile mockChartFile = new MockMultipartFile("chart", "dummy".getBytes());
105 MockMultipartFile mockOverrideFile = new MockMultipartFile("override", "dummy".getBytes());
107 doReturn(charts.get(0)).when(chartStore).saveChart(any(), any(), any());
109 ChartInfo chart = chartService.saveChart(charts.get(0), mockChartFile, mockOverrideFile);
110 assertNotNull(chart);
111 assertThat(chart.getChartId().getName()).isEqualTo(charts.get(0).getChartId().getName());
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);
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());
126 ChartInfo testChart = charts.get(1);
127 testChart.setRepository(null);
128 doReturn(null).when(chartService).findChartRepo(any());
129 chartService.installChart(charts.get(1));
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);
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)));
145 doThrow(ServiceException.class).when(helmClient).findChartRepository(any());
146 assertThatThrownBy(() -> chartService.findChartRepo(charts.get(0))).isInstanceOf(ServiceException.class);