Improved helm-generator code to make it more testable and improved code coverage
[dcaegen2/platform.git] / mod2 / helm-generator / helmchartgenerator-core / src / main / java / org / onap / dcaegen2 / platform / helmchartgenerator / validation / ChartTemplateStructureValidatorImpl.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.validation;
20
21 import org.springframework.stereotype.Component;
22
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26
27 /**
28  * A class to validate structure of the base helm directory
29  */
30 @Component
31 public class ChartTemplateStructureValidatorImpl implements ChartTemplateStructureValidator {
32
33     /**
34      * validates base helm chart directory and throws error if the structure is not proper.
35      * @param chartTemplateLocation base helm chart dir location
36      */
37     @Override
38     public void validateChartTemplateStructure(String chartTemplateLocation) {
39         checkBaseDirectory(chartTemplateLocation);
40     }
41
42     private void checkBaseDirectory(String chartTemplateLocation) {
43         Path base = Paths.get(chartTemplateLocation, "base");
44         Path templates = Paths.get(chartTemplateLocation, "base/templates");
45         Path chart = Paths.get(chartTemplateLocation, "base/Chart.yaml");
46         Path values = Paths.get(chartTemplateLocation, "base/values.yaml");
47         if(!Files.exists(base)){
48             throw new RuntimeException("base directory not found in chart template location");
49         }
50         if(!Files.exists(templates)){
51             throw new RuntimeException("templates directory not found in base directory");
52         }
53         if(!Files.exists(chart)){
54             throw new RuntimeException("chart.yaml not found in base directory");
55         }
56         if(!Files.exists(values)){
57             throw new RuntimeException("values.yaml not found in base directory");
58         }
59     }
60 }