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.acm.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.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
30 import java.io.IOException;
31 import java.nio.file.Path;
32 import java.util.List;
33 import org.junit.jupiter.api.AfterEach;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.junit.jupiter.MockitoExtension;
41 import org.mockito.junit.jupiter.MockitoSettings;
42 import org.mockito.quality.Strictness;
43 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
44 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
45 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList;
46 import org.onap.policy.clamp.acm.participant.kubernetes.parameters.ParticipantK8sParameters;
47 import org.onap.policy.common.utils.coder.Coder;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 import org.springframework.mock.web.MockMultipartFile;
52 import org.springframework.util.FileSystemUtils;
55 @ExtendWith(MockitoExtension.class)
56 @MockitoSettings(strictness = Strictness.LENIENT)
57 class ChartStoreTest {
59 private static final Coder CODER = new StandardCoder();
60 private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
61 private static List<ChartInfo> charts;
64 private ParticipantK8sParameters parameters;
66 private ChartStore chartStore;
70 static void init() throws CoderException {
71 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
74 //Overriding the local chart dir parameter to a temp folder under target for testing java FILE IO operations.
77 Mockito.doReturn("target/tmp/").when(parameters).getLocalChartDirectory();
78 Mockito.doReturn("info.json").when(parameters).getInfoFileName();
79 chartStore = new ChartStore(parameters);
82 //Clean up the 'tmp' dir after each test case.
84 void cleanUp() throws IOException {
85 FileSystemUtils.deleteRecursively(Path.of(parameters.getLocalChartDirectory()));
86 chartStore.getLocalChartMap().clear();
90 void test_getHelmChartFile() {
91 File file = chartStore.getHelmChartFile(charts.get(0));
93 assertThat(file.getPath()).endsWith(charts.get(0).getChartId().getName());
97 void test_getOverrideFile() {
98 File file = chartStore.getOverrideFile(charts.get(0));
100 assertThat(file.getPath()).endsWith("values.yaml");
104 void test_saveChart() throws IOException, ServiceException {
105 MockMultipartFile mockChartFile = new MockMultipartFile("chart", "dummy".getBytes());
106 MockMultipartFile mockOverrideFile = new MockMultipartFile("override", "dummy".getBytes());
107 ChartInfo testChart = charts.get(0);
108 testChart.setChartId(new ToscaConceptIdentifier("testChart", "1.0.0"));
109 ChartInfo result = chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile);
111 assertThat(result.getChartId().getName()).isEqualTo("testChart");
112 assertThat(chartStore.getLocalChartMap()).hasSize(1);
114 assertThatThrownBy(() -> chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile))
115 .isInstanceOf(ServiceException.class);
120 void test_getChart() {
121 assertNull(chartStore.getChart(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion()));
122 chartStore.getLocalChartMap().put(charts.get(0).getChartId().getName() + "_" + charts.get(0).getChartId()
123 .getVersion(), charts.get(0));
124 ChartInfo chart = chartStore.getChart(charts.get(0).getChartId().getName(),
125 charts.get(0).getChartId().getVersion());
126 assertThat(chart.getChartId().getName()).isEqualTo(charts.get(0).getChartId().getName());
130 void test_getAllChart() {
131 // When the chart store is empty before adding any charts
132 assertThat(chartStore.getAllCharts()).isEmpty();
134 for (ChartInfo chart : charts) {
135 chartStore.getLocalChartMap().put(chart.getChartId().getName() + "_" + chart.getChartId().getVersion(),
138 List<ChartInfo> retrievedChartList = chartStore.getAllCharts();
139 assertThat(retrievedChartList).isNotEmpty();
140 assertThat(retrievedChartList.size()).isEqualTo(charts.size());
144 void test_deleteChart() {
145 chartStore.getLocalChartMap().put(charts.get(0).getChartId().getName() + "_" + charts.get(0).getChartId()
146 .getVersion(), charts.get(0));
147 assertThat(chartStore.getLocalChartMap()).hasSize(1);
148 chartStore.deleteChart(charts.get(0));
149 assertThat(chartStore.getLocalChartMap()).isEmpty();
153 void test_getAppPath() {
154 Path path = chartStore.getAppPath(charts.get(0).getChartId());
156 assertThat(path.toString()).endsWith(charts.get(0).getChartId().getVersion());
157 assertThat(path.toString()).startsWith("target");
161 void test_chartSoreInstantiationWithExistingChartFiles() throws IOException, ServiceException {
162 MockMultipartFile mockChartFile = new MockMultipartFile("HelmChartFile", "dummyData".getBytes());
163 MockMultipartFile mockOverrideFile = new MockMultipartFile("overrideFile.yaml", "dummyData".getBytes());
164 ChartInfo testChart = charts.get(0);
165 testChart.setChartId(new ToscaConceptIdentifier("dummyChart", "1.0.0"));
167 //Creating a dummy chart in local dir.
168 chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile);
170 //Instantiating a new chartStore object with pre available chart in local.
171 ChartStore chartStore2 = new ChartStore(parameters);
172 assertThat(chartStore2.getLocalChartMap()).hasSize(1).containsKey("dummyChart_" + charts.get(0).getChartId()