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.assertNull;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import java.io.IOException;
30 import java.nio.file.Path;
31 import java.util.List;
32 import org.junit.jupiter.api.AfterEach;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.junit.jupiter.MockitoExtension;
40 import org.mockito.junit.jupiter.MockitoSettings;
41 import org.mockito.quality.Strictness;
42 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
43 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
44 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
45 import org.onap.policy.clamp.controlloop.participant.kubernetes.parameters.ParticipantK8sParameters;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.springframework.mock.web.MockMultipartFile;
50 import org.springframework.util.FileSystemUtils;
53 @ExtendWith(MockitoExtension.class)
54 @MockitoSettings(strictness = Strictness.LENIENT)
55 class ChartStoreTest {
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;
62 private ParticipantK8sParameters parameters;
64 private ChartStore chartStore;
68 static void init() throws CoderException {
69 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
72 //Overriding the local chart dir parameter to a temp folder under target for testing java FILE IO operations.
75 Mockito.doReturn("target/tmp/").when(parameters).getLocalChartDirectory();
76 Mockito.doReturn("info.json").when(parameters).getInfoFileName();
77 chartStore = new ChartStore(parameters);
80 //Clean up the 'tmp' dir after each test case.
82 void cleanUp() throws IOException {
83 FileSystemUtils.deleteRecursively(Path.of(parameters.getLocalChartDirectory()));
84 chartStore.getLocalChartMap().clear();
88 void test_getHelmChartFile() {
89 File file = chartStore.getHelmChartFile(charts.get(0));
91 assertThat(file.getPath()).endsWith(charts.get(0).getChartName());
95 void test_getOverrideFile() {
96 File file = chartStore.getOverrideFile(charts.get(0));
98 assertThat(file.getPath()).endsWith("values.yaml");
102 void test_saveChart() throws IOException, ServiceException {
103 MockMultipartFile mockChartFile = new MockMultipartFile("chart", "dummy".getBytes());
104 MockMultipartFile mockOverrideFile = new MockMultipartFile("override", "dummy".getBytes());
105 ChartInfo testChart = charts.get(0);
106 testChart.setChartName("testChart");
107 ChartInfo result = chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile);
109 assertThat(result.getChartName()).isEqualTo("testChart");
110 assertThat(chartStore.getLocalChartMap()).hasSize(1);
112 assertThatThrownBy(() -> chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile))
113 .isInstanceOf(ServiceException.class);
118 void test_getChart() {
119 assertNull(chartStore.getChart(charts.get(0).getChartName(), charts.get(0).getVersion()));
120 chartStore.getLocalChartMap().put(charts.get(0).getChartName() + "_" + charts.get(0).getVersion(),
122 ChartInfo chart = chartStore.getChart(charts.get(0).getChartName(), charts.get(0).getVersion());
123 assertThat(chart.getChartName()).isEqualTo(charts.get(0).getChartName());
127 void test_getAllChart() {
128 // When the chart store is empty before adding any charts
129 assertThat(chartStore.getAllCharts()).isEmpty();
131 for (ChartInfo chart : charts) {
132 chartStore.getLocalChartMap().put(chart.getChartName() + "_" + chart.getVersion(), chart);
134 List<ChartInfo> retrievedChartList = chartStore.getAllCharts();
135 assertThat(retrievedChartList).isNotEmpty();
136 assertThat(retrievedChartList.size()).isEqualTo(charts.size());
140 void test_deleteChart() {
141 chartStore.getLocalChartMap().put(charts.get(0).getChartName() + "_" + charts.get(0).getVersion(),
143 assertThat(chartStore.getLocalChartMap()).hasSize(1);
144 chartStore.deleteChart(charts.get(0));
145 assertThat(chartStore.getLocalChartMap()).isEmpty();
149 void test_getAppPath() {
150 Path path = chartStore.getAppPath(charts.get(0).getChartName(), charts.get(0).getVersion());
152 assertThat(path.toString()).endsWith(charts.get(0).getVersion());
153 assertThat(path.toString()).startsWith("target");
157 void test_chartSoreInstantiationWithExistingChartFiles() throws IOException, ServiceException {
158 MockMultipartFile mockChartFile = new MockMultipartFile("HelmChartFile", "dummyData".getBytes());
159 MockMultipartFile mockOverrideFile = new MockMultipartFile("overrideFile.yaml", "dummyData".getBytes());
160 ChartInfo testChart = charts.get(0);
161 testChart.setChartName("dummyChart");
163 //Creating a dummy chart in local dir.
164 chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile);
166 //Instantiating a new chartStore object with pre available chart in local.
167 ChartStore chartStore2 = new ChartStore(parameters);
168 assertThat(chartStore2.getLocalChartMap()).hasSize(1).containsKey("dummyChart_" + charts.get(0).getVersion());