Sync Integ to Master
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / 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.openecomp.sdc.ci.tests.utils.general;
22
23 import com.aventstack.extentreports.Status;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import java.io.BufferedOutputStream;
27 import java.io.BufferedWriter;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.io.FileWriter;
32 import java.io.FilenameFilter;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.nio.file.Paths;
36 import java.util.ArrayList;
37 import java.util.Enumeration;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Properties;
42 import java.util.zip.ZipEntry;
43 import java.util.zip.ZipException;
44 import java.util.zip.ZipFile;
45 import java.util.zip.ZipInputStream;
46
47 import org.apache.commons.io.FileUtils;
48 import org.openecomp.sdc.be.model.DataTypeDefinition;
49 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
50 import org.openecomp.sdc.ci.tests.config.Config;
51 import org.openecomp.sdc.common.util.GeneralUtility;
52 import org.yaml.snakeyaml.Yaml;
53
54 import com.aventstack.extentreports.Status;
55 import java.io.*;
56 import java.nio.file.Paths;
57 import java.util.*;
58 import java.util.zip.ZipEntry;
59 import java.util.zip.ZipException;
60 import java.util.zip.ZipFile;
61 import java.util.zip.ZipInputStream;
62
63 import static org.testng.AssertJUnit.assertTrue;
64
65 public class FileHandling {
66
67 //      ------------------yaml parser methods----------------------------
68         public static Map<?, ?> parseYamlFile(String filePath) throws Exception {
69                 Yaml yaml = new Yaml();
70                 File file = new File(filePath);
71                 InputStream inputStream = new FileInputStream(file);
72                 Map<?, ?> map = (Map<?, ?>) yaml.load(inputStream);
73                 return map;
74         }
75         
76         /**
77          * The method return map fetched objects by pattern from yaml file 
78          * @param yamlFile
79          * @param pattern
80          * @return
81          * @throws Exception
82          */
83         public static Map<String, Object> parseYamlFileToMapByPattern(File yamlFile, String pattern) throws Exception {
84                 Map<?, ?> yamlFileToMap = FileHandling.parseYamlFile(yamlFile.toString());
85                 Map<String, Object> objectMap = getObjectMapByPattern(yamlFileToMap, pattern);
86                 return objectMap;
87         }
88         
89         @SuppressWarnings("unchecked")
90         public static Map<String, Object> getObjectMapByPattern(Map<?, ?> parseUpdetedEnvFile, String pattern) {
91                 Map<String, Object> objectMap = null;
92                 
93                 Object objectUpdetedEnvFile = parseUpdetedEnvFile.get(pattern);
94                 if(objectUpdetedEnvFile instanceof HashMap){
95                         objectMap = (Map<String, Object>) objectUpdetedEnvFile;
96                 }
97                 return objectMap;
98         }
99         
100         
101         public static Map<String, DataTypeDefinition> parseDataTypesYaml(String filePath) throws Exception {
102                 @SuppressWarnings("unchecked")
103                 Map<String, DataTypeDefinition> dataTypesMap = (Map<String, DataTypeDefinition>) parseYamlFile(filePath);
104                 return dataTypesMap;
105         }
106 //      -------------------------------------------------------------------------------------------------
107
108         /**
109          * @param folder, folder name under "Files" folder
110          * @return path to given folder from perspective of working directory or sdc-vnfs repository
111          */
112         public static String getFilePath(String folder) {
113                 String filepath = System.getProperty("filePath");
114                 boolean isFilePathEmptyOrNull = (filepath == null || filepath.isEmpty());
115
116                 // return folder from perspective of sdc-vnfs repository
117                 if (isFilePathEmptyOrNull && ( System.getProperty("os.name").contains("Windows") || System.getProperty("os.name").contains("Mac"))) {
118                         return FileHandling.getResourcesFilesPath() + folder + File.separator;
119                 }
120
121                 // return folder from perspective of working directory ( in general for nightly run from Linux, should already contain "Files" directory )
122                 return FileHandling.getBasePath() + "Files" + File.separator + folder + File.separator;
123         }
124
125         public static String getBasePath() {
126                 return System.getProperty("user.dir") + File.separator;
127         }
128         
129         public static String getSdcVnfsPath() {
130                 return  getBasePath() + Paths.get("..", "..", "sdc-vnfs").toString();
131         }
132         
133         public static String getDriversPath() {
134                 return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
135                                 + File.separator + "ci" + File.separator + "drivers" + File.separator;
136         }
137
138         public static String getResourcesFilesPath() {
139 //              return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
140 //                              + File.separator + "Files" + File.separator;
141
142                 return getSdcVnfsPath()+ File.separator + "ui-tests" + File.separator + "Files" + File.separator;
143         }
144         
145         public static String getResourcesEnvFilesPath() {
146                 return getBasePath() + File.separator + "src" + File.separator + "main" + File.separator + "resources"
147                                 + File.separator + "Files" + File.separator + "ResourcesEnvFiles" +File.separator;
148         }
149
150         public static String getCiFilesPath() {
151                 return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
152                                 + File.separator + "ci";
153         }
154
155         public static String getConfFilesPath() {
156                 return getCiFilesPath() + File.separator + "conf" + File.separator;
157         }
158
159         public static String getTestSuitesFilesPath() {
160                 return getCiFilesPath() + File.separator + "testSuites" + File.separator;
161         }
162         
163         public static String getVnfRepositoryPath() {
164                 return getFilePath("VNFs");
165         }
166
167         public static String getPortMirroringRepositoryPath() {
168                 return getFilePath("PortMirroring");
169         }
170         
171         public static File getConfigFile(String configFileName) throws Exception {
172                 File configFile = new File(FileHandling.getBasePath() + File.separator + "conf" + File.separator + configFileName);
173                 if (!configFile.exists()) {
174                         configFile = new File(FileHandling.getConfFilesPath() + configFileName);
175                 }
176                 return configFile;
177         }
178
179         public static Object[] filterFileNamesFromFolder(String filepath, String extension) {
180                 try {
181                         File dir = new File(filepath);
182                         List<String> filenames = new ArrayList<String>();
183                         
184                         FilenameFilter extensionFilter = new FilenameFilter() {
185                                 public boolean accept(File dir, String name) {
186                                         return name.endsWith(extension);
187                                 }
188                         };
189                         
190                         if (dir.isDirectory()) {
191                                 for (File file : dir.listFiles(extensionFilter)) {
192                                         filenames.add(file.getName());
193                                 }
194                                 return filenames.toArray();
195                         }
196
197                 } catch (Exception e) {
198                         e.printStackTrace();
199                 }
200                 return null;
201         }
202
203         public static List<String> filterFileNamesListFromFolder(String filepath, String extension) {
204                 List<String> filenames = new ArrayList<String>();
205                 try {
206                         File dir = new File(filepath);
207                         
208                         FilenameFilter extensionFilter = new FilenameFilter() {
209                                 public boolean accept(File dir, String name) {
210                                         return name.endsWith(extension);
211                                 }
212                         };
213                         
214                         if (dir.isDirectory()) {
215                                 for (File file : dir.listFiles(extensionFilter)) {
216                                         filenames.add(file.getName());
217                                 }
218                                 return filenames;
219                         }
220
221                 } catch (Exception e) {
222                         e.printStackTrace();
223                 }
224                 return filenames;
225         }
226         
227         public static String[] getArtifactsFromZip(String filepath, String zipFilename){
228                 try {
229                         ZipFile zipFile = new ZipFile(filepath + File.separator + zipFilename);
230                         Enumeration<? extends ZipEntry> entries = zipFile.entries();
231                         
232                         String[] artifactNames = new String[zipFile.size() - 1];
233
234                         int i = 0;
235                         while(entries.hasMoreElements()){
236                                 ZipEntry nextElement = entries.nextElement();
237                                 if (!nextElement.isDirectory()){ 
238                                         if (!nextElement.getName().equals("MANIFEST.json")){
239                                                 String name = nextElement.getName();
240                                                 artifactNames[i++] = name;
241                                         }
242                                 }
243                         }
244                         zipFile.close();
245                         return artifactNames;
246                 } catch(ZipException zipEx) {
247                         System.err.println("Error in zip file named : " +  zipFilename);        
248                         zipEx.printStackTrace();
249                 } catch (IOException e) {
250                         System.err.println("Unhandled exception : ");
251                         e.printStackTrace();
252                 }
253                 
254                 return null;
255                 
256         }
257
258         public static List<String> getFileNamesFromZip(String zipFileLocation){
259                 try{
260                         ZipFile zipFile = new ZipFile(zipFileLocation);
261                         Enumeration<? extends ZipEntry> entries = zipFile.entries();
262                         
263                         List<String> artifactNames = new ArrayList<>();
264
265                         int i = 0;
266                         while(entries.hasMoreElements()){
267                                 ZipEntry nextElement = entries.nextElement();
268                                 if (!nextElement.isDirectory()){ 
269                                         String name = nextElement.getName();
270                                         artifactNames.add(name);
271                                 }
272                         }
273                         zipFile.close();
274                         return artifactNames;
275                 }
276                 catch(ZipException zipEx){
277                         System.err.println("Error in zip file named : " +  zipFileLocation);    
278                         zipEx.printStackTrace();
279                 } catch (IOException e) {
280                         System.err.println("Unhandled exception : ");
281                         e.printStackTrace();
282                 }
283                 return null;
284         }
285         
286 //      public static Object[] getZipFileNamesFromFolder(String filepath) {
287 //              return filterFileNamesFromFolder(filepath, ".zip");
288 //      }
289         
290         public static List<String> getZipFileNamesFromFolder(String filepath) {
291                 List<String> fileNamesListFromFolder = filterFileNamesListFromFolder(filepath, ".zip");
292                 fileNamesListFromFolder.addAll(filterFileNamesListFromFolder(filepath, ".csar"));
293                 return fileNamesListFromFolder;
294         }
295
296         public static int countFilesInZipFile(String[] artifactsArr, String reqExtension){
297                 int fileCounter = 0;
298                 for (String artifact : artifactsArr){
299                         String extensionFile = artifact.substring(artifact.lastIndexOf(".") + 1 , artifact.length());
300                         if (extensionFile.equals(reqExtension)){
301                                 fileCounter++;
302                         }
303                 }
304                 return fileCounter;
305         }
306         
307
308         /**
309          * @return last modified file name from default directory
310          * @throws Exception
311          */
312 //      TODO add download directory capability
313 //      public static synchronized File getLastModifiedFileNameFromDir() throws Exception{
314 //              return getLastModifiedFileNameFromDir(ComponentBaseTest.getWindowTest().getDownloadDirectory());
315 //      }
316         
317         /**
318          * @param dirPath
319          * @return last modified file name from dirPath directory
320          */
321         public static synchronized File getLastModifiedFileNameFromDir(String dirPath){
322             File dir = new File(dirPath);
323             File[] files = dir.listFiles();
324             if (files == null) {
325                 assertTrue("File not found under directory " + dirPath, false);
326                 return null;
327             }
328
329             File lastModifiedFile = files[0];
330             for (int i = 1; i < files.length; i++) {
331                 if(files[i].isDirectory()) {
332                         continue;
333                 }
334                 if (lastModifiedFile.lastModified()  < files[i].lastModified()) {
335                    lastModifiedFile = files[i];
336                 }
337             }
338             return lastModifiedFile;
339         }
340
341         public static void deleteDirectory(String directoryPath) {
342                 File dir = new File(directoryPath);
343                 try {
344                         FileUtils.cleanDirectory(dir);
345                 } catch (IOException e) {
346                         System.out.println("Failed to delete " + dir);
347                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to delete " + dir);
348                 }
349         }
350         
351         public static void createDirectory(String directoryPath) {
352                 File directory = new File(String.valueOf(directoryPath));
353             if (! directory.exists()){
354                 directory.mkdir();
355             }
356         }
357
358
359         /**
360          * The method append data to existing file, if file not exists - create it
361          * @param pathToFile
362          * @param text
363          * @param leftSpaceCount
364          * @throws IOException
365          */
366         public static synchronized void writeToFile(File pathToFile, Object text, Integer leftSpaceCount) throws IOException{
367
368                 BufferedWriter bw = null;
369                 FileWriter fw = null;
370                 if(!pathToFile.exists()){
371                         createEmptyFile(pathToFile);
372                 }
373                 try {
374                         fw = new FileWriter(pathToFile, true);
375                         bw = new BufferedWriter(fw);
376                         StringBuilder sb = new StringBuilder();
377                         if(leftSpaceCount > 0 ){
378                                 for(int i = 0; i < leftSpaceCount; i++){
379                                         sb.append(" ");
380                                 }
381                         }
382                         bw.write(sb.toString() + text);
383                         bw.newLine();
384                         bw.close();
385                         fw.close();
386                 } catch (Exception e) {
387                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
388                 }
389         }
390         
391 //      public static synchronized void writeToFile(File pathToFile, Map<String, Pair<String, Object>> dataMap, Integer leftSpaceCount) throws IOException{
392 //
393 //              BufferedWriter bw = null;
394 //              FileWriter fw = null;
395 //              try {
396 //                      if(!pathToFile.exists()){
397 //                              createEmptyFile(pathToFile);
398 //                      }
399 //                      fw = new FileWriter(pathToFile, true);
400 //                      bw = new BufferedWriter(fw);
401 //                      StringBuilder sb = new StringBuilder();
402 //                      if(leftSpaceCount > 0 ){
403 //                              for(int i = 0; i < leftSpaceCount; i++){
404 //                                      sb.append(" ");
405 //                              }
406 //                      }
407 //                      for(Map.Entry<String, Pair<String, Object>> entry : dataMap.entrySet()){
408 //                              Object record = ArtifactUIUtils.getFormatedData(entry.getKey(), entry.getValue().right);
409 //                              bw.write(sb.toString() + record);
410 //                              bw.newLine();
411 //                      }
412 //                      bw.close();
413 //                      fw.close();
414 //              } catch (Exception e) {
415 //                      ComponentBaseTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
416 //              }
417 //      }
418         
419         public static void deleteLastDowloadedFiles(List<File> files) throws IOException {
420                 for (File file : files) {
421                         File fileToDelete =new File(Config.instance().getWindowsDownloadDirectory()+file.getName());
422                         fileToDelete.delete();
423                 }
424         }
425 // TODO add work with directory capaability
426 //      public static void cleanCurrentDownloadDir() throws IOException {
427 //              try{
428 //                      ExtentTestActions.log(Status.INFO, "Cleaning directory " + ComponentBaseTest.getWindowTest().getDownloadDirectory());
429 //                      System.gc();
430 //                      FileUtils.cleanDirectory(new File(ComponentBaseTest.getWindowTest().getDownloadDirectory()));
431 //              }
432 //              catch(Exception e){
433 //
434 //              }
435 //      }
436
437         public static String getCreateDirByName(String dirName) {
438                 File dir = new File(dirName);
439                 dir.mkdir();
440                 if(!dir.exists()) {
441                 }
442
443                 return dir.getPath();
444         }
445         
446         public static boolean isFileDownloaded(String downloadPath, String fileName) {
447                 boolean flag = false;
448                 File dir = new File(downloadPath);
449                 File[] dir_contents = dir.listFiles();
450                 for (int i = 0; i < dir_contents.length; i++) {
451                         if (dir_contents[i].getName().equals(fileName))
452                                 return flag = true;
453                 }
454                 return flag;
455         }
456         
457         public static String getMD5OfFile(File file) throws IOException {
458                 String content = FileUtils.readFileToString(file);
459                 String md5 = GeneralUtility.calculateMD5Base64EncodedByString(content);
460                 return md5;
461         }
462         
463         public static File createEmptyFile(String fileToCreate) {
464                 File file= new File(fileToCreate);
465                 try {
466                         if(file.exists()){
467                                 deleteFile(file);
468                         }
469                         file.createNewFile();
470                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
471                 } catch (IOException e) {
472                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
473                         e.printStackTrace();
474                 }
475                 return file;
476         }
477         
478         public static File createEmptyFile(File fileToCreate) {
479                 try {
480                         if(fileToCreate.exists()){
481                                 deleteFile(fileToCreate);
482                         }
483                         fileToCreate.createNewFile();
484                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
485                 } catch (IOException e) {
486                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
487                         e.printStackTrace();
488                 }
489                 return fileToCreate;
490         }
491         
492         public static void deleteFile(File file){
493                 
494                 try{
495                 if(file.exists()){
496                         file.deleteOnExit();
497                         ComponentBaseTest.getExtendTest().log(Status.INFO, "File " + file.getName() + "has been deleted");
498                 }else{
499                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to delete file " + file.getName());
500                 }
501         }catch(Exception e){
502                 e.printStackTrace();
503         }
504
505         }
506         
507         
508         /**
509          * get file list from directory by extension array
510          * @param directory
511          * @param okFileExtensions
512          * @return
513          */
514         public static List<File> getHeatAndHeatEnvArtifactsFromZip(File directory, String[] okFileExtensions){
515                 
516                         List<File> fileList = new ArrayList<>();
517                         File[] files = directory.listFiles();
518                         
519                         for (String extension : okFileExtensions){
520                                 for(File file : files){
521                                         if (file.getName().toLowerCase().endsWith(extension)){
522                                                 fileList.add(file);
523                                         }
524                                 }
525                         }
526                         return fileList;
527         }
528         
529         private static final int BUFFER_SIZE = 4096;
530 //    public static void unzip(String zipFilePath, String destDirectory) throws IOException {
531 //        File destDir = new File(destDirectory);
532 //        if (!destDir.exists()) {
533 //            destDir.mkdir();
534 //        }
535 //        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
536 //        ZipEntry entry = zipIn.getNextEntry();
537 ////         iterates over entries in the zip file
538 //        while (entry != null) {
539 //              String entryName;
540 //              if(System.getProperty("os.name").contains("Windows")){
541 //                      entryName = entry.getName().replaceAll("/", "\\"+File.separator);
542 //              }else{
543 //                      entryName = entry.getName();
544 //              }
545 //            String filePath = destDirectory + entryName;
546 //            String currPath = destDirectory;
547 //            String[] dirs = entryName.split("\\"+File.separator);
548 //            String currToken;
549 //            for(int i = 0; i<dirs.length;++i){
550 //              currToken = dirs[i];
551 //              if(!entry.isDirectory() && i==dirs.length-1){
552 //                       extractFile(zipIn, filePath);
553 //              } else {
554 //                      if(currPath.endsWith(File.separator)){
555 //                              currPath = currPath + currToken;
556 //                      }else{
557 //                              currPath = currPath + File.separator + currToken;
558 //                      }
559 ////                     if the entry is a directory, make the directory
560 //                    File dir = new File(currPath);
561 //                    dir.mkdir();
562 //                }
563 //            }
564 //            zipIn.closeEntry();
565 //            entry = zipIn.getNextEntry();
566 //        }
567 //        zipIn.close();
568 //    }
569
570     private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
571         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
572         byte[] bytesIn = new byte[BUFFER_SIZE];
573         int read = 0;
574         while ((read = zipIn.read(bytesIn)) != -1) {
575             bos.write(bytesIn, 0, read);
576         }
577         bos.close();
578     }
579         
580 //    public static int getFileCountFromDefaulDownloadDirectory(){
581 //      return new File(ComponentBaseTest.getWindowTest().getDownloadDirectory()).listFiles().length;
582 //    }
583     
584     
585     public static String getKeyByValueFromPropertyFormatFile(String fullPath, String key) {
586                 Properties prop = new Properties();
587                 InputStream input = null;
588                 String value = null;
589                 try {
590                         input = new FileInputStream(fullPath);
591                         prop.load(input);
592                         value = (prop.getProperty(key));
593
594                 } catch (IOException ex) {
595                         ex.printStackTrace();
596                 } finally {
597                         if (input != null) {
598                                 try {
599                                         input.close();
600                                 } catch (IOException e) {
601                                         e.printStackTrace();
602                                 }
603                         }
604                 }
605
606                 return value.replaceAll("\"","");
607         }
608
609     public static void overWriteExistindDir(String outputCsar) throws IOException {
610                 String basePath = getBasePath();
611                 String csarDir = FileHandling.getCreateDirByName("outputCsar");
612                 FileUtils.cleanDirectory(new File(csarDir));
613     }
614 }