029db999418a8c60bef15637588c20107084f2ee
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.acm.participant.kubernetes.service;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertNull;
28
29 import java.io.File;
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;
53
54
55 @ExtendWith(MockitoExtension.class)
56 @MockitoSettings(strictness = Strictness.LENIENT)
57 class ChartStoreTest {
58
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;
62
63     @Mock
64     private ParticipantK8sParameters parameters;
65
66     private ChartStore chartStore;
67
68
69     @BeforeAll
70     static void init() throws CoderException {
71         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
72     }
73
74     //Overriding the local chart dir parameter to a temp folder under target for testing java FILE IO operations.
75     @BeforeEach
76     void setup() {
77         Mockito.doReturn("target/tmp/").when(parameters).getLocalChartDirectory();
78         Mockito.doReturn("info.json").when(parameters).getInfoFileName();
79         chartStore = new ChartStore(parameters);
80     }
81
82     //Clean up the 'tmp' dir after each test case.
83     @AfterEach
84     void cleanUp() throws IOException {
85         FileSystemUtils.deleteRecursively(Path.of(parameters.getLocalChartDirectory()));
86         chartStore.getLocalChartMap().clear();
87     }
88
89     @Test
90     void test_getHelmChartFile() {
91         File file = chartStore.getHelmChartFile(charts.get(0));
92         assertNotNull(file);
93         assertThat(file.getPath()).endsWith(charts.get(0).getChartId().getName());
94     }
95
96     @Test
97     void test_getOverrideFile() {
98         File file = chartStore.getOverrideFile(charts.get(0));
99         assertNotNull(file);
100         assertThat(file.getPath()).endsWith("values.yaml");
101     }
102
103     @Test
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);
110
111         assertThat(result.getChartId().getName()).isEqualTo("testChart");
112         assertThat(chartStore.getLocalChartMap()).hasSize(1);
113
114         assertThatThrownBy(() -> chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile))
115             .isInstanceOf(ServiceException.class);
116     }
117
118
119     @Test
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());
127     }
128
129     @Test
130     void test_getAllChart() {
131         // When the chart store is empty before adding any charts
132         assertThat(chartStore.getAllCharts()).isEmpty();
133
134         for (ChartInfo chart : charts) {
135             chartStore.getLocalChartMap().put(chart.getChartId().getName() + "_" + chart.getChartId().getVersion(),
136                 chart);
137         }
138         List<ChartInfo> retrievedChartList = chartStore.getAllCharts();
139         assertThat(retrievedChartList).isNotEmpty();
140         assertThat(retrievedChartList.size()).isEqualTo(charts.size());
141     }
142
143     @Test
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();
150     }
151
152     @Test
153     void test_getAppPath() {
154         Path path = chartStore.getAppPath(charts.get(0).getChartId());
155         assertNotNull(path);
156         assertThat(path.toString()).endsWith(charts.get(0).getChartId().getVersion());
157         assertThat(path.toString()).startsWith("target");
158     }
159
160     @Test
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"));
166
167         //Creating a dummy chart in local dir.
168         chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile);
169
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()
173             .getVersion());
174     }
175 }