Add sdc startup in IT
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / backend / ci / tests / utils / general / FileHandling.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.backend.ci.tests.utils.general;
22
23 import static org.testng.AssertJUnit.assertTrue;
24
25 import com.aventstack.extentreports.Status;
26 import java.io.BufferedWriter;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileWriter;
30 import java.io.FilenameFilter;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Enumeration;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Properties;
40 import java.util.zip.ZipEntry;
41 import java.util.zip.ZipException;
42 import java.util.zip.ZipFile;
43 import org.apache.commons.io.FileUtils;
44 import org.onap.sdc.backend.ci.tests.datatypes.enums.XnfTypeEnum;
45 import org.openecomp.sdc.be.model.DataTypeDefinition;
46 import org.onap.sdc.backend.ci.tests.api.ComponentBaseTest;
47 import org.openecomp.sdc.common.util.GeneralUtility;
48 import org.yaml.snakeyaml.Yaml;
49
50 public class FileHandling {
51
52 //      ------------------yaml parser methods----------------------------
53         public static Map<?, ?> parseYamlFile(String filePath) throws Exception {
54                 Yaml yaml = new Yaml();
55                 File file = new File(filePath);
56                 InputStream inputStream = new FileInputStream(file);
57                 Map<?, ?> map = (Map<?, ?>) yaml.load(inputStream);
58                 return map;
59         }
60         
61         /**
62          * The method return map fetched objects by pattern from yaml file 
63          * @param yamlFile
64          * @param pattern
65          * @return
66          * @throws Exception
67          */
68         public static Map<String, Object> parseYamlFileToMapByPattern(File yamlFile, String pattern) throws Exception {
69                 Map<?, ?> yamlFileToMap = FileHandling.parseYamlFile(yamlFile.toString());
70                 Map<String, Object> objectMap = getObjectMapByPattern(yamlFileToMap, pattern);
71                 return objectMap;
72         }
73         
74         @SuppressWarnings("unchecked")
75         public static Map<String, Object> getObjectMapByPattern(Map<?, ?> parseUpdetedEnvFile, String pattern) {
76                 Map<String, Object> objectMap = null;
77                 
78                 Object objectUpdetedEnvFile = parseUpdetedEnvFile.get(pattern);
79                 if(objectUpdetedEnvFile instanceof HashMap){
80                         objectMap = (Map<String, Object>) objectUpdetedEnvFile;
81                 }
82                 return objectMap;
83         }
84         
85         
86         public static Map<String, DataTypeDefinition> parseDataTypesYaml(String filePath) throws Exception {
87                 @SuppressWarnings("unchecked")
88                 Map<String, DataTypeDefinition> dataTypesMap = (Map<String, DataTypeDefinition>) parseYamlFile(filePath);
89                 return dataTypesMap;
90         }
91 //      -------------------------------------------------------------------------------------------------
92
93         /**
94          * @param folder, folder name under "Files" folder
95          * @return path to given folder from perspective of working directory or sdc-vnfs repository
96          */
97         public static String getFilePath(String folder) {
98                 // return folder from perspective of working directory ( in general for nightly run from Linux, should already contain "Files" directory )
99                 return FileHandling.getBasePath() + "src/test/resources/Files" + File.separator + folder + File.separator;
100         }
101
102         public static String getBasePath() {
103                 return System.getProperty("user.dir") + File.separator;
104         }
105         
106         public static String getSdcVnfsPath() {
107                 return  getBasePath() + Paths.get("..", "..", "sdc-vnfs").toString();
108         }
109         
110         public static String getDriversPath() {
111                 return getBasePath() + "src" + File.separator + "test" + File.separator + "resources"
112                                 + File.separator + "ci" + File.separator + "drivers" + File.separator;
113         }
114
115         public static String getResourcesFilesPath() {
116                 return getSdcVnfsPath()+ File.separator + "ui-tests" + File.separator + "Files" + File.separator;
117         }
118         
119         public static String getResourcesEnvFilesPath() {
120                 return getBasePath() + File.separator + "src" + File.separator + "test" + File.separator + "resources"
121                                 + File.separator + "Files" + File.separator + "ResourcesEnvFiles" +File.separator;
122         }
123
124         public static String getCiFilesPath() {
125                 return getBasePath() + "src" + File.separator + "test" + File.separator + "resources"
126                                 + File.separator + "ci";
127         }
128
129         public static String getConfFilesPath() {
130                 return getCiFilesPath() + File.separator + "conf" + File.separator;
131         }
132
133         public static String getVnfRepositoryPath() {
134                 return getFilePath("VNFs");
135         }
136
137         public static String getXnfRepositoryPath(XnfTypeEnum xnfTypeEnum) {
138                 return xnfTypeEnum.getValue().equals(XnfTypeEnum.PNF.name()) ? getFilePath("PNFs") : getFilePath("VNFs");
139         }
140
141         public static String getPortMirroringRepositoryPath() {
142                 return getFilePath("PortMirroring");
143         }
144         
145         public static File getConfigFile(String configFileName) throws Exception {
146                 File configFile = new File(FileHandling.getBasePath() + File.separator + "conf" + File.separator + configFileName);
147                 if (!configFile.exists()) {
148                         configFile = new File(FileHandling.getConfFilesPath() + configFileName);
149                 }
150                 return configFile;
151         }
152
153         public static Object[] filterFileNamesFromFolder(String filepath, String extension) {
154                 try {
155                         File dir = new File(filepath);
156                         List<String> filenames = new ArrayList<String>();
157                         
158                         FilenameFilter extensionFilter = new FilenameFilter() {
159                                 public boolean accept(File dir, String name) {
160                                         return name.endsWith(extension);
161                                 }
162                         };
163                         
164                         if (dir.isDirectory()) {
165                                 for (File file : dir.listFiles(extensionFilter)) {
166                                         filenames.add(file.getName());
167                                 }
168                                 return filenames.toArray();
169                         }
170
171                 } catch (Exception e) {
172                         e.printStackTrace();
173                 }
174                 return null;
175         }
176
177         public static List<String> filterFileNamesListFromFolder(String filepath, String extension) {
178                 List<String> filenames = new ArrayList<String>();
179                 try {
180                         File dir = new File(filepath);
181                         
182                         FilenameFilter extensionFilter = new FilenameFilter() {
183                                 public boolean accept(File dir, String name) {
184                                         return name.endsWith(extension);
185                                 }
186                         };
187                         
188                         if (dir.isDirectory()) {
189                                 for (File file : dir.listFiles(extensionFilter)) {
190                                         filenames.add(file.getName());
191                                 }
192                                 return filenames;
193                         }
194
195                 } catch (Exception e) {
196                         e.printStackTrace();
197                 }
198                 return filenames;
199         }
200         
201         public static String[] getArtifactsFromZip(String filepath, String zipFilename){
202                 try {
203                         ZipFile zipFile = new ZipFile(filepath + File.separator + zipFilename);
204                         Enumeration<? extends ZipEntry> entries = zipFile.entries();
205                         
206                         String[] artifactNames = new String[zipFile.size() - 1];
207
208                         int i = 0;
209                         while(entries.hasMoreElements()){
210                                 ZipEntry nextElement = entries.nextElement();
211                                 if (!nextElement.isDirectory()){ 
212                                         if (!nextElement.getName().equals("MANIFEST.json")){
213                                                 String name = nextElement.getName();
214                                                 artifactNames[i++] = name;
215                                         }
216                                 }
217                         }
218                         zipFile.close();
219                         return artifactNames;
220                 } catch(ZipException zipEx) {
221                         System.err.println("Error in zip file named : " +  zipFilename);        
222                         zipEx.printStackTrace();
223                 } catch (IOException e) {
224                         System.err.println("Unhandled exception : ");
225                         e.printStackTrace();
226                 }
227                 
228                 return null;
229                 
230         }
231
232         public static List<String> getFileNamesFromZip(String zipFileLocation){
233                 try{
234                         ZipFile zipFile = new ZipFile(zipFileLocation);
235                         Enumeration<? extends ZipEntry> entries = zipFile.entries();
236                         
237                         List<String> artifactNames = new ArrayList<>();
238
239                         int i = 0;
240                         while(entries.hasMoreElements()){
241                                 ZipEntry nextElement = entries.nextElement();
242                                 if (!nextElement.isDirectory()){ 
243                                         String name = nextElement.getName();
244                                         artifactNames.add(name);
245                                 }
246                         }
247                         zipFile.close();
248                         return artifactNames;
249                 }
250                 catch(ZipException zipEx){
251                         System.err.println("Error in zip file named : " +  zipFileLocation);    
252                         zipEx.printStackTrace();
253                 } catch (IOException e) {
254                         System.err.println("Unhandled exception : ");
255                         e.printStackTrace();
256                 }
257                 return null;
258         }
259         
260         public static List<String> getZipFileNamesFromFolder(String filepath) {
261                 List<String> fileNamesListFromFolder = filterFileNamesListFromFolder(filepath, ".zip");
262                 fileNamesListFromFolder.addAll(filterFileNamesListFromFolder(filepath, ".csar"));
263                 return fileNamesListFromFolder;
264         }
265
266         public static int countFilesInZipFile(String[] artifactsArr, String reqExtension){
267                 int fileCounter = 0;
268                 for (String artifact : artifactsArr){
269                         String extensionFile = artifact.substring(artifact.lastIndexOf(".") + 1 , artifact.length());
270                         if (extensionFile.equals(reqExtension)){
271                                 fileCounter++;
272                         }
273                 }
274                 return fileCounter;
275         }
276         
277         /**
278          * @param dirPath
279          * @return last modified file name from dirPath directory
280          */
281         public static synchronized File getLastModifiedFileNameFromDir(String dirPath){
282             File dir = new File(dirPath);
283             File[] files = dir.listFiles();
284             if (files == null) {
285                 assertTrue("File not found under directory " + dirPath, false);
286                 return null;
287             }
288
289             File lastModifiedFile = files[0];
290             for (int i = 1; i < files.length; i++) {
291                 if(files[i].isDirectory()) {
292                         continue;
293                 }
294                 if (lastModifiedFile.lastModified()  < files[i].lastModified()) {
295                    lastModifiedFile = files[i];
296                 }
297             }
298             return lastModifiedFile;
299         }
300
301         public static void deleteDirectory(String directoryPath) {
302                 File dir = new File(directoryPath);
303                 try {
304                         FileUtils.cleanDirectory(dir);
305                 } catch (IllegalArgumentException e) {
306                         System.out.println("Failed to clean " + dir);
307                 } catch (IOException e) {
308                         System.out.println("Failed to clean " + dir);
309                 }
310         }
311         
312         public static void createDirectory(String directoryPath) {
313                 File directory = new File(String.valueOf(directoryPath));
314             if (! directory.exists()){
315                 directory.mkdir();
316             }
317         }
318
319
320         /**
321          * The method append data to existing file, if file not exists - create it
322          * @param pathToFile
323          * @param text
324          * @param leftSpaceCount
325          * @throws IOException
326          */
327         public static synchronized void writeToFile(File pathToFile, Object text, Integer leftSpaceCount) throws IOException{
328
329                 BufferedWriter bw = null;
330                 FileWriter fw = null;
331                 if(!pathToFile.exists()){
332                         createEmptyFile(pathToFile);
333                 }
334                 try {
335                         fw = new FileWriter(pathToFile, true);
336                         bw = new BufferedWriter(fw);
337                         StringBuilder sb = new StringBuilder();
338                         if(leftSpaceCount > 0 ){
339                                 for(int i = 0; i < leftSpaceCount; i++){
340                                         sb.append(" ");
341                                 }
342                         }
343                         bw.write(sb.toString() + text);
344                         bw.newLine();
345                         bw.close();
346                         fw.close();
347                 } catch (Exception e) {
348                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
349                 }
350         }
351
352         public static String getCreateDirByName(String dirName) {
353                 File dir = new File(dirName);
354                 dir.mkdir();
355                 if(!dir.exists()) {
356                 }
357
358                 return dir.getPath();
359         }
360         
361         public static boolean isFileDownloaded(String downloadPath, String fileName) {
362                 boolean flag = false;
363                 File dir = new File(downloadPath);
364                 File[] dir_contents = dir.listFiles();
365                 for (int i = 0; i < dir_contents.length; i++) {
366                         if (dir_contents[i].getName().equals(fileName))
367                                 return flag = true;
368                 }
369                 return flag;
370         }
371         
372         public static String getMD5OfFile(File file) throws IOException {
373                 String content = FileUtils.readFileToString(file);
374                 String md5 = GeneralUtility.calculateMD5Base64EncodedByString(content);
375                 return md5;
376         }
377         
378         public static File createEmptyFile(String fileToCreate) {
379                 File file= new File(fileToCreate);
380                 try {
381                         if(file.exists()){
382                                 deleteFile(file);
383                         }
384                         file.createNewFile();
385                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
386                 } catch (IOException e) {
387                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
388                         e.printStackTrace();
389                 }
390                 return file;
391         }
392         
393         public static File createEmptyFile(File fileToCreate) {
394                 try {
395                         if(fileToCreate.exists()){
396                                 deleteFile(fileToCreate);
397                         }
398                         fileToCreate.createNewFile();
399                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
400                 } catch (IOException e) {
401                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
402                         e.printStackTrace();
403                 }
404                 return fileToCreate;
405         }
406         
407         public static void deleteFile(File file){
408                 
409                 try{
410                 if(file.exists()){
411                         file.deleteOnExit();
412                         ComponentBaseTest.getExtendTest().log(Status.INFO, "File " + file.getName() + "has been deleted");
413                 }else{
414                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to delete file " + file.getName());
415                 }
416         }catch(Exception e){
417                 e.printStackTrace();
418         }
419
420         }
421         
422         
423         /**
424          * get file list from directory by extension array
425          * @param directory
426          * @param okFileExtensions
427          * @return
428          */
429         public static List<File> getHeatAndHeatEnvArtifactsFromZip(File directory, String[] okFileExtensions){
430                 
431                         List<File> fileList = new ArrayList<>();
432                         File[] files = directory.listFiles();
433                         
434                         for (String extension : okFileExtensions){
435                                 for(File file : files){
436                                         if (file.getName().toLowerCase().endsWith(extension)){
437                                                 fileList.add(file);
438                                         }
439                                 }
440                         }
441                         return fileList;
442         }
443
444     public static String getKeyByValueFromPropertyFormatFile(String fullPath, String key) {
445                 Properties prop = new Properties();
446                 InputStream input = null;
447                 String value = null;
448                 try {
449                         input = new FileInputStream(fullPath);
450                         prop.load(input);
451                         value = (prop.getProperty(key));
452
453                 } catch (IOException ex) {
454                         ex.printStackTrace();
455                 } finally {
456                         if (input != null) {
457                                 try {
458                                         input.close();
459                                 } catch (IOException e) {
460                                         e.printStackTrace();
461                                 }
462                         }
463                 }
464
465                 return value.replaceAll("\"","");
466         }
467
468     public static void overWriteExistindDir(String outputCsar) throws IOException {
469                 String basePath = getBasePath();
470                 String csarDir = FileHandling.getCreateDirByName("target/outputCsar");
471                 FileUtils.cleanDirectory(new File(csarDir));
472     }
473 }