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 com.clearspring.analytics.util.Pair;
25 import org.apache.commons.io.FileUtils;
26 import org.openecomp.sdc.be.model.DataTypeDefinition;
27 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
28 import org.openecomp.sdc.ci.tests.config.Config;
30 import org.openecomp.sdc.common.util.GeneralUtility;
31 import org.yaml.snakeyaml.Yaml;
34 import java.nio.file.Paths;
36 import java.util.zip.ZipEntry;
37 import java.util.zip.ZipException;
38 import java.util.zip.ZipFile;
39 import java.util.zip.ZipInputStream;
41 import static org.testng.AssertJUnit.assertTrue;
43 public class FileHandling {
45 // ------------------yaml parser methods----------------------------
46 public static Map<?, ?> parseYamlFile(String filePath) throws Exception {
47 Yaml yaml = new Yaml();
48 File file = new File(filePath);
49 InputStream inputStream = new FileInputStream(file);
50 Map<?, ?> map = (Map<?, ?>) yaml.load(inputStream);
55 * The method return map fetched objects by pattern from yaml file
61 public static Map<String, Object> parseYamlFileToMapByPattern(File yamlFile, String pattern) throws Exception {
62 Map<?, ?> yamlFileToMap = FileHandling.parseYamlFile(yamlFile.toString());
63 Map<String, Object> objectMap = getObjectMapByPattern(yamlFileToMap, pattern);
67 @SuppressWarnings("unchecked")
68 public static Map<String, Object> getObjectMapByPattern(Map<?, ?> parseUpdetedEnvFile, String pattern) {
69 Map<String, Object> objectMap = null;
71 Object objectUpdetedEnvFile = parseUpdetedEnvFile.get(pattern);
72 if(objectUpdetedEnvFile instanceof HashMap){
73 objectMap = (Map<String, Object>) objectUpdetedEnvFile;
79 public static Map<String, DataTypeDefinition> parseDataTypesYaml(String filePath) throws Exception {
80 @SuppressWarnings("unchecked")
81 Map<String, DataTypeDefinition> dataTypesMap = (Map<String, DataTypeDefinition>) parseYamlFile(filePath);
84 // -------------------------------------------------------------------------------------------------
86 public static String getFilePath(String folder) {
87 String filepath = System.getProperty("filepath");
88 if (filepath == null && System.getProperty("os.name").contains("Windows")) {
89 filepath = FileHandling.getResourcesFilesPath() + folder + File.separator;
92 else if(filepath.isEmpty() && !System.getProperty("os.name").contains("Windows")){
93 filepath = FileHandling.getBasePath() + "Files" + File.separator + folder + File.separator;
96 System.out.println(filepath);
101 public static String getBasePath() {
102 return System.getProperty("user.dir") + File.separator;
105 public static String getSdcVnfsPath() {
106 return getBasePath() + Paths.get("..", "..", "sdc-vnfs").toString();
109 public static String getDriversPath() {
110 return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
111 + File.separator + "ci" + File.separator + "drivers" + File.separator;
114 public static String getResourcesFilesPath() {
115 // return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
116 // + File.separator + "Files" + File.separator;
118 return getSdcVnfsPath()+ File.separator + "ui-tests" + File.separator + "Files" + File.separator;
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;
126 public static String getCiFilesPath() {
127 return getBasePath() + "src" + File.separator + "main" + File.separator + "resources"
128 + File.separator + "ci";
131 public static String getConfFilesPath() {
132 return getCiFilesPath() + File.separator + "conf" + File.separator;
135 public static String getTestSuitesFilesPath() {
136 return getCiFilesPath() + File.separator + "testSuites" + File.separator;
139 public static String getVnfRepositoryPath() {
140 return getFilePath("VNFs");
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);
151 public static Object[] filterFileNamesFromFolder(String filepath, String extension) {
153 File dir = new File(filepath);
154 List<String> filenames = new ArrayList<String>();
156 FilenameFilter extensionFilter = new FilenameFilter() {
157 public boolean accept(File dir, String name) {
158 return name.endsWith(extension);
162 if (dir.isDirectory()) {
163 for (File file : dir.listFiles(extensionFilter)) {
164 filenames.add(file.getName());
166 return filenames.toArray();
169 } catch (Exception e) {
175 public static List<String> filterFileNamesListFromFolder(String filepath, String extension) {
177 File dir = new File(filepath);
178 List<String> filenames = new ArrayList<String>();
180 FilenameFilter extensionFilter = new FilenameFilter() {
181 public boolean accept(File dir, String name) {
182 return name.endsWith(extension);
186 if (dir.isDirectory()) {
187 for (File file : dir.listFiles(extensionFilter)) {
188 filenames.add(file.getName());
193 } catch (Exception e) {
199 public static String[] getArtifactsFromZip(String filepath, String zipFilename){
201 ZipFile zipFile = new ZipFile(filepath + File.separator + zipFilename);
202 Enumeration<? extends ZipEntry> entries = zipFile.entries();
204 String[] artifactNames = new String[zipFile.size() - 1];
207 while(entries.hasMoreElements()){
208 ZipEntry nextElement = entries.nextElement();
209 if (!nextElement.isDirectory()){
210 if (!nextElement.getName().equals("MANIFEST.json")){
211 String name = nextElement.getName();
212 artifactNames[i++] = name;
217 return artifactNames;
219 catch(ZipException zipEx){
220 System.err.println("Error in zip file named : " + zipFilename);
221 zipEx.printStackTrace();
222 } catch (IOException e) {
223 System.err.println("Unhandled exception : ");
231 // public static Object[] getZipFileNamesFromFolder(String filepath) {
232 // return filterFileNamesFromFolder(filepath, ".zip");
235 public static List<String> getZipFileNamesFromFolder(String filepath) {
236 return filterFileNamesListFromFolder(filepath, ".zip");
239 public static int countFilesInZipFile(String[] artifactsArr, String reqExtension){
241 for (String artifact : artifactsArr){
242 String extensionFile = artifact.substring(artifact.lastIndexOf(".") + 1 , artifact.length());
243 if (extensionFile.equals(reqExtension)){
252 * @return last modified file name from default directory
255 // TODO add download directory capability
256 // public static synchronized File getLastModifiedFileNameFromDir() throws Exception{
257 // return getLastModifiedFileNameFromDir(ComponentBaseTest.getWindowTest().getDownloadDirectory());
262 * @return last modified file name from dirPath directory
264 public static synchronized File getLastModifiedFileNameFromDir(String dirPath){
265 File dir = new File(dirPath);
266 File[] files = dir.listFiles();
268 assertTrue("File not found under directory " + dirPath, false);
272 File lastModifiedFile = files[0];
273 for (int i = 1; i < files.length; i++) {
274 if(files[i].isDirectory()) {
277 if (lastModifiedFile.lastModified() < files[i].lastModified()) {
278 lastModifiedFile = files[i];
281 return lastModifiedFile;
284 public static void deleteDirectory(String directoryPath) {
285 File dir = new File(directoryPath);
287 FileUtils.cleanDirectory(dir);
288 } catch (IOException e) {
289 System.out.println("Failed to delete " + dir);
290 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to delete " + dir);
294 public static void createDirectory(String directoryPath) {
295 File directory = new File(String.valueOf(directoryPath));
296 if (! directory.exists()){
303 * The method append data to existing file, if file not exists - create it
306 * @param leftSpaceCount
307 * @throws IOException
309 public static synchronized void writeToFile(File pathToFile, Object text, Integer leftSpaceCount) throws IOException{
311 BufferedWriter bw = null;
312 FileWriter fw = null;
313 if(!pathToFile.exists()){
314 createEmptyFile(pathToFile);
317 fw = new FileWriter(pathToFile, true);
318 bw = new BufferedWriter(fw);
319 StringBuilder sb = new StringBuilder();
320 if(leftSpaceCount > 0 ){
321 for(int i = 0; i < leftSpaceCount; i++){
325 bw.write(sb.toString() + text);
329 } catch (Exception e) {
330 ComponentBaseTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
334 // public static synchronized void writeToFile(File pathToFile, Map<String, Pair<String, Object>> dataMap, Integer leftSpaceCount) throws IOException{
336 // BufferedWriter bw = null;
337 // FileWriter fw = null;
339 // if(!pathToFile.exists()){
340 // createEmptyFile(pathToFile);
342 // fw = new FileWriter(pathToFile, true);
343 // bw = new BufferedWriter(fw);
344 // StringBuilder sb = new StringBuilder();
345 // if(leftSpaceCount > 0 ){
346 // for(int i = 0; i < leftSpaceCount; i++){
350 // for(Map.Entry<String, Pair<String, Object>> entry : dataMap.entrySet()){
351 // Object record = ArtifactUIUtils.getFormatedData(entry.getKey(), entry.getValue().right);
352 // bw.write(sb.toString() + record);
357 // } catch (Exception e) {
358 // ComponentBaseTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
362 public static void deleteLastDowloadedFiles(List<File> files) throws IOException {
363 for (File file : files) {
364 File fileToDelete =new File(Config.instance().getWindowsDownloadDirectory()+file.getName());
365 fileToDelete.delete();
368 // TODO add work with directory capaability
369 // public static void cleanCurrentDownloadDir() throws IOException {
371 // ExtentTestActions.log(Status.INFO, "Cleaning directory " + ComponentBaseTest.getWindowTest().getDownloadDirectory());
373 // FileUtils.cleanDirectory(new File(ComponentBaseTest.getWindowTest().getDownloadDirectory()));
375 // catch(Exception e){
380 public static boolean isFileDownloaded(String downloadPath, String fileName) {
381 boolean flag = false;
382 File dir = new File(downloadPath);
383 File[] dir_contents = dir.listFiles();
384 for (int i = 0; i < dir_contents.length; i++) {
385 if (dir_contents[i].getName().equals(fileName))
391 public static String getMD5OfFile(File file) throws IOException {
392 String content = FileUtils.readFileToString(file);
393 String md5 = GeneralUtility.calculateMD5Base64EncodedByString(content);
397 public static File createEmptyFile(String fileToCreate) {
398 File file= new File(fileToCreate);
403 file.createNewFile();
404 ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
405 } catch (IOException e) {
406 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
412 public static File createEmptyFile(File fileToCreate) {
414 if(fileToCreate.exists()){
415 deleteFile(fileToCreate);
417 fileToCreate.createNewFile();
418 ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
419 } catch (IOException e) {
420 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
426 public static void deleteFile(File file){
431 ComponentBaseTest.getExtendTest().log(Status.INFO, "File " + file.getName() + "has been deleted");
433 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to delete file " + file.getName());
443 * get file list from directory by extension array
445 * @param okFileExtensions
448 public static List<File> getHeatAndHeatEnvArtifactsFromZip(File directory, String[] okFileExtensions){
450 List<File> fileList = new ArrayList<>();
451 File[] files = directory.listFiles();
453 for (String extension : okFileExtensions){
454 for(File file : files){
455 if (file.getName().toLowerCase().endsWith(extension)){
463 private static final int BUFFER_SIZE = 4096;
464 // public static void unzip(String zipFilePath, String destDirectory) throws IOException {
465 // File destDir = new File(destDirectory);
466 // if (!destDir.exists()) {
469 // ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
470 // ZipEntry entry = zipIn.getNextEntry();
471 //// iterates over entries in the zip file
472 // while (entry != null) {
474 // if(System.getProperty("os.name").contains("Windows")){
475 // entryName = entry.getName().replaceAll("/", "\\"+File.separator);
477 // entryName = entry.getName();
479 // String filePath = destDirectory + entryName;
480 // String currPath = destDirectory;
481 // String[] dirs = entryName.split("\\"+File.separator);
483 // for(int i = 0; i<dirs.length;++i){
484 // currToken = dirs[i];
485 // if(!entry.isDirectory() && i==dirs.length-1){
486 // extractFile(zipIn, filePath);
488 // if(currPath.endsWith(File.separator)){
489 // currPath = currPath + currToken;
491 // currPath = currPath + File.separator + currToken;
493 //// if the entry is a directory, make the directory
494 // File dir = new File(currPath);
498 // zipIn.closeEntry();
499 // entry = zipIn.getNextEntry();
504 private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
505 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
506 byte[] bytesIn = new byte[BUFFER_SIZE];
508 while ((read = zipIn.read(bytesIn)) != -1) {
509 bos.write(bytesIn, 0, read);
514 // public static int getFileCountFromDefaulDownloadDirectory(){
515 // return new File(ComponentBaseTest.getWindowTest().getDownloadDirectory()).listFiles().length;
519 public static String getKeyByValueFromPropertyFormatFile(String fullPath, String key) {
520 Properties prop = new Properties();
521 InputStream input = null;
524 input = new FileInputStream(fullPath);
526 value = (prop.getProperty(key));
528 } catch (IOException ex) {
529 ex.printStackTrace();
534 } catch (IOException e) {
540 return value.replaceAll("\"","");