updated dependencies to 9.x-0 and added ServiceAccount.nameOverride substitution...
[dcaegen2/platform.git] / mod2 / helm-generator / helmchartgenerator-core / src / test / java / org / onap / dcaegen2 / platform / helmchartgenerator / KeyValueMergerTest.java
1 /*
2  * # ============LICENSE_START=======================================================
3  * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
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  * # ============LICENSE_END=========================================================
17  */
18
19 package org.onap.dcaegen2.platform.helmchartgenerator;
20
21 import org.apache.commons.io.FileUtils;
22 import org.assertj.core.api.Assertions;
23 import org.junit.jupiter.api.AfterEach;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder.KeyValueMerger;
31 import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.ChartInfo;
32 import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.Metadata;
33 import org.yaml.snakeyaml.Yaml;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.PrintWriter;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.util.Arrays;
42 import java.util.HashMap;
43 import java.util.Map;
44
45 import static org.mockito.ArgumentMatchers.any;
46
47 @ExtendWith(MockitoExtension.class)
48 class KeyValueMergerTest {
49
50     private KeyValueMerger merger;
51
52     @Mock
53     private Yaml yamlHelper;
54
55     private File chartDir;
56
57     @BeforeEach
58     void setUp() {
59         merger = new KeyValueMerger(yamlHelper);
60     }
61
62     @Test
63     void mergeValuesToChart() throws IOException {
64         ChartInfo chartInfo = prepareChartInfo();
65         chartDir = prepareChartDir();
66
67         Mockito.when(yamlHelper.load(any(InputStream.class))).thenReturn(new HashMap<String, Object>());
68
69         merger.mergeValuesToChart(chartInfo, chartDir);
70         Mockito.verify(yamlHelper, Mockito.times(2)).dump(any(HashMap.class), any(PrintWriter.class));
71     }
72
73     @Test
74     void testServiceAccountNameOverrride() throws Exception{
75         ChartInfo chartInfo = prepareChartInfo();
76         chartDir = prepareChartDir();
77         HashMap<String, Object> valuesKvWithSA = getValuesKvWithSA();
78         Mockito.when(yamlHelper.load(any(InputStream.class))).thenReturn(valuesKvWithSA);
79
80         merger.mergeValuesToChart(chartInfo, chartDir);
81
82         Map<String, Object> serviceAccountKv = (Map<String, Object>) valuesKvWithSA.get("serviceAccount");
83         Assertions.assertThat(serviceAccountKv.get("nameOverride")).isEqualTo("someComponent");
84     }
85
86     private HashMap<String, Object> getValuesKvWithSA() {
87         HashMap<String, Object> innerKV = new HashMap<>();
88         innerKV.put("nameOverride", "TBD");
89         innerKV.put("roles", Arrays.asList("read"));
90
91         HashMap<String, Object> KV = new HashMap<>();
92         KV.put("serviceAccount", innerKV);
93         return KV;
94     }
95
96     @AfterEach
97     void tearDown(){
98         FileUtils.deleteQuietly(chartDir);
99     }
100
101     private File prepareChartDir() throws IOException {
102         final Path chartDir = Files.createTempDirectory("chartDir");
103         Files.createFile(chartDir.resolve("Chart.yaml"));
104         Files.createFile(chartDir.resolve("values.yaml"));
105         return chartDir.toFile();
106     }
107
108     private ChartInfo prepareChartInfo() {
109         ChartInfo chartInfo = new ChartInfo();
110
111         Metadata metadata = new Metadata();
112         metadata.setName("someComponent");
113         metadata.setVersion("someVersion");
114         metadata.setDescription("someDescription");
115
116         Map<String, Object> values = new HashMap<>();
117         values.put("someKey", "someValue");
118
119         chartInfo.setMetadata(metadata);
120         chartInfo.setValues(values);
121
122         return chartInfo;
123     }
124 }
125