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;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
34 import org.openecomp.sdc.ci.tests.utils.Decoder;
35 import org.openecomp.sdc.ci.tests.utils.Utils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import fj.data.Either;
41 public class FileUtils {
42 static Logger logger = LoggerFactory.getLogger(Utils.class.getName());
44 public static void writeToFile(String filePath, String content) {
46 Files.write(Paths.get(filePath), content.getBytes());
47 } catch (IOException e) {
52 public static String getFileName(String fullyQualified) {
53 String fileName = fullyQualified;
55 int i = fullyQualified.lastIndexOf('.');
57 fileName = fullyQualified.substring(i + 1);
63 public static Either<String, Exception> getFileContentUTF8(String filePath) {
64 Either<String, Exception> eitherResult;
66 String content = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
67 eitherResult = Either.left(content);
68 } catch (Exception e) {
69 eitherResult = Either.right(e);
74 public static List<String> getFileListFromBaseDirectoryByTestName(String testResourcesPath) {
76 File file = new File(testResourcesPath);
77 File[] listFiles = file.listFiles();
78 if (listFiles != null) {
79 List<String> listFileName = new ArrayList<String>();
80 for (File newFile : listFiles) {
81 if (newFile.isFile()) {
82 listFileName.add(newFile.getPath());
87 assertTrue("directory " + testResourcesPath + " is empty", false);
91 public static String getFilePathFromListByPattern(List<String> fileList, String pattern) {
93 for (String filePath : fileList) {
94 if (filePath.contains(pattern)) {
101 public static String loadPayloadFileFromListUsingPosition(List<String> listFileName, String pattern,
102 Boolean isBase64, int positionInList) throws IOException {
103 List<String> newList = new ArrayList<String>(Arrays.asList(listFileName.get(positionInList)));
104 return loadPayloadFile(newList, pattern, isBase64);
107 public static String loadPayloadFile(List<String> listFileName, String pattern, Boolean isBase64) throws IOException {
109 String payload = null;
110 fileName = FileUtils.getFilePathFromListByPattern(listFileName, pattern);
111 logger.debug("fileName: {}", fileName);
113 if (fileName != null) {
114 payload = Decoder.readFileToString(fileName);
116 payload = Decoder.encode(payload.getBytes());
119 assertTrue("file to upload not found", false);
124 public static String getFileNameFromPath(String testResourcesPath) {
126 File file = new File(testResourcesPath);
127 String fileName = null;
129 return file.getName();
131 assertTrue("file to upload not found", false);