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 / ComponentSpecValidatorImpl.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 lombok.extern.slf4j.Slf4j;
22 import org.apache.commons.io.FileUtils;
23 import org.everit.json.schema.Schema;
24 import org.everit.json.schema.loader.SchemaLoader;
25 import org.json.JSONObject;
26 import org.json.JSONTokener;
27 import org.onap.dcaegen2.platform.helmchartgenerator.Utils;
28 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.base.ComponentSpec;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.core.io.ClassPathResource;
31 import org.springframework.stereotype.Service;
32
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileNotFoundException;
36 import java.io.IOException;
37 import java.io.InputStream;
38
39 /**
40  * A class for Component specification validation.
41  * @author Dhrumin Desai
42  */
43 @Service
44 @Slf4j
45 public class ComponentSpecValidatorImpl implements ComponentSpecValidator {
46
47     @Autowired
48     private Utils utils;
49
50     public ComponentSpecValidatorImpl(Utils utils) {
51         this.utils = utils;
52     }
53
54     /**
55      * Validates the spec json file  against schema and prints errors if found
56      * @param specFileLocation specification json file location
57      * @param specSchemaLocation json schema file location
58      * @throws IOException
59      */
60     @Override
61     public void validateSpecFile(String specFileLocation, String specSchemaLocation) throws IOException {
62         File schemaFile = getSchemaFile(specSchemaLocation);
63         ComponentSpec cs = utils.deserializeJsonFileToModel(specFileLocation, ComponentSpec.class);
64         validateSpecSchema(new File(specFileLocation), schemaFile);
65         validateHelmRequirements(cs);
66     }
67
68     private File getSchemaFile(String specSchemaLocation) throws IOException {
69         if(specSchemaLocation.isEmpty()){
70             return defaultSchemaFile();
71         }
72         else{
73             return new File(specSchemaLocation);
74         }
75     }
76
77     private File defaultSchemaFile() throws IOException {
78         File schemaFile = File.createTempFile("schema", ".json");
79         InputStream inputStream = new ClassPathResource("component-spec-schema.json").getInputStream();
80         FileUtils.copyInputStreamToFile(inputStream, schemaFile);
81         return schemaFile;
82     }
83
84     private void validateSpecSchema(File specFile, File schemaFile) {
85         try {
86             JSONTokener schemaData = new JSONTokener(new FileInputStream(schemaFile));
87             JSONObject jsonSchema = new JSONObject(schemaData);
88
89             JSONTokener jsonDataFile = new JSONTokener(new FileInputStream(specFile));
90             JSONObject jsonObject = new JSONObject(jsonDataFile);
91
92             Schema schemaValidator = SchemaLoader.load(jsonSchema);
93             schemaValidator.validate(jsonObject);
94         } catch (FileNotFoundException e) {
95             log.error(e.getMessage());
96             throw new RuntimeException("Specfile or Schemafile not found.");
97         }
98     }
99
100     private void validateHelmRequirements(ComponentSpec cs) {
101         checkHealthCheckSection(cs);
102         checkHelmSection(cs);
103     }
104
105     private void checkHealthCheckSection(ComponentSpec cs) {
106         if(cs.getAuxilary().getHealthcheck().getPort() == null) {
107             throw new RuntimeException("port in healthcheck section is a required field but was not found");
108         }
109     }
110
111     private void checkHelmSection(ComponentSpec cs) {
112         if(cs.getAuxilary().getHelm() == null){
113             throw new RuntimeException("helm section in component spec is required but was not found");
114         }
115     }
116 }