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