2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.utils.general;
23 import com.aventstack.extentreports.Status;
24 import static org.testng.AssertJUnit.assertTrue;
26 import java.io.BufferedOutputStream;
27 import java.io.BufferedWriter;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.io.FileWriter;
32 import java.io.FilenameFilter;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.nio.file.Paths;
36 import java.util.ArrayList;
37 import java.util.Enumeration;
38 import java.util.HashMap;
39 import java.util.List;
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;
47 import org.apache.commons.io.FileUtils;
48 import org.openecomp.sdc.be.model.DataTypeDefinition;
49 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
50 import org.openecomp.sdc.ci.tests.config.Config;
51 import org.openecomp.sdc.common.util.GeneralUtility;
52 import org.yaml.snakeyaml.Yaml;
54 import com.aventstack.extentreports.Status;
56 import java.nio.file.Paths;
58 import java.util.zip.ZipEntry;
59 import java.util.zip.ZipException;
60 import java.util.zip.ZipFile;
61 import java.util.zip.ZipInputStream;
63 import static org.testng.AssertJUnit.assertTrue;
65 public class FileHandling {
67 // ------------------yaml parser methods----------------------------
68 public static Map<?, ?> parseYamlFile(String filePath) throws Exception {
69 Yaml yaml = new Yaml();
70 File file = new File(filePath);
71 InputStream inputStream = new FileInputStream(file);
72 Map<?, ?> map = (Map<?, ?>) yaml.load(inputStream);
77 * The method return map fetched objects by pattern from yaml file
83 public static Map<String, Object> parseYamlFileToMapByPattern(File yamlFile, String pattern) throws Exception {
84 Map<?, ?> yamlFileToMap = FileHandling.parseYamlFile(yamlFile.toString());
85 Map<String, Object> objectMap = getObjectMapByPattern(yamlFileToMap, pattern);
89 @SuppressWarnings("unchecked")
90 public static Map<String, Object> getObjectMapByPattern(Map<?, ?> parseUpdetedEnvFile, String pattern) {
91 Map<String, Object> objectMap = null;
93 Object objectUpdetedEnvFile = parseUpdetedEnvFile.get(pattern);
94 if(objectUpdetedEnvFile instanceof HashMap){
95 objectMap = (Map<String, Object>) objectUpdetedEnvFile;
101 public static Map<String, DataTypeDefinition> parseDataTypesYaml(String filePath) throws Exception {
102 @SuppressWarnings("unchecked")
103 Map<String, DataTypeDefinition> dataTypesMap = (Map<String, DataTypeDefinition>) parseYamlFile(filePath);
106 // -------------------------------------------------------------------------------------------------
109 * @param folder, folder name under "Files" folder
110 * @return path to given folder from perspective of working directory or sdc-vnfs repository
112 public static String getFilePath(String folder) {
113 String filepath = System.getProperty("filePath");
114 boolean isFilePathEmptyOrNull = (filepath == null || filepath.isEmpty());
116 // return folder from perspective of sdc-vnfs repository
117 if (isFilePathEmptyOrNull && ( System.getProperty("os.name").contains("Windows") || System.getProperty("os.name").contains("Mac"))) {
118 return FileHandling.getResourcesFilesPath() + folder + File.separator;
121 // return folder from perspective of working directory ( in general for nightly run from Linux, should already contain "Files" directory )
122 return FileHandling.getBasePath() + "Files" + File.separator + folder + File.separator;
125 public static String getBasePath() {
126 return System.getProperty("user.dir") + File.separator;
129 public static String getSdcVnfsPath() {
130 return getBasePath() + Paths.get("..", "..", "sdc-vnfs").toString();
133 public static String getDriversPath() {
134 return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
135 + File.separator + "ci" + File.separator + "drivers" + File.separator;
138 public static String getResourcesFilesPath() {
139 // return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
140 // + File.separator + "Files" + File.separator;
142 return getSdcVnfsPath()+ File.separator + "ui-tests" + File.separator + "Files" + File.separator;
145 public static String getResourcesEnvFilesPath() {
146 return getBasePath() + File.separator + "src" + File.separator + "main" + File.separator + "resources"
147 + File.separator + "Files" + File.separator + "ResourcesEnvFiles" +File.separator;
150 public static String getCiFilesPath() {
151 return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
152 + File.separator + "ci";
155 public static String getConfFilesPath() {
156 return getCiFilesPath() + File.separator + "conf" + File.separator;
159 public static String getTestSuitesFilesPath() {
160 return getCiFilesPath() + File.separator + "testSuites" + File.separator;
163 public static String getVnfRepositoryPath() {
164 return getFilePath("VNFs");
167 public static String getPortMirroringRepositoryPath() {
168 return getFilePath("PortMirroring");
171 public static File getConfigFile(String configFileName) throws Exception {
172 File configFile = new File(FileHandling.getBasePath() + File.separator + "conf" + File.separator + configFileName);
173 if (!configFile.exists()) {
174 configFile = new File(FileHandling.getConfFilesPath() + configFileName);
179 public static Object[] filterFileNamesFromFolder(String filepath, String extension) {
181 File dir = new File(filepath);
182 List<String> filenames = new ArrayList<String>();
184 FilenameFilter extensionFilter = new FilenameFilter() {
185 public boolean accept(File dir, String name) {
186 return name.endsWith(extension);
190 if (dir.isDirectory()) {
191 for (File file : dir.listFiles(extensionFilter)) {
192 filenames.add(file.getName());
194 return filenames.toArray();
197 } catch (Exception e) {
203 public static List<String> filterFileNamesListFromFolder(String filepath, String extension) {
204 List<String> filenames = new ArrayList<String>();
206 File dir = new File(filepath);
208 FilenameFilter extensionFilter = new FilenameFilter() {
209 public boolean accept(File dir, String name) {
210 return name.endsWith(extension);
214 if (dir.isDirectory()) {
215 for (File file : dir.listFiles(extensionFilter)) {
216 filenames.add(file.getName());
221 } catch (Exception e) {
227 public static String[] getArtifactsFromZip(String filepath, String zipFilename){
229 ZipFile zipFile = new ZipFile(filepath + File.separator + zipFilename);
230 Enumeration<? extends ZipEntry> entries = zipFile.entries();
232 String[] artifactNames = new String[zipFile.size() - 1];
235 while(entries.hasMoreElements()){
236 ZipEntry nextElement = entries.nextElement();
237 if (!nextElement.isDirectory()){
238 if (!nextElement.getName().equals("MANIFEST.json")){
239 String name = nextElement.getName();
240 artifactNames[i++] = name;
245 return artifactNames;
246 } catch(ZipException zipEx) {
247 System.err.println("Error in zip file named : " + zipFilename);
248 zipEx.printStackTrace();
249 } catch (IOException e) {
250 System.err.println("Unhandled exception : ");
258 public static List<String> getFileNamesFromZip(String zipFileLocation){
260 ZipFile zipFile = new ZipFile(zipFileLocation);
261 Enumeration<? extends ZipEntry> entries = zipFile.entries();
263 List<String> artifactNames = new ArrayList<>();
266 while(entries.hasMoreElements()){
267 ZipEntry nextElement = entries.nextElement();
268 if (!nextElement.isDirectory()){
269 String name = nextElement.getName();
270 artifactNames.add(name);
274 return artifactNames;
276 catch(ZipException zipEx){
277 System.err.println("Error in zip file named : " + zipFileLocation);
278 zipEx.printStackTrace();
279 } catch (IOException e) {
280 System.err.println("Unhandled exception : ");
286 // public static Object[] getZipFileNamesFromFolder(String filepath) {
287 // return filterFileNamesFromFolder(filepath, ".zip");
290 public static List<String> getZipFileNamesFromFolder(String filepath) {
291 List<String> fileNamesListFromFolder = filterFileNamesListFromFolder(filepath, ".zip");
292 fileNamesListFromFolder.addAll(filterFileNamesListFromFolder(filepath, ".csar"));
293 return fileNamesListFromFolder;
296 public static int countFilesInZipFile(String[] artifactsArr, String reqExtension){
298 for (String artifact : artifactsArr){
299 String extensionFile = artifact.substring(artifact.lastIndexOf(".") + 1 , artifact.length());
300 if (extensionFile.equals(reqExtension)){
309 * @return last modified file name from default directory
312 // TODO add download directory capability
313 // public static synchronized File getLastModifiedFileNameFromDir() throws Exception{
314 // return getLastModifiedFileNameFromDir(ComponentBaseTest.getWindowTest().getDownloadDirectory());
319 * @return last modified file name from dirPath directory
321 public static synchronized File getLastModifiedFileNameFromDir(String dirPath){
322 File dir = new File(dirPath);
323 File[] files = dir.listFiles();
325 assertTrue("File not found under directory " + dirPath, false);
329 File lastModifiedFile = files[0];
330 for (int i = 1; i < files.length; i++) {
331 if(files[i].isDirectory()) {
334 if (lastModifiedFile.lastModified() < files[i].lastModified()) {
335 lastModifiedFile = files[i];
338 return lastModifiedFile;
341 public static void deleteDirectory(String directoryPath) {
342 File dir = new File(directoryPath);
344 FileUtils.cleanDirectory(dir);
345 } catch (IOException e) {
346 System.out.println("Failed to delete " + dir);
347 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to delete " + dir);
351 public static void createDirectory(String directoryPath) {
352 File directory = new File(String.valueOf(directoryPath));
353 if (! directory.exists()){
360 * The method append data to existing file, if file not exists - create it
363 * @param leftSpaceCount
364 * @throws IOException
366 public static synchronized void writeToFile(File pathToFile, Object text, Integer leftSpaceCount) throws IOException{
368 BufferedWriter bw = null;
369 FileWriter fw = null;
370 if(!pathToFile.exists()){
371 createEmptyFile(pathToFile);
374 fw = new FileWriter(pathToFile, true);
375 bw = new BufferedWriter(fw);
376 StringBuilder sb = new StringBuilder();
377 if(leftSpaceCount > 0 ){
378 for(int i = 0; i < leftSpaceCount; i++){
382 bw.write(sb.toString() + text);
386 } catch (Exception e) {
387 ComponentBaseTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
391 // public static synchronized void writeToFile(File pathToFile, Map<String, Pair<String, Object>> dataMap, Integer leftSpaceCount) throws IOException{
393 // BufferedWriter bw = null;
394 // FileWriter fw = null;
396 // if(!pathToFile.exists()){
397 // createEmptyFile(pathToFile);
399 // fw = new FileWriter(pathToFile, true);
400 // bw = new BufferedWriter(fw);
401 // StringBuilder sb = new StringBuilder();
402 // if(leftSpaceCount > 0 ){
403 // for(int i = 0; i < leftSpaceCount; i++){
407 // for(Map.Entry<String, Pair<String, Object>> entry : dataMap.entrySet()){
408 // Object record = ArtifactUIUtils.getFormatedData(entry.getKey(), entry.getValue().right);
409 // bw.write(sb.toString() + record);
414 // } catch (Exception e) {
415 // ComponentBaseTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
419 public static void deleteLastDowloadedFiles(List<File> files) throws IOException {
420 for (File file : files) {
421 File fileToDelete =new File(Config.instance().getWindowsDownloadDirectory()+file.getName());
422 fileToDelete.delete();
425 // TODO add work with directory capaability
426 // public static void cleanCurrentDownloadDir() throws IOException {
428 // ExtentTestActions.log(Status.INFO, "Cleaning directory " + ComponentBaseTest.getWindowTest().getDownloadDirectory());
430 // FileUtils.cleanDirectory(new File(ComponentBaseTest.getWindowTest().getDownloadDirectory()));
432 // catch(Exception e){
437 public static String getCreateDirByName(String dirName) {
438 File dir = new File(dirName);
443 return dir.getPath();
446 public static boolean isFileDownloaded(String downloadPath, String fileName) {
447 boolean flag = false;
448 File dir = new File(downloadPath);
449 File[] dir_contents = dir.listFiles();
450 for (int i = 0; i < dir_contents.length; i++) {
451 if (dir_contents[i].getName().equals(fileName))
457 public static String getMD5OfFile(File file) throws IOException {
458 String content = FileUtils.readFileToString(file);
459 String md5 = GeneralUtility.calculateMD5Base64EncodedByString(content);
463 public static File createEmptyFile(String fileToCreate) {
464 File file= new File(fileToCreate);
469 file.createNewFile();
470 ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
471 } catch (IOException e) {
472 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
478 public static File createEmptyFile(File fileToCreate) {
480 if(fileToCreate.exists()){
481 deleteFile(fileToCreate);
483 fileToCreate.createNewFile();
484 ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
485 } catch (IOException e) {
486 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
492 public static void deleteFile(File file){
497 ComponentBaseTest.getExtendTest().log(Status.INFO, "File " + file.getName() + "has been deleted");
499 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to delete file " + file.getName());
509 * get file list from directory by extension array
511 * @param okFileExtensions
514 public static List<File> getHeatAndHeatEnvArtifactsFromZip(File directory, String[] okFileExtensions){
516 List<File> fileList = new ArrayList<>();
517 File[] files = directory.listFiles();
519 for (String extension : okFileExtensions){
520 for(File file : files){
521 if (file.getName().toLowerCase().endsWith(extension)){
529 private static final int BUFFER_SIZE = 4096;
530 // public static void unzip(String zipFilePath, String destDirectory) throws IOException {
531 // File destDir = new File(destDirectory);
532 // if (!destDir.exists()) {
535 // ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
536 // ZipEntry entry = zipIn.getNextEntry();
537 //// iterates over entries in the zip file
538 // while (entry != null) {
540 // if(System.getProperty("os.name").contains("Windows")){
541 // entryName = entry.getName().replaceAll("/", "\\"+File.separator);
543 // entryName = entry.getName();
545 // String filePath = destDirectory + entryName;
546 // String currPath = destDirectory;
547 // String[] dirs = entryName.split("\\"+File.separator);
549 // for(int i = 0; i<dirs.length;++i){
550 // currToken = dirs[i];
551 // if(!entry.isDirectory() && i==dirs.length-1){
552 // extractFile(zipIn, filePath);
554 // if(currPath.endsWith(File.separator)){
555 // currPath = currPath + currToken;
557 // currPath = currPath + File.separator + currToken;
559 //// if the entry is a directory, make the directory
560 // File dir = new File(currPath);
564 // zipIn.closeEntry();
565 // entry = zipIn.getNextEntry();
570 private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
571 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
572 byte[] bytesIn = new byte[BUFFER_SIZE];
574 while ((read = zipIn.read(bytesIn)) != -1) {
575 bos.write(bytesIn, 0, read);
580 // public static int getFileCountFromDefaulDownloadDirectory(){
581 // return new File(ComponentBaseTest.getWindowTest().getDownloadDirectory()).listFiles().length;
585 public static String getKeyByValueFromPropertyFormatFile(String fullPath, String key) {
586 Properties prop = new Properties();
587 InputStream input = null;
590 input = new FileInputStream(fullPath);
592 value = (prop.getProperty(key));
594 } catch (IOException ex) {
595 ex.printStackTrace();
600 } catch (IOException e) {
606 return value.replaceAll("\"","");
609 public static void overWriteExistindDir(String outputCsar) throws IOException {
610 String basePath = getBasePath();
611 String csarDir = FileHandling.getCreateDirByName("outputCsar");
612 FileUtils.cleanDirectory(new File(csarDir));