Merge automation from ECOMP's repository
[vid.git] / vid-automation / src / main / java / org / onap / 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.onap.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.net.InetAddress;
35 import java.net.UnknownHostException;
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.onap.sdc.ci.tests.datatypes.UserCredentials;
49 import org.onap.sdc.ci.tests.execute.setup.ExtentTestActions;
50 import org.onap.sdc.ci.tests.execute.setup.SetupCDTest;
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         
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 File getConfigFile(String configFileName) throws Exception {
140                 File configFile = new File(FileHandling.getBasePath() + File.separator + "conf" + File.separator + configFileName);
141                 if (!configFile.exists()) {
142                         configFile = new File(FileHandling.getConfFilesPath() + configFileName);
143                 }
144                 return configFile;
145         }
146
147         public static Object[] filterFileNamesFromFolder(String filepath, String extension) {
148                 try {
149                         File dir = new File(filepath);
150                         List<String> filenames = new ArrayList<String>();
151                         
152                         FilenameFilter extensionFilter = new FilenameFilter() {
153                                 public boolean accept(File dir, String name) {
154                                         return name.endsWith(extension);
155                                 }
156                         };
157                         
158                         if (dir.isDirectory()) {
159                                 for (File file : dir.listFiles(extensionFilter)) {
160                                         filenames.add(file.getName());
161                                 }
162                                 return filenames.toArray();
163                         }
164
165                 } catch (Exception e) {
166                         e.printStackTrace();
167                 }
168                 return null;
169         }
170
171         public static String[] getArtifactsFromZip(String filepath, String zipFilename){
172                 try{
173                         ZipFile zipFile = new ZipFile(filepath + File.separator + zipFilename);
174                         Enumeration<? extends ZipEntry> entries = zipFile.entries();
175                         
176                         String[] artifactNames = new String[zipFile.size() - 1];
177
178                         int i = 0;
179                         while(entries.hasMoreElements()){
180                                 ZipEntry nextElement = entries.nextElement();
181                                 if (!nextElement.isDirectory()){ 
182                                         if (!nextElement.getName().equals("MANIFEST.json")){
183                                                 String name = nextElement.getName();
184                                                 artifactNames[i++] = name;
185                                         }
186                                 }
187                         }
188                         zipFile.close();
189                         return artifactNames;
190                 }
191                 catch(ZipException zipEx){
192                         System.err.println("Error in zip file named : " +  zipFilename);        
193                         zipEx.printStackTrace();
194                 } catch (IOException e) {
195                         System.err.println("Unhandled exception : ");
196                         e.printStackTrace();
197                 }
198                 
199                 return null;
200                 
201         }
202
203         public static Object[] getZipFileNamesFromFolder(String filepath) {
204                 return filterFileNamesFromFolder(filepath, ".zip");
205         }
206
207         public static int countFilesInZipFile(String[] artifactsArr, String reqExtension){
208                 int fileCounter = 0;
209                 for (String artifact : artifactsArr){
210                         String extensionFile = artifact.substring(artifact.lastIndexOf(".") + 1 , artifact.length());
211                         if (extensionFile.equals(reqExtension)){
212                                 fileCounter++;
213                         }
214                 }
215                 return fileCounter;
216         }
217         
218
219         public static synchronized File getLastModifiedFileFromDir() throws Exception{
220                 return getLastModifiedFileFromDir(SetupCDTest.getWindowTest().getDownloadDirectory());
221         }
222         
223         public static synchronized File getLastModifiedFileFromDir(String dirPath){
224             File dir = new File(dirPath);
225             File[] files = dir.listFiles();
226             if (files == null) {
227                 assertTrue("File not found under directory " + dirPath, false);
228                 return null;
229             }
230
231             File lastModifiedFile = files[0];
232             for (int i = 1; i < files.length; i++) {
233                 if(files[i].isDirectory()) {
234                         continue;
235                 }
236                 if (lastModifiedFile.lastModified()  < files[i].lastModified()) {
237                    lastModifiedFile = files[i];
238                 }
239             }
240             return lastModifiedFile;
241         }
242
243         public static void deleteDirectory(String directoryPath) {
244                 File dir = new File(directoryPath);
245                 try {
246                         FileUtils.deleteDirectory(dir);
247                 } catch (IOException e) {
248                         System.out.println("Failed to delete " + dir);
249                         SetupCDTest.getExtendTest().log(Status.INFO, "Failed to delete " + dir);
250                 }
251         }
252         
253         public static void createDirectory(String directoryPath) {
254                 File directory = new File(String.valueOf(directoryPath));
255             if (! directory.exists()){
256                 directory.mkdir();
257             }
258         }
259
260
261         /**
262          * The method append data to existing file, if file not exists - create it
263          * @param pathToFile
264          * @param text
265          * @param leftSpaceCount
266          * @throws IOException
267          */
268         public static synchronized void writeToFile(File pathToFile, Object text, Integer leftSpaceCount) throws IOException{
269                 
270                 BufferedWriter bw = null;
271                 FileWriter fw = null;
272                 if(!pathToFile.exists()){
273                         createEmptyFile(pathToFile);
274                 }
275                 try {
276                         fw = new FileWriter(pathToFile, true);
277                         bw = new BufferedWriter(fw);
278                         StringBuilder sb = new StringBuilder();
279                         if(leftSpaceCount > 0 ){
280                                 for(int i = 0; i < leftSpaceCount; i++){
281                                         sb.append(" ");
282                                 }
283                         }
284                         bw.write(sb.toString() + text);
285                         bw.newLine();
286                         bw.close();
287                         fw.close();
288                 } catch (Exception e) {
289                         SetupCDTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
290                 }
291         }
292         
293         
294         public static void cleanCurrentDownloadDir() throws IOException {
295                 try{
296                         ExtentTestActions.log(Status.INFO, "Cleaning directory " + SetupCDTest.getWindowTest().getDownloadDirectory());
297                         System.gc();
298                         FileUtils.cleanDirectory(new File(SetupCDTest.getWindowTest().getDownloadDirectory()));
299                 }
300                 catch(Exception e){
301                         
302                 }
303         }
304         
305         public static boolean isFileDownloaded(String downloadPath, String fileName) {
306                 boolean flag = false;
307                 File dir = new File(downloadPath);
308                 File[] dir_contents = dir.listFiles();
309                 for (int i = 0; i < dir_contents.length; i++) {
310                         if (dir_contents[i].getName().equals(fileName))
311                                 return flag = true;
312                 }
313                 return flag;
314         }
315         
316         public static String getMD5OfFile(File file) throws IOException {
317                 String content = FileUtils.readFileToString(file);
318                 String md5 = GeneralUtility.calculateMD5ByString(content);
319                 return md5;
320         }
321         
322         public static File createEmptyFile(String fileToCreate) {
323                 File file= new File(fileToCreate);
324                 try {
325                         if(file.exists()){
326                                 deleteFile(file);
327                         }
328                         file.createNewFile();
329                         SetupCDTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
330                 } catch (IOException e) {
331                         SetupCDTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
332                         e.printStackTrace();
333                 }
334                 return file;
335         }
336         
337         public static File createEmptyFile(File fileToCreate) {
338                 try {
339                         if(fileToCreate.exists()){
340                                 deleteFile(fileToCreate);
341                         }
342                         fileToCreate.createNewFile();
343                         SetupCDTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
344                 } catch (IOException e) {
345                         SetupCDTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
346                         e.printStackTrace();
347                 }
348                 return fileToCreate;
349         }
350         
351         public static void deleteFile(File file){
352                 
353                 try{
354                 if(file.exists()){
355                         file.deleteOnExit();
356                         SetupCDTest.getExtendTest().log(Status.INFO, "File " + file.getName() + "has been deleted");
357                 }else{
358                         SetupCDTest.getExtendTest().log(Status.INFO, "Failed to delete file " + file.getName());
359                 }
360         }catch(Exception e){
361                 e.printStackTrace();
362         }
363
364         }
365         
366         
367         /**
368          * get file list from directory by extension array
369          * @param directory
370          * @param okFileExtensions
371          * @return
372          */
373         public static List<File> getHeatAndHeatEnvArtifactsFromZip(File directory, String[] okFileExtensions){
374                 
375                         List<File> fileList = new ArrayList<>();
376                         File[] files = directory.listFiles();
377                         
378                         for (String extension : okFileExtensions){
379                                 for(File file : files){
380                                         if (file.getName().toLowerCase().endsWith(extension)){
381                                                 fileList.add(file);
382                                         }
383                                 }
384                         }
385                         return fileList;
386         }
387         
388         private static final int BUFFER_SIZE = 4096;
389     public static void unzip(String zipFilePath, String destDirectory) throws IOException {
390         File destDir = new File(destDirectory);
391         if (!destDir.exists()) {
392             destDir.mkdir();
393         }
394         ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
395         ZipEntry entry = zipIn.getNextEntry();
396 //         iterates over entries in the zip file
397         while (entry != null) {
398                 String entryName;
399                 if(System.getProperty("os.name").contains("Windows")){
400                         entryName = entry.getName().replaceAll("/", "\\"+File.separator);
401                 }else{
402                         entryName = entry.getName();
403                 }
404             String filePath = destDirectory + entryName;
405             String currPath = destDirectory;
406             String[] dirs = entryName.split("\\"+File.separator);
407             String currToken;
408             for(int i = 0; i<dirs.length;++i){
409                 currToken = dirs[i];
410                 if(!entry.isDirectory() && i==dirs.length-1){
411                          extractFile(zipIn, filePath);
412                 } else {
413                         if(currPath.endsWith(File.separator)){
414                                 currPath = currPath + currToken;
415                         }else{
416                                 currPath = currPath + File.separator + currToken;
417                         }
418 //                     if the entry is a directory, make the directory
419                     File dir = new File(currPath);
420                     dir.mkdir();
421                 }
422             }
423             zipIn.closeEntry();
424             entry = zipIn.getNextEntry();
425         }
426         zipIn.close();
427     }
428
429     private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
430         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
431         byte[] bytesIn = new byte[BUFFER_SIZE];
432         int read = 0;
433         while ((read = zipIn.read(bytesIn)) != -1) {
434             bos.write(bytesIn, 0, read);
435         }
436         bos.close();
437     }
438         
439     public static int getFileCountFromDefaulDownloadDirectory(){
440         return new File(SetupCDTest.getWindowTest().getDownloadDirectory()).listFiles().length;
441     }
442     
443     
444     public static String getKeyByValueFromPropertyFormatFile(String fullPath, String key) {
445                 Properties prop = new Properties();
446                 InputStream input = null;
447                 String value = null;
448                 try {
449                         input = new FileInputStream(fullPath);
450                         prop.load(input);
451                         value = (prop.getProperty(key));
452
453                 } catch (IOException ex) {
454                         ex.printStackTrace();
455                 } finally {
456                         if (input != null) {
457                                 try {
458                                         input.close();
459                                 } catch (IOException e) {
460                                         e.printStackTrace();
461                                 }
462                         }
463                 }
464
465                 return value.replaceAll("\"","");
466         }
467     
468         public static  String getExecutionHostAddress() {
469                 
470                 String computerName = null;
471                 try {
472                            computerName = InetAddress.getLocalHost().getHostAddress().replaceAll("\\.", "&middot;");
473                            System.out.println(computerName);
474                           if (computerName.indexOf(".") > -1)
475                             computerName = computerName.substring(0,
476                                 computerName.indexOf(".")).toUpperCase();
477                         } catch (UnknownHostException e) {
478                                 System.out.println("Uknown hostAddress");
479                         }
480                         return computerName != null ? computerName : "Uknown hostAddress";
481         }
482         
483         public static Map<?, ?> loadCredentialsFile(String path, String filename) throws Exception {
484                 File credentialsFileRemote = new File(path + filename);
485                 Map<?, ?> yamlFile = FileHandling.parseYamlFile(credentialsFileRemote.getAbsolutePath());
486                 return yamlFile;
487         }
488         
489 }