re base code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / general / FileUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.utils.general;
22
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;
28
29 import java.io.File;
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;
37
38 import static org.testng.AssertJUnit.assertTrue;
39
40 public class FileUtils {
41         static Logger logger = LoggerFactory.getLogger(Utils.class.getName());
42
43         public static void writeToFile(String filePath, String content) {
44                 try {
45                         Files.write(Paths.get(filePath), content.getBytes());
46                 } catch (IOException e) {
47                         e.printStackTrace();
48                 }
49         }
50
51         public static String getFileName(String fullyQualified) {
52                 String fileName = fullyQualified;
53
54                 int i = fullyQualified.lastIndexOf('.');
55                 if (i > 0) {
56                         fileName = fullyQualified.substring(i + 1);
57                 }
58                 return fileName;
59
60         }
61
62         public static Either<String, Exception> getFileContentUTF8(String filePath) {
63                 Either<String, Exception> eitherResult;
64                 try {
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);
69                 }
70                 return eitherResult;
71         }
72
73         public static List<String> getFileListFromBaseDirectoryByTestName(String testResourcesPath) {
74
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());
82                                 }
83                         }
84                         return listFileName;
85                 }
86                 assertTrue("directory " + testResourcesPath + " is empty", false);
87                 return null;
88         }
89
90         public static String getFilePathFromListByPattern(List<String> fileList, String pattern) {
91
92                 for (String filePath : fileList) {
93                         if (filePath.contains(pattern)) {
94                                 return filePath;
95                         }
96                 }
97                 return null;
98         }
99
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);
104         }
105
106         public static String loadPayloadFile(List<String> listFileName, String pattern, Boolean isBase64) throws IOException {
107                 String fileName;
108                 String payload = null;
109                 fileName = FileUtils.getFilePathFromListByPattern(listFileName, pattern);
110                 logger.debug("fileName: {}", fileName);
111
112                 if (fileName != null) {
113                         payload = Decoder.readFileToString(fileName);
114                         if (isBase64) {
115                                 payload = Decoder.encode(payload.getBytes());
116                         }
117                 } else {
118                         assertTrue("file to upload not found", false);
119                 }
120                 return payload;
121         }
122
123         public static String getFileNameFromPath(String testResourcesPath) {
124
125                 File file = new File(testResourcesPath);
126                 String fileName = null;
127                 if (file.exists()) {
128                         return file.getName();
129                 } else {
130                         assertTrue("file to upload not found", false);
131                 }
132                 return fileName;
133
134         }
135 }