2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2021 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.sdc.backend.ci.tests.utils.general;
24 import static org.testng.AssertJUnit.assertTrue;
26 import com.aventstack.extentreports.Status;
27 import java.io.BufferedWriter;
29 import java.io.FileInputStream;
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.EnumMap;
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 org.apache.commons.io.FileUtils;
46 import org.onap.sdc.backend.ci.tests.api.ComponentBaseTest;
47 import org.onap.sdc.backend.ci.tests.datatypes.enums.PackageTypeEnum;
48 import org.openecomp.sdc.be.model.DataTypeDefinition;
49 import org.openecomp.sdc.common.util.GeneralUtility;
50 import org.yaml.snakeyaml.Yaml;
52 public class FileHandling {
54 // ------------------yaml parser methods----------------------------
55 public static Map<?, ?> parseYamlFile(String filePath) throws Exception {
56 Yaml yaml = new Yaml();
57 File file = new File(filePath);
58 InputStream inputStream = new FileInputStream(file);
59 Map<?, ?> map = (Map<?, ?>) yaml.load(inputStream);
64 * The method return map fetched objects by pattern from yaml file
70 public static Map<String, Object> parseYamlFileToMapByPattern(File yamlFile, String pattern) throws Exception {
71 Map<?, ?> yamlFileToMap = FileHandling.parseYamlFile(yamlFile.toString());
72 Map<String, Object> objectMap = getObjectMapByPattern(yamlFileToMap, pattern);
76 @SuppressWarnings("unchecked")
77 public static Map<String, Object> getObjectMapByPattern(Map<?, ?> parseUpdetedEnvFile, String pattern) {
78 Map<String, Object> objectMap = null;
80 Object objectUpdetedEnvFile = parseUpdetedEnvFile.get(pattern);
81 if(objectUpdetedEnvFile instanceof HashMap){
82 objectMap = (Map<String, Object>) objectUpdetedEnvFile;
88 public static Map<String, DataTypeDefinition> parseDataTypesYaml(String filePath) throws Exception {
89 @SuppressWarnings("unchecked")
90 Map<String, DataTypeDefinition> dataTypesMap = (Map<String, DataTypeDefinition>) parseYamlFile(filePath);
93 // -------------------------------------------------------------------------------------------------
96 * @param folder, folder name under "Files" folder
97 * @return path to given folder from perspective of working directory or sdc-vnfs repository
99 public static String getFilePath(String folder) {
100 // return folder from perspective of working directory ( in general for nightly run from Linux, should already contain "Files" directory )
101 return FileHandling.getBasePath() + "src/test/resources/Files" + File.separator + folder + File.separator;
104 public static String getBasePath() {
105 return System.getProperty("user.dir") + File.separator;
108 public static String getSdcVnfsPath() {
109 return getBasePath() + Paths.get("..", "..", "sdc-vnfs").toString();
112 public static String getDriversPath() {
113 return getBasePath() + "src" + File.separator + "test" + File.separator + "resources"
114 + File.separator + "ci" + File.separator + "drivers" + File.separator;
117 public static String getResourcesFilesPath() {
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 + "test" + File.separator + "resources"
123 + File.separator + "Files" + File.separator + "ResourcesEnvFiles" +File.separator;
126 public static String getCiFilesPath() {
127 return getBasePath() + "src" + File.separator + "test" + File.separator + "resources"
128 + File.separator + "ci";
131 public static String getConfFilesPath() {
132 return getCiFilesPath() + File.separator + "conf" + File.separator;
135 private static final EnumMap<PackageTypeEnum, String> PACKAGE_REPOSITORY_PATHS_MAP = new EnumMap<>(Map.of(
136 PackageTypeEnum.PNF, getPnfRepositoryPath(),
137 PackageTypeEnum.CNF, getCnfRepositoryPath(),
138 PackageTypeEnum.CNF_HELM, getCnfRepositoryForHelmValidatorPath(),
139 PackageTypeEnum.VNF, getVnfRepositoryPath(),
140 PackageTypeEnum.ETSI, getEtsiRepositoryPath(),
141 PackageTypeEnum.VFC, getVfcRepositoryPath(),
142 PackageTypeEnum.ASD, getAsdRepositoryPath()
145 public static String getVnfRepositoryPath() {
146 return getFilePath("VNFs");
149 private static String getPnfRepositoryPath() {
150 return getFilePath("PNFs");
153 private static String getCnfRepositoryPath() {
154 return getFilePath("CNFs");
157 private static String getCnfRepositoryForHelmValidatorPath() {
158 return getFilePath("CNFs/helm_validator");
161 private static String getEtsiRepositoryPath() { return getFilePath("ETSI"); }
163 private static String getAsdRepositoryPath() { return getFilePath("ASDs"); }
165 private static String getVfcRepositoryPath() { return getFilePath("VFCs"); }
167 public static String getPackageRepositoryPath(PackageTypeEnum packageTypeEnum) {
168 return PACKAGE_REPOSITORY_PATHS_MAP.get(packageTypeEnum);
171 public static String getPortMirroringRepositoryPath() {
172 return getFilePath("PortMirroring");
175 public static File getConfigFile(String configFileName) throws Exception {
176 File configFile = new File(FileHandling.getBasePath() + File.separator + "conf" + File.separator + configFileName);
177 if (!configFile.exists()) {
178 configFile = new File(FileHandling.getConfFilesPath() + configFileName);
183 public static Object[] filterFileNamesFromFolder(String filepath, String extension) {
185 File dir = new File(filepath);
186 List<String> filenames = new ArrayList<String>();
188 FilenameFilter extensionFilter = new FilenameFilter() {
189 public boolean accept(File dir, String name) {
190 return name.endsWith(extension);
194 if (dir.isDirectory()) {
195 for (File file : dir.listFiles(extensionFilter)) {
196 filenames.add(file.getName());
198 return filenames.toArray();
201 } catch (Exception e) {
207 public static List<String> filterFileNamesListFromFolder(String filepath, String extension) {
208 List<String> filenames = new ArrayList<String>();
210 File dir = new File(filepath);
212 FilenameFilter extensionFilter = new FilenameFilter() {
213 public boolean accept(File dir, String name) {
214 return name.endsWith(extension);
218 if (dir.isDirectory()) {
219 for (File file : dir.listFiles(extensionFilter)) {
220 filenames.add(file.getName());
225 } catch (Exception e) {
231 public static String[] getArtifactsFromZip(String filepath, String zipFilename){
233 ZipFile zipFile = new ZipFile(filepath + File.separator + zipFilename);
234 Enumeration<? extends ZipEntry> entries = zipFile.entries();
236 String[] artifactNames = new String[zipFile.size() - 1];
239 while(entries.hasMoreElements()){
240 ZipEntry nextElement = entries.nextElement();
241 if (!nextElement.isDirectory()){
242 if (!nextElement.getName().equals("MANIFEST.json")){
243 String name = nextElement.getName();
244 artifactNames[i++] = name;
249 return artifactNames;
250 } catch(ZipException zipEx) {
251 System.err.println("Error in zip file named : " + zipFilename);
252 zipEx.printStackTrace();
253 } catch (IOException e) {
254 System.err.println("Unhandled exception : ");
262 public static List<String> getFileNamesFromZip(String zipFileLocation){
264 ZipFile zipFile = new ZipFile(zipFileLocation);
265 Enumeration<? extends ZipEntry> entries = zipFile.entries();
267 List<String> artifactNames = new ArrayList<>();
270 while(entries.hasMoreElements()){
271 ZipEntry nextElement = entries.nextElement();
272 if (!nextElement.isDirectory()){
273 String name = nextElement.getName();
274 artifactNames.add(name);
278 return artifactNames;
280 catch(ZipException zipEx){
281 System.err.println("Error in zip file named : " + zipFileLocation);
282 zipEx.printStackTrace();
283 } catch (IOException e) {
284 System.err.println("Unhandled exception : ");
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 dirPath directory
311 public static synchronized File getLastModifiedFileNameFromDir(String dirPath){
312 File dir = new File(dirPath);
313 File[] files = dir.listFiles();
315 assertTrue("File not found under directory " + dirPath, false);
319 File lastModifiedFile = files[0];
320 for (int i = 1; i < files.length; i++) {
321 if(files[i].isDirectory()) {
324 if (lastModifiedFile.lastModified() < files[i].lastModified()) {
325 lastModifiedFile = files[i];
328 return lastModifiedFile;
331 public static void deleteDirectory(String directoryPath) {
332 File dir = new File(directoryPath);
334 FileUtils.cleanDirectory(dir);
335 } catch (IllegalArgumentException e) {
336 System.out.println("Failed to clean " + dir);
337 } catch (IOException e) {
338 System.out.println("Failed to clean " + dir);
342 public static void createDirectory(String directoryPath) {
343 File directory = new File(String.valueOf(directoryPath));
344 if (! directory.exists()){
351 * The method append data to existing file, if file not exists - create it
354 * @param leftSpaceCount
355 * @throws IOException
357 public static synchronized void writeToFile(File pathToFile, Object text, Integer leftSpaceCount) throws IOException{
359 BufferedWriter bw = null;
360 FileWriter fw = null;
361 if(!pathToFile.exists()){
362 createEmptyFile(pathToFile);
365 fw = new FileWriter(pathToFile, true);
366 bw = new BufferedWriter(fw);
367 StringBuilder sb = new StringBuilder();
368 if(leftSpaceCount > 0 ){
369 for(int i = 0; i < leftSpaceCount; i++){
373 bw.write(sb.toString() + text);
377 } catch (Exception e) {
378 ComponentBaseTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
382 public static String getCreateDirByName(String dirName) {
383 File dir = new File(dirName);
388 return dir.getPath();
391 public static boolean isFileDownloaded(String downloadPath, String fileName) {
392 boolean flag = false;
393 File dir = new File(downloadPath);
394 File[] dir_contents = dir.listFiles();
395 for (int i = 0; i < dir_contents.length; i++) {
396 if (dir_contents[i].getName().equals(fileName))
402 public static String getMD5OfFile(File file) throws IOException {
403 String content = FileUtils.readFileToString(file);
404 String md5 = GeneralUtility.calculateMD5Base64EncodedByString(content);
408 public static File createEmptyFile(String fileToCreate) {
409 File file= new File(fileToCreate);
414 file.createNewFile();
415 ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
416 } catch (IOException e) {
417 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
423 public static File createEmptyFile(File fileToCreate) {
425 if(fileToCreate.exists()){
426 deleteFile(fileToCreate);
428 fileToCreate.createNewFile();
429 ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
430 } catch (IOException e) {
431 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
437 public static void deleteFile(File file){
442 ComponentBaseTest.getExtendTest().log(Status.INFO, "File " + file.getName() + "has been deleted");
444 ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to delete file " + file.getName());
454 * get file list from directory by extension array
456 * @param okFileExtensions
459 public static List<File> getHeatAndHeatEnvArtifactsFromZip(File directory, String[] okFileExtensions){
461 List<File> fileList = new ArrayList<>();
462 File[] files = directory.listFiles();
464 for (String extension : okFileExtensions){
465 for(File file : files){
466 if (file.getName().toLowerCase().endsWith(extension)){
474 public static String getKeyByValueFromPropertyFormatFile(String fullPath, String key) {
475 Properties prop = new Properties();
476 InputStream input = null;
479 input = new FileInputStream(fullPath);
481 value = (prop.getProperty(key));
483 } catch (IOException ex) {
484 ex.printStackTrace();
489 } catch (IOException e) {
495 return value.replaceAll("\"","");
498 public static void overWriteExistindDir(String outputCsar) throws IOException {
499 String basePath = getBasePath();
500 String csarDir = FileHandling.getCreateDirByName("target/outputCsar");
501 FileUtils.cleanDirectory(new File(csarDir));