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 static org.testng.AssertJUnit.assertTrue;
25 import java.io.BufferedOutputStream;
26 import java.io.BufferedWriter;
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;
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;
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;
53 import com.aventstack.extentreports.Status;
55 public class FileHandling {
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);
67 * The method return map fetched objects by pattern from yaml file
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);
79 @SuppressWarnings("unchecked")
80 public static Map<String, Object> getObjectMapByPattern(Map<?, ?> parseUpdetedEnvFile, String pattern) {
81 Map<String, Object> objectMap = null;
83 Object objectUpdetedEnvFile = parseUpdetedEnvFile.get(pattern);
84 if(objectUpdetedEnvFile instanceof HashMap){
85 objectMap = (Map<String, Object>) objectUpdetedEnvFile;
91 public static Map<String, DataTypeDefinition> parseDataTypesYaml(String filePath) throws Exception {
92 @SuppressWarnings("unchecked")
93 Map<String, DataTypeDefinition> dataTypesMap = (Map<String, DataTypeDefinition>) parseYamlFile(filePath);
96 // -------------------------------------------------------------------------------------------------
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;
104 else if(filepath.isEmpty() && !System.getProperty("os.name").contains("Windows")){
105 filepath = FileHandling.getBasePath() + "Files" + File.separator + folder + File.separator;
108 System.out.println(filepath);
113 public static String getBasePath() {
114 return System.getProperty("user.dir") + File.separator;
117 public static String getSdcVnfsPath() {
118 return getBasePath() + Paths.get("..", "..", "sdc-vnfs").toString();
121 public static String getDriversPath() {
122 return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
123 + File.separator + "ci" + File.separator + "drivers" + File.separator;
126 public static String getResourcesFilesPath() {
127 // return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
128 // + File.separator + "Files" + File.separator;
130 return getSdcVnfsPath()+ File.separator + "ui-tests" + File.separator + "Files" + File.separator;
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;
138 public static String getCiFilesPath() {
139 return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
140 + File.separator + "ci";
143 public static String getConfFilesPath() {
144 return getCiFilesPath() + File.separator + "conf" + File.separator;
147 public static String getTestSuitesFilesPath() {
148 return getCiFilesPath() + File.separator + "testSuites" + File.separator;
151 public static String getVnfRepositoryPath() {
152 return getFilePath("VNFs");
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);
163 public static Object[] filterFileNamesFromFolder(String filepath, String extension) {
165 File dir = new File(filepath);
166 List<String> filenames = new ArrayList<String>();
168 FilenameFilter extensionFilter = new FilenameFilter() {
169 public boolean accept(File dir, String name) {
170 return name.endsWith(extension);
174 if (dir.isDirectory()) {
175 for (File file : dir.listFiles(extensionFilter)) {
176 filenames.add(file.getName());
178 return filenames.toArray();
181 } catch (Exception e) {
187 public static List<String> filterFileNamesListFromFolder(String filepath, String extension) {
188 List<String> filenames = new ArrayList<String>();
190 File dir = new File(filepath);
192 FilenameFilter extensionFilter = new FilenameFilter() {
193 public boolean accept(File dir, String name) {
194 return name.endsWith(extension);
198 if (dir.isDirectory()) {
199 for (File file : dir.listFiles(extensionFilter)) {
200 filenames.add(file.getName());
205 } catch (Exception e) {
211 public static String[] getArtifactsFromZip(String filepath, String zipFilename){
213 ZipFile zipFile = new ZipFile(filepath + File.separator + zipFilename);
214 Enumeration<? extends ZipEntry> entries = zipFile.entries();
216 String[] artifactNames = new String[zipFile.size() - 1];
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;
229 return artifactNames;
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 : ");
243 // public static Object[] getZipFileNamesFromFolder(String filepath) {
244 // return filterFileNamesFromFolder(filepath, ".zip");
247 public static List<String> getZipFileNamesFromFolder(String filepath) {
248 List<String> fileNamesListFromFolder = filterFileNamesListFromFolder(filepath, ".zip");
249 fileNamesListFromFolder.addAll(filterFileNamesListFromFolder(filepath, ".csar"));
250 return fileNamesListFromFolder;
253 public static int countFilesInZipFile(String[] artifactsArr, String reqExtension){
255 for (String artifact : artifactsArr){
256 String extensionFile = artifact.substring(artifact.lastIndexOf(".") + 1 , artifact.length());
257 if (extensionFile.equals(reqExtension)){
266 * @return last modified file name from default directory
269 // TODO add download directory capability
270 // public static synchronized File getLastModifiedFileNameFromDir() throws Exception{
271 // return getLastModifiedFileNameFromDir(ComponentBaseTest.getWindowTest().getDownloadDirectory());
276 * @return last modified file name from dirPath directory
278 public static synchronized File getLastModifiedFileNameFromDir(String dirPath){
279 File dir = new File(dirPath);
280 File[] files = dir.listFiles();
282 assertTrue("File not found under directory " + dirPath, false);
286 File lastModifiedFile = files[0];
287 for (int i = 1; i < files.length; i++) {
288 if(files[i].isDirectory()) {
291 if (lastModifiedFile.lastModified() < files[i].lastModified()) {
292 lastModifiedFile = files[i];
295 return lastModifiedFile;
298 public static void deleteDirectory(String directoryPath) {
299 File dir = new File(directoryPath);
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);
308 public static void createDirectory(String directoryPath) {
309 File directory = new File(String.valueOf(directoryPath));
310 if (! directory.exists()){
317 * The method append data to existing file, if file not exists - create it
320 * @param leftSpaceCount
321 * @throws IOException
323 public static synchronized void writeToFile(File pathToFile, Object text, Integer leftSpaceCount) throws IOException{
325 BufferedWriter bw = null;
326 FileWriter fw = null;
327 if(!pathToFile.exists()){
328 createEmptyFile(pathToFile);
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++){
339 bw.write(sb.toString() + text);
343 } catch (Exception e) {
344 ComponentBaseTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
348 // public static synchronized void writeToFile(File pathToFile, Map<String, Pair<String, Object>> dataMap, Integer leftSpaceCount) throws IOException{
350 // BufferedWriter bw = null;
351 // FileWriter fw = null;
353 // if(!pathToFile.exists()){
354 // createEmptyFile(pathToFile);
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++){
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);
371 // } catch (Exception e) {
372 // ComponentBaseTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
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();
382 // TODO add work with directory capaability
383 // public static void cleanCurrentDownloadDir() throws IOException {
385 // ExtentTestActions.log(Status.INFO, "Cleaning directory " + ComponentBaseTest.getWindowTest().getDownloadDirectory());
387 // FileUtils.cleanDirectory(new File(ComponentBaseTest.getWindowTest().getDownloadDirectory()));
389 // catch(Exception e){
394 public static String getCreateDirByName(String dirName) {
395 File dir = new File(dirName);
400 return dir.getPath();
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))
414 public static String getMD5OfFile(File file) throws IOException {
415 String content = FileUtils.readFileToString(file);
416 String md5 = GeneralUtility.calculateMD5Base64EncodedByString(content);
420 public static File createEmptyFile(String fileToCreate) {
421 File file= new File(fileToCreate);
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);
435 public static File createEmptyFile(File fileToCreate) {
437 if(fileToCreate.exists()){
438 deleteFile(fileToCreate);
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);
449 public static void deleteFile(File file){
454 ComponentBaseTest.getExtendTest().log(Status.INFO, "File " + file.getName() + "has been deleted");
456 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to delete file " + file.getName());
466 * get file list from directory by extension array
468 * @param okFileExtensions
471 public static List<File> getHeatAndHeatEnvArtifactsFromZip(File directory, String[] okFileExtensions){
473 List<File> fileList = new ArrayList<>();
474 File[] files = directory.listFiles();
476 for (String extension : okFileExtensions){
477 for(File file : files){
478 if (file.getName().toLowerCase().endsWith(extension)){
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()) {
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) {
497 // if(System.getProperty("os.name").contains("Windows")){
498 // entryName = entry.getName().replaceAll("/", "\\"+File.separator);
500 // entryName = entry.getName();
502 // String filePath = destDirectory + entryName;
503 // String currPath = destDirectory;
504 // String[] dirs = entryName.split("\\"+File.separator);
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);
511 // if(currPath.endsWith(File.separator)){
512 // currPath = currPath + currToken;
514 // currPath = currPath + File.separator + currToken;
516 //// if the entry is a directory, make the directory
517 // File dir = new File(currPath);
521 // zipIn.closeEntry();
522 // entry = zipIn.getNextEntry();
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];
531 while ((read = zipIn.read(bytesIn)) != -1) {
532 bos.write(bytesIn, 0, read);
537 // public static int getFileCountFromDefaulDownloadDirectory(){
538 // return new File(ComponentBaseTest.getWindowTest().getDownloadDirectory()).listFiles().length;
542 public static String getKeyByValueFromPropertyFormatFile(String fullPath, String key) {
543 Properties prop = new Properties();
544 InputStream input = null;
547 input = new FileInputStream(fullPath);
549 value = (prop.getProperty(key));
551 } catch (IOException ex) {
552 ex.printStackTrace();
557 } catch (IOException e) {
563 return value.replaceAll("\"","");