2d05a7a0e406f0509af446a70b5b3fd99e6df0f8
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.kubernetes.service;
22
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;
27
28 import java.io.File;
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;
51
52
53 @ExtendWith(MockitoExtension.class)
54 @MockitoSettings(strictness = Strictness.LENIENT)
55 class ChartStoreTest {
56
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;
60
61     @Mock
62     private ParticipantK8sParameters parameters;
63
64     private ChartStore chartStore;
65
66
67     @BeforeAll
68     static void init() throws CoderException {
69         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
70     }
71
72     //Overriding the local chart dir parameter to a temp folder under target for testing java FILE IO operations.
73     @BeforeEach
74     void setup() {
75         Mockito.doReturn("target/tmp/").when(parameters).getLocalChartDirectory();
76         Mockito.doReturn("info.json").when(parameters).getInfoFileName();
77         chartStore = new ChartStore(parameters);
78     }
79
80     //Clean up the 'tmp' dir after each test case.
81     @AfterEach
82     void cleanUp() throws IOException {
83         FileSystemUtils.deleteRecursively(Path.of(parameters.getLocalChartDirectory()));
84         chartStore.getLocalChartMap().clear();
85     }
86
87     @Test
88     void test_getHelmChartFile() {
89         File file = chartStore.getHelmChartFile(charts.get(0));
90         assertNotNull(file);
91         assertThat(file.getPath()).endsWith(charts.get(0).getChartName());
92     }
93
94     @Test
95     void test_getOverrideFile() {
96         File file = chartStore.getOverrideFile(charts.get(0));
97         assertNotNull(file);
98         assertThat(file.getPath()).endsWith("values.yaml");
99     }
100
101     @Test
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);
108
109         assertThat(result.getChartName()).isEqualTo("testChart");
110         assertThat(chartStore.getLocalChartMap()).hasSize(1);
111
112         assertThatThrownBy(() -> chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile))
113             .isInstanceOf(ServiceException.class);
114     }
115
116
117     @Test
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(),
121             charts.get(0));
122         ChartInfo chart = chartStore.getChart(charts.get(0).getChartName(), charts.get(0).getVersion());
123         assertThat(chart.getChartName()).isEqualTo(charts.get(0).getChartName());
124     }
125
126     @Test
127     void test_getAllChart() {
128         // When the chart store is empty before adding any charts
129         assertThat(chartStore.getAllCharts()).isEmpty();
130
131         for (ChartInfo chart : charts) {
132             chartStore.getLocalChartMap().put(chart.getChartName() + "_" + chart.getVersion(), chart);
133         }
134         List<ChartInfo> retrievedChartList = chartStore.getAllCharts();
135         assertThat(retrievedChartList).isNotEmpty();
136         assertThat(retrievedChartList.size()).isEqualTo(charts.size());
137     }
138
139     @Test
140     void test_deleteChart() {
141         chartStore.getLocalChartMap().put(charts.get(0).getChartName() + "_" + charts.get(0).getVersion(),
142             charts.get(0));
143         assertThat(chartStore.getLocalChartMap()).hasSize(1);
144         chartStore.deleteChart(charts.get(0));
145         assertThat(chartStore.getLocalChartMap()).isEmpty();
146     }
147
148     @Test
149     void test_getAppPath() {
150         Path path = chartStore.getAppPath(charts.get(0).getChartName(), charts.get(0).getVersion());
151         assertNotNull(path);
152         assertThat(path.toString()).endsWith(charts.get(0).getVersion());
153         assertThat(path.toString()).startsWith("target");
154     }
155
156     @Test
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");
162
163         //Creating a dummy chart in local dir.
164         chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile);
165
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());
169     }
170 }