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.onap.sdc.backend.ci.tests.utils.general;
23 import static org.testng.AssertJUnit.assertTrue;
25 import com.aventstack.extentreports.Status;
26 import java.io.BufferedWriter;
28 import java.io.FileInputStream;
29 import java.io.FileWriter;
30 import java.io.FilenameFilter;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Enumeration;
36 import java.util.HashMap;
37 import java.util.List;
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 org.apache.commons.io.FileUtils;
44 import org.onap.sdc.backend.ci.tests.datatypes.enums.XnfTypeEnum;
45 import org.openecomp.sdc.be.model.DataTypeDefinition;
46 import org.onap.sdc.backend.ci.tests.api.ComponentBaseTest;
47 import org.openecomp.sdc.common.util.GeneralUtility;
48 import org.yaml.snakeyaml.Yaml;
50 public class FileHandling {
52 // ------------------yaml parser methods----------------------------
53 public static Map<?, ?> parseYamlFile(String filePath) throws Exception {
54 Yaml yaml = new Yaml();
55 File file = new File(filePath);
56 InputStream inputStream = new FileInputStream(file);
57 Map<?, ?> map = (Map<?, ?>) yaml.load(inputStream);
62 * The method return map fetched objects by pattern from yaml file
68 public static Map<String, Object> parseYamlFileToMapByPattern(File yamlFile, String pattern) throws Exception {
69 Map<?, ?> yamlFileToMap = FileHandling.parseYamlFile(yamlFile.toString());
70 Map<String, Object> objectMap = getObjectMapByPattern(yamlFileToMap, pattern);
74 @SuppressWarnings("unchecked")
75 public static Map<String, Object> getObjectMapByPattern(Map<?, ?> parseUpdetedEnvFile, String pattern) {
76 Map<String, Object> objectMap = null;
78 Object objectUpdetedEnvFile = parseUpdetedEnvFile.get(pattern);
79 if(objectUpdetedEnvFile instanceof HashMap){
80 objectMap = (Map<String, Object>) objectUpdetedEnvFile;
86 public static Map<String, DataTypeDefinition> parseDataTypesYaml(String filePath) throws Exception {
87 @SuppressWarnings("unchecked")
88 Map<String, DataTypeDefinition> dataTypesMap = (Map<String, DataTypeDefinition>) parseYamlFile(filePath);
91 // -------------------------------------------------------------------------------------------------
94 * @param folder, folder name under "Files" folder
95 * @return path to given folder from perspective of working directory or sdc-vnfs repository
97 public static String getFilePath(String folder) {
98 // return folder from perspective of working directory ( in general for nightly run from Linux, should already contain "Files" directory )
99 return FileHandling.getBasePath() + "src/test/resources/Files" + File.separator + folder + File.separator;
102 public static String getBasePath() {
103 return System.getProperty("user.dir") + File.separator;
106 public static String getSdcVnfsPath() {
107 return getBasePath() + Paths.get("..", "..", "sdc-vnfs").toString();
110 public static String getDriversPath() {
111 return getBasePath() + "src" + File.separator + "test" + File.separator + "resources"
112 + File.separator + "ci" + File.separator + "drivers" + File.separator;
115 public static String getResourcesFilesPath() {
116 return getSdcVnfsPath()+ File.separator + "ui-tests" + File.separator + "Files" + File.separator;
119 public static String getResourcesEnvFilesPath() {
120 return getBasePath() + File.separator + "src" + File.separator + "test" + File.separator + "resources"
121 + File.separator + "Files" + File.separator + "ResourcesEnvFiles" +File.separator;
124 public static String getCiFilesPath() {
125 return getBasePath() + "src" + File.separator + "test" + File.separator + "resources"
126 + File.separator + "ci";
129 public static String getConfFilesPath() {
130 return getCiFilesPath() + File.separator + "conf" + File.separator;
133 public static String getVnfRepositoryPath() {
134 return getFilePath("VNFs");
137 public static String getXnfRepositoryPath(XnfTypeEnum xnfTypeEnum) {
138 return xnfTypeEnum.getValue().equals(XnfTypeEnum.PNF.name()) ? getFilePath("PNFs") : getFilePath("VNFs");
141 public static String getPortMirroringRepositoryPath() {
142 return getFilePath("PortMirroring");
145 public static File getConfigFile(String configFileName) throws Exception {
146 File configFile = new File(FileHandling.getBasePath() + File.separator + "conf" + File.separator + configFileName);
147 if (!configFile.exists()) {
148 configFile = new File(FileHandling.getConfFilesPath() + configFileName);
153 public static Object[] filterFileNamesFromFolder(String filepath, String extension) {
155 File dir = new File(filepath);
156 List<String> filenames = new ArrayList<String>();
158 FilenameFilter extensionFilter = new FilenameFilter() {
159 public boolean accept(File dir, String name) {
160 return name.endsWith(extension);
164 if (dir.isDirectory()) {
165 for (File file : dir.listFiles(extensionFilter)) {
166 filenames.add(file.getName());
168 return filenames.toArray();
171 } catch (Exception e) {
177 public static List<String> filterFileNamesListFromFolder(String filepath, String extension) {
178 List<String> filenames = new ArrayList<String>();
180 File dir = new File(filepath);
182 FilenameFilter extensionFilter = new FilenameFilter() {
183 public boolean accept(File dir, String name) {
184 return name.endsWith(extension);
188 if (dir.isDirectory()) {
189 for (File file : dir.listFiles(extensionFilter)) {
190 filenames.add(file.getName());
195 } catch (Exception e) {
201 public static String[] getArtifactsFromZip(String filepath, String zipFilename){
203 ZipFile zipFile = new ZipFile(filepath + File.separator + zipFilename);
204 Enumeration<? extends ZipEntry> entries = zipFile.entries();
206 String[] artifactNames = new String[zipFile.size() - 1];
209 while(entries.hasMoreElements()){
210 ZipEntry nextElement = entries.nextElement();
211 if (!nextElement.isDirectory()){
212 if (!nextElement.getName().equals("MANIFEST.json")){
213 String name = nextElement.getName();
214 artifactNames[i++] = name;
219 return artifactNames;
220 } catch(ZipException zipEx) {
221 System.err.println("Error in zip file named : " + zipFilename);
222 zipEx.printStackTrace();
223 } catch (IOException e) {
224 System.err.println("Unhandled exception : ");
232 public static List<String> getFileNamesFromZip(String zipFileLocation){
234 ZipFile zipFile = new ZipFile(zipFileLocation);
235 Enumeration<? extends ZipEntry> entries = zipFile.entries();
237 List<String> artifactNames = new ArrayList<>();
240 while(entries.hasMoreElements()){
241 ZipEntry nextElement = entries.nextElement();
242 if (!nextElement.isDirectory()){
243 String name = nextElement.getName();
244 artifactNames.add(name);
248 return artifactNames;
250 catch(ZipException zipEx){
251 System.err.println("Error in zip file named : " + zipFileLocation);
252 zipEx.printStackTrace();
253 } catch (IOException e) {
254 System.err.println("Unhandled exception : ");
260 public static List<String> getZipFileNamesFromFolder(String filepath) {
261 List<String> fileNamesListFromFolder = filterFileNamesListFromFolder(filepath, ".zip");
262 fileNamesListFromFolder.addAll(filterFileNamesListFromFolder(filepath, ".csar"));
263 return fileNamesListFromFolder;
266 public static int countFilesInZipFile(String[] artifactsArr, String reqExtension){
268 for (String artifact : artifactsArr){
269 String extensionFile = artifact.substring(artifact.lastIndexOf(".") + 1 , artifact.length());
270 if (extensionFile.equals(reqExtension)){
279 * @return last modified file name from dirPath directory
281 public static synchronized File getLastModifiedFileNameFromDir(String dirPath){
282 File dir = new File(dirPath);
283 File[] files = dir.listFiles();
285 assertTrue("File not found under directory " + dirPath, false);
289 File lastModifiedFile = files[0];
290 for (int i = 1; i < files.length; i++) {
291 if(files[i].isDirectory()) {
294 if (lastModifiedFile.lastModified() < files[i].lastModified()) {
295 lastModifiedFile = files[i];
298 return lastModifiedFile;
301 public static void deleteDirectory(String directoryPath) {
302 File dir = new File(directoryPath);
304 FileUtils.cleanDirectory(dir);
305 } catch (IllegalArgumentException e) {
306 System.out.println("Failed to clean " + dir);
307 } catch (IOException e) {
308 System.out.println("Failed to clean " + dir);
312 public static void createDirectory(String directoryPath) {
313 File directory = new File(String.valueOf(directoryPath));
314 if (! directory.exists()){
321 * The method append data to existing file, if file not exists - create it
324 * @param leftSpaceCount
325 * @throws IOException
327 public static synchronized void writeToFile(File pathToFile, Object text, Integer leftSpaceCount) throws IOException{
329 BufferedWriter bw = null;
330 FileWriter fw = null;
331 if(!pathToFile.exists()){
332 createEmptyFile(pathToFile);
335 fw = new FileWriter(pathToFile, true);
336 bw = new BufferedWriter(fw);
337 StringBuilder sb = new StringBuilder();
338 if(leftSpaceCount > 0 ){
339 for(int i = 0; i < leftSpaceCount; i++){
343 bw.write(sb.toString() + text);
347 } catch (Exception e) {
348 ComponentBaseTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
352 public static String getCreateDirByName(String dirName) {
353 File dir = new File(dirName);
358 return dir.getPath();
361 public static boolean isFileDownloaded(String downloadPath, String fileName) {
362 boolean flag = false;
363 File dir = new File(downloadPath);
364 File[] dir_contents = dir.listFiles();
365 for (int i = 0; i < dir_contents.length; i++) {
366 if (dir_contents[i].getName().equals(fileName))
372 public static String getMD5OfFile(File file) throws IOException {
373 String content = FileUtils.readFileToString(file);
374 String md5 = GeneralUtility.calculateMD5Base64EncodedByString(content);
378 public static File createEmptyFile(String fileToCreate) {
379 File file= new File(fileToCreate);
384 file.createNewFile();
385 ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
386 } catch (IOException e) {
387 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
393 public static File createEmptyFile(File fileToCreate) {
395 if(fileToCreate.exists()){
396 deleteFile(fileToCreate);
398 fileToCreate.createNewFile();
399 ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
400 } catch (IOException e) {
401 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
407 public static void deleteFile(File file){
412 ComponentBaseTest.getExtendTest().log(Status.INFO, "File " + file.getName() + "has been deleted");
414 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to delete file " + file.getName());
424 * get file list from directory by extension array
426 * @param okFileExtensions
429 public static List<File> getHeatAndHeatEnvArtifactsFromZip(File directory, String[] okFileExtensions){
431 List<File> fileList = new ArrayList<>();
432 File[] files = directory.listFiles();
434 for (String extension : okFileExtensions){
435 for(File file : files){
436 if (file.getName().toLowerCase().endsWith(extension)){
444 public static String getKeyByValueFromPropertyFormatFile(String fullPath, String key) {
445 Properties prop = new Properties();
446 InputStream input = null;
449 input = new FileInputStream(fullPath);
451 value = (prop.getProperty(key));
453 } catch (IOException ex) {
454 ex.printStackTrace();
459 } catch (IOException e) {
465 return value.replaceAll("\"","");
468 public static void overWriteExistindDir(String outputCsar) throws IOException {
469 String basePath = getBasePath();
470 String csarDir = FileHandling.getCreateDirByName("target/outputCsar");
471 FileUtils.cleanDirectory(new File(csarDir));