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 / Utils.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 com.fasterxml.jackson.databind.ObjectMapper;
22 import lombok.extern.slf4j.Slf4j;
23 import org.apache.commons.io.FileUtils;
24 import org.springframework.stereotype.Component;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.util.Map;
31
32 /**
33  * An utility class for various file related tasks.
34  * @author Dhrumin Desai
35  */
36 @Slf4j
37 @Component
38 public class Utils {
39
40     private final ObjectMapper MAPPER = new ObjectMapper();
41
42     public <T> T deserializeJsonFileToModel(String filePath, Class<T> modelClass) {
43         return deserializeJsonFileToModel(new File(filePath), modelClass);
44     }
45
46     /**
47      * maps json file to a model class
48      * @param file Json file which holds the data
49      * @param modelClass target model class for mapping
50      * @return mapped model instance
51      */
52     public <T> T deserializeJsonFileToModel(File file, Class<T> modelClass) {
53         try {
54             return MAPPER.readValue(file, modelClass);
55         } catch (IOException e) {
56             log.error(e.getMessage());
57             throw new RuntimeException("Error occurred while converting file to the given model class");
58         }
59     }
60
61     /**
62      * copies dir/file to a temp location on OS
63      * @param srcLocation
64      * @return
65      */
66     public File cloneFileToTempLocation(String srcLocation) {
67         File cloneLocation = null;
68         try {
69             Path tempRootDir = Files.createTempDirectory("chart");
70             cloneLocation = new File(tempRootDir.toString());
71             log.info("cloning dir/file at : " + tempRootDir);
72             FileUtils.copyDirectory(new File(srcLocation), cloneLocation);
73         } catch (IOException e) {
74             log.error(e.getMessage());
75             throw new RuntimeException("Error occured while cloning file to temp location.");
76         }
77         return cloneLocation;
78     }
79
80     /**
81      * deletes dir / file from temp location of OS
82      * @param dir  dir to be deleted
83      */
84     public void deleteTempFileLocation(File dir) {
85         try {
86             FileUtils.deleteDirectory(dir);
87         } catch (IOException e) {
88             log.warn("Could not delete dir/file: " + dir.getAbsolutePath());
89         }
90     }
91
92     /**
93      * puts value into a map if only exists
94      * @param map a map
95      * @param key a key
96      * @param value a value
97      */
98     public void putIfNotNull(Map<String, Object> map, String key, Object value){
99         if(value != null){
100             map.put(key, value);
101         }
102     }
103
104 }
105