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 fj.data.Either;
24 import org.openecomp.sdc.ci.tests.utils.Decoder;
25 import org.openecomp.sdc.ci.tests.utils.Utils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
38 import static org.testng.AssertJUnit.assertTrue;
40 public class FileUtils {
41 static Logger logger = LoggerFactory.getLogger(Utils.class.getName());
43 public static void writeToFile(String filePath, String content) {
45 Files.write(Paths.get(filePath), content.getBytes());
46 } catch (IOException e) {
51 public static String getFileName(String fullyQualified) {
52 String fileName = fullyQualified;
54 int i = fullyQualified.lastIndexOf('.');
56 fileName = fullyQualified.substring(i + 1);
62 public static Either<String, Exception> getFileContentUTF8(String filePath) {
63 Either<String, Exception> eitherResult;
65 String content = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
66 eitherResult = Either.left(content);
67 } catch (Exception e) {
68 eitherResult = Either.right(e);
73 public static List<String> getFileListFromBaseDirectoryByTestName(String testResourcesPath) {
75 File file = new File(testResourcesPath);
76 File[] listFiles = file.listFiles();
77 if (listFiles != null) {
78 List<String> listFileName = new ArrayList<String>();
79 for (File newFile : listFiles) {
80 if (newFile.isFile()) {
81 listFileName.add(newFile.getPath());
86 assertTrue("directory " + testResourcesPath + " is empty", false);
90 public static String getFilePathFromListByPattern(List<String> fileList, String pattern) {
92 for (String filePath : fileList) {
93 if (filePath.contains(pattern)) {
100 public static String loadPayloadFileFromListUsingPosition(List<String> listFileName, String pattern,
101 Boolean isBase64, int positionInList) throws IOException {
102 List<String> newList = new ArrayList<String>(Arrays.asList(listFileName.get(positionInList)));
103 return loadPayloadFile(newList, pattern, isBase64);
106 public static String loadPayloadFile(List<String> listFileName, String pattern, Boolean isBase64) throws IOException {
108 String payload = null;
109 fileName = FileUtils.getFilePathFromListByPattern(listFileName, pattern);
110 logger.debug("fileName: {}", fileName);
112 if (fileName != null) {
113 payload = Decoder.readFileToString(fileName);
115 payload = Decoder.encode(payload.getBytes());
118 assertTrue("file to upload not found", false);
123 public static String getFileNameFromPath(String testResourcesPath) {
125 File file = new File(testResourcesPath);
126 String fileName = null;
128 return file.getName();
130 assertTrue("file to upload not found", false);