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