[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / utilities / 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.utilities;
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.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 java.util.zip.ZipInputStream;
44
45 import org.apache.commons.io.FileUtils;
46 import org.openecomp.sdc.ci.tests.config.Config;
47 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
48 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
49 import org.openecomp.sdc.common.util.GeneralUtility;
50 import org.yaml.snakeyaml.Yaml;
51
52 import com.aventstack.extentreports.Status;
53 import com.clearspring.analytics.util.Pair;
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         
92         public static String getFilePath(String folder) {
93                 String filepath = System.getProperty("filepath");
94                 if (filepath == null && System.getProperty("os.name").contains("Windows")) {
95                         filepath = FileHandling.getResourcesFilesPath() + folder + File.separator;
96                 }
97                 
98                 else if(filepath.isEmpty() && !System.getProperty("os.name").contains("Windows")){
99                                 filepath = FileHandling.getBasePath() + "Files" + File.separator + folder + File.separator;
100                 }
101                 
102                 System.out.println(filepath);
103                 
104                 return filepath;
105         }
106
107         public static String getBasePath() {
108                 return System.getProperty("user.dir") + File.separator;
109         }
110         
111         public static String getDriversPath() {
112                 return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
113                                 + File.separator + "ci" + File.separator + "drivers" + File.separator;
114         }
115
116         public static String getResourcesFilesPath() {
117                 return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
118                                 + 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 String[] getArtifactsFromZip(String filepath, String zipFilename){
176                 try{
177                         ZipFile zipFile = new ZipFile(filepath + File.separator + zipFilename);
178                         Enumeration<? extends ZipEntry> entries = zipFile.entries();
179                         
180                         String[] artifactNames = new String[zipFile.size() - 1];
181
182                         int i = 0;
183                         while(entries.hasMoreElements()){
184                                 ZipEntry nextElement = entries.nextElement();
185                                 if (!nextElement.isDirectory()){ 
186                                         if (!nextElement.getName().equals("MANIFEST.json")){
187                                                 String name = nextElement.getName();
188                                                 artifactNames[i++] = name;
189                                         }
190                                 }
191                         }
192                         zipFile.close();
193                         return artifactNames;
194                 }
195                 catch(ZipException zipEx){
196                         System.err.println("Error in zip file named : " +  zipFilename);        
197                         zipEx.printStackTrace();
198                 } catch (IOException e) {
199                         System.err.println("Unhandled exception : ");
200                         e.printStackTrace();
201                 }
202                 
203                 return null;
204                 
205         }
206
207         public static Object[] getZipFileNamesFromFolder(String filepath) {
208                 return filterFileNamesFromFolder(filepath, ".zip");
209         }
210
211         public static int countFilesInZipFile(String[] artifactsArr, String reqExtension){
212                 int fileCounter = 0;
213                 for (String artifact : artifactsArr){
214                         String extensionFile = artifact.substring(artifact.lastIndexOf(".") + 1 , artifact.length());
215                         if (extensionFile.equals(reqExtension)){
216                                 fileCounter++;
217                         }
218                 }
219                 return fileCounter;
220         }
221         
222
223         public static synchronized File getLastModifiedFileFromDir() throws Exception{
224                 return getLastModifiedFileFromDir(SetupCDTest.getWindowTest().getDownloadDirectory());
225         }
226         
227         public static synchronized File getLastModifiedFileFromDir(String dirPath){
228             File dir = new File(dirPath);
229             File[] files = dir.listFiles();
230             if (files == null) {
231                 assertTrue("File not found under directory " + dirPath, false);
232                 return null;
233             }
234
235             File lastModifiedFile = files[0];
236             for (int i = 1; i < files.length; i++) {
237                 if(files[i].isDirectory()) {
238                         continue;
239                 }
240                 if (lastModifiedFile.lastModified()  < files[i].lastModified()) {
241                    lastModifiedFile = files[i];
242                 }
243             }
244             return lastModifiedFile;
245         }
246
247         public static void deleteDirectory(String directoryPath) {
248                 File dir = new File(directoryPath);
249                 try {
250                         FileUtils.deleteDirectory(dir);
251                 } catch (IOException e) {
252                         System.out.println("Failed to delete " + dir);
253                         SetupCDTest.getExtendTest().log(Status.INFO, "Failed to delete " + dir);
254                 }
255         }
256         
257         public static void createDirectory(String directoryPath) {
258                 File directory = new File(String.valueOf(directoryPath));
259             if (! directory.exists()){
260                 directory.mkdir();
261             }
262         }
263
264
265         /**
266          * The method append data to existing file, if file not exists - create it
267          * @param pathToFile
268          * @param text
269          * @param leftSpaceCount
270          * @throws IOException
271          */
272         public static synchronized void writeToFile(File pathToFile, Object text, Integer leftSpaceCount) throws IOException{
273                 
274                 BufferedWriter bw = null;
275                 FileWriter fw = null;
276                 if(!pathToFile.exists()){
277                         createEmptyFile(pathToFile);
278                 }
279                 try {
280                         fw = new FileWriter(pathToFile, true);
281                         bw = new BufferedWriter(fw);
282                         StringBuilder sb = new StringBuilder();
283                         if(leftSpaceCount > 0 ){
284                                 for(int i = 0; i < leftSpaceCount; i++){
285                                         sb.append(" ");
286                                 }
287                         }
288                         bw.write(sb.toString() + text);
289                         bw.newLine();
290                         bw.close();
291                         fw.close();
292                 } catch (Exception e) {
293                         SetupCDTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
294                 }
295         }
296         
297         public static synchronized void writeToFile(File pathToFile, Map<String, Pair<String, Object>> dataMap, Integer leftSpaceCount) throws IOException{
298                 
299                 BufferedWriter bw = null;
300                 FileWriter fw = null;
301                 try {
302                         if(!pathToFile.exists()){
303                                 createEmptyFile(pathToFile);
304                         }
305                         fw = new FileWriter(pathToFile, true);
306                         bw = new BufferedWriter(fw);
307                         StringBuilder sb = new StringBuilder();
308                         if(leftSpaceCount > 0 ){
309                                 for(int i = 0; i < leftSpaceCount; i++){
310                                         sb.append(" ");
311                                 }
312                         }
313                         for(Map.Entry<String, Pair<String, Object>> entry : dataMap.entrySet()){
314                                 Object record = ArtifactUIUtils.getFormatedData(entry.getKey(), entry.getValue().right);
315                                 bw.write(sb.toString() + record);
316                                 bw.newLine();
317                         }
318                         bw.close();
319                         fw.close();
320                 } catch (Exception e) {
321                         SetupCDTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
322                 }
323         }
324         
325         public static void deleteLastDowloadedFiles(List<File> files) throws IOException {
326                 for (File file : files) {
327                         File fileToDelete =new File(Config.instance().getWindowsDownloadDirectory()+file.getName());
328                         fileToDelete.delete();
329                 }
330         }
331
332         public static void cleanCurrentDownloadDir() throws IOException {
333                 try{
334                         ExtentTestActions.log(Status.INFO, "Cleaning directory " + SetupCDTest.getWindowTest().getDownloadDirectory());
335                         System.gc();
336                         FileUtils.cleanDirectory(new File(SetupCDTest.getWindowTest().getDownloadDirectory()));
337                 }
338                 catch(Exception e){
339                         
340                 }
341         }
342         
343         public static boolean isFileDownloaded(String downloadPath, String fileName) {
344                 boolean flag = false;
345                 File dir = new File(downloadPath);
346                 File[] dir_contents = dir.listFiles();
347                 for (int i = 0; i < dir_contents.length; i++) {
348                         if (dir_contents[i].getName().equals(fileName))
349                                 return flag = true;
350                 }
351                 return flag;
352         }
353         
354         public static String getMD5OfFile(File file) throws IOException {
355                 String content = FileUtils.readFileToString(file);
356                 String md5 = GeneralUtility.calculateMD5ByString(content);
357                 return md5;
358         }
359         
360         public static File createEmptyFile(String fileToCreate) {
361                 File file= new File(fileToCreate);
362                 try {
363                         if(file.exists()){
364                                 deleteFile(file);
365                         }
366                         file.createNewFile();
367                         SetupCDTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
368                 } catch (IOException e) {
369                         SetupCDTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
370                         e.printStackTrace();
371                 }
372                 return file;
373         }
374         
375         public static File createEmptyFile(File fileToCreate) {
376                 try {
377                         if(fileToCreate.exists()){
378                                 deleteFile(fileToCreate);
379                         }
380                         fileToCreate.createNewFile();
381                         SetupCDTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
382                 } catch (IOException e) {
383                         SetupCDTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
384                         e.printStackTrace();
385                 }
386                 return fileToCreate;
387         }
388         
389         public static void deleteFile(File file){
390                 
391                 try{
392                 if(file.exists()){
393                         file.deleteOnExit();
394                         SetupCDTest.getExtendTest().log(Status.INFO, "File " + file.getName() + "has been deleted");
395                 }else{
396                         SetupCDTest.getExtendTest().log(Status.INFO, "Failed to delete file " + file.getName());
397                 }
398         }catch(Exception e){
399                 e.printStackTrace();
400         }
401
402         }
403         
404         
405         /**
406          * get file list from directory by extension array
407          * @param directory
408          * @param okFileExtensions
409          * @return
410          */
411         public static List<File> getHeatAndHeatEnvArtifactsFromZip(File directory, String[] okFileExtensions){
412                 
413                         List<File> fileList = new ArrayList<>();
414                         File[] files = directory.listFiles();
415                         
416                         for (String extension : okFileExtensions){
417                                 for(File file : files){
418                                         if (file.getName().toLowerCase().endsWith(extension)){
419                                                 fileList.add(file);
420                                         }
421                                 }
422                         }
423                         return fileList;
424         }
425         
426         private static final int BUFFER_SIZE = 4096;
427     public static void unzip(String zipFilePath, String destDirectory) throws IOException {
428         File destDir = new File(destDirectory);
429         if (!destDir.exists()) {
430             destDir.mkdir();
431         }
432         ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
433         ZipEntry entry = zipIn.getNextEntry();
434 //         iterates over entries in the zip file
435         while (entry != null) {
436                 String entryName;
437                 if(System.getProperty("os.name").contains("Windows")){
438                         entryName = entry.getName().replaceAll("/", "\\"+File.separator);
439                 }else{
440                         entryName = entry.getName();
441                 }
442             String filePath = destDirectory + entryName;
443             String currPath = destDirectory;
444             String[] dirs = entryName.split("\\"+File.separator);
445             String currToken;
446             for(int i = 0; i<dirs.length;++i){
447                 currToken = dirs[i];
448                 if(!entry.isDirectory() && i==dirs.length-1){
449                          extractFile(zipIn, filePath);
450                 } else {
451                         if(currPath.endsWith(File.separator)){
452                                 currPath = currPath + currToken;
453                         }else{
454                                 currPath = currPath + File.separator + currToken;
455                         }
456 //                     if the entry is a directory, make the directory
457                     File dir = new File(currPath);
458                     dir.mkdir();
459                 }
460             }
461             zipIn.closeEntry();
462             entry = zipIn.getNextEntry();
463         }
464         zipIn.close();
465     }
466
467     private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
468         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
469         byte[] bytesIn = new byte[BUFFER_SIZE];
470         int read = 0;
471         while ((read = zipIn.read(bytesIn)) != -1) {
472             bos.write(bytesIn, 0, read);
473         }
474         bos.close();
475     }
476         
477     public static int getFileCountFromDefaulDownloadDirectory(){
478         return new File(SetupCDTest.getWindowTest().getDownloadDirectory()).listFiles().length;
479     }
480     
481     
482     public static String getKeyByValueFromPropertyFormatFile(String fullPath, String key) {
483                 Properties prop = new Properties();
484                 InputStream input = null;
485                 String value = null;
486                 try {
487                         input = new FileInputStream(fullPath);
488                         prop.load(input);
489                         value = (prop.getProperty(key));
490
491                 } catch (IOException ex) {
492                         ex.printStackTrace();
493                 } finally {
494                         if (input != null) {
495                                 try {
496                                         input.close();
497                                 } catch (IOException e) {
498                                         e.printStackTrace();
499                                 }
500                         }
501                 }
502
503                 return value.replaceAll("\"","");
504         }
505 }