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.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 import org.springframework.mock.web.MockMultipartFile;
51 import org.springframework.util.FileSystemUtils;
54 @ExtendWith(MockitoExtension.class)
55 @MockitoSettings(strictness = Strictness.LENIENT)
56 class ChartStoreTest {
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;
63 private ParticipantK8sParameters parameters;
65 private ChartStore chartStore;
69 static void init() throws CoderException {
70 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
73 //Overriding the local chart dir parameter to a temp folder under target for testing java FILE IO operations.
76 Mockito.doReturn("target/tmp/").when(parameters).getLocalChartDirectory();
77 Mockito.doReturn("info.json").when(parameters).getInfoFileName();
78 chartStore = new ChartStore(parameters);
81 //Clean up the 'tmp' dir after each test case.
83 void cleanUp() throws IOException {
84 FileSystemUtils.deleteRecursively(Path.of(parameters.getLocalChartDirectory()));
85 chartStore.getLocalChartMap().clear();
89 void test_getHelmChartFile() {
90 File file = chartStore.getHelmChartFile(charts.get(0));
92 assertThat(file.getPath()).endsWith(charts.get(0).getChartId().getName());
96 void test_getOverrideFile() {
97 File file = chartStore.getOverrideFile(charts.get(0));
99 assertThat(file.getPath()).endsWith("values.yaml");
103 void test_saveChart() throws IOException, ServiceException {
104 MockMultipartFile mockChartFile = new MockMultipartFile("chart", "dummy".getBytes());
105 MockMultipartFile mockOverrideFile = new MockMultipartFile("override", "dummy".getBytes());
106 ChartInfo testChart = charts.get(0);
107 testChart.setChartId(new ToscaConceptIdentifier("testChart", "1.0.0"));
108 ChartInfo result = chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile);
110 assertThat(result.getChartId().getName()).isEqualTo("testChart");
111 assertThat(chartStore.getLocalChartMap()).hasSize(1);
113 assertThatThrownBy(() -> chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile))
114 .isInstanceOf(ServiceException.class);
119 void test_getChart() {
120 assertNull(chartStore.getChart(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion()));
121 chartStore.getLocalChartMap().put(charts.get(0).getChartId().getName() + "_" + charts.get(0).getChartId()
122 .getVersion(), charts.get(0));
123 ChartInfo chart = chartStore.getChart(charts.get(0).getChartId().getName(),
124 charts.get(0).getChartId().getVersion());
125 assertThat(chart.getChartId().getName()).isEqualTo(charts.get(0).getChartId().getName());
129 void test_getAllChart() {
130 // When the chart store is empty before adding any charts
131 assertThat(chartStore.getAllCharts()).isEmpty();
133 for (ChartInfo chart : charts) {
134 chartStore.getLocalChartMap().put(chart.getChartId().getName() + "_" + chart.getChartId().getVersion(),
137 List<ChartInfo> retrievedChartList = chartStore.getAllCharts();
138 assertThat(retrievedChartList).isNotEmpty();
139 assertThat(retrievedChartList.size()).isEqualTo(charts.size());
143 void test_deleteChart() {
144 chartStore.getLocalChartMap().put(charts.get(0).getChartId().getName() + "_" + charts.get(0).getChartId()
145 .getVersion(), charts.get(0));
146 assertThat(chartStore.getLocalChartMap()).hasSize(1);
147 chartStore.deleteChart(charts.get(0));
148 assertThat(chartStore.getLocalChartMap()).isEmpty();
152 void test_getAppPath() {
153 Path path = chartStore.getAppPath(charts.get(0).getChartId());
155 assertThat(path.toString()).endsWith(charts.get(0).getChartId().getVersion());
156 assertThat(path.toString()).startsWith("target");
160 void test_chartSoreInstantiationWithExistingChartFiles() throws IOException, ServiceException {
161 MockMultipartFile mockChartFile = new MockMultipartFile("HelmChartFile", "dummyData".getBytes());
162 MockMultipartFile mockOverrideFile = new MockMultipartFile("overrideFile.yaml", "dummyData".getBytes());
163 ChartInfo testChart = charts.get(0);
164 testChart.setChartId(new ToscaConceptIdentifier("dummyChart", "1.0.0"));
166 //Creating a dummy chart in local dir.
167 chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile);
169 //Instantiating a new chartStore object with pre available chart in local.
170 ChartStore chartStore2 = new ChartStore(parameters);
171 assertThat(chartStore2.getLocalChartMap()).hasSize(1).containsKey("dummyChart_" + charts.get(0).getChartId()