[SDC-29] rebase continue work to align source
[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 static org.testng.AssertJUnit.assertTrue;
24
25 import java.io.File;
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;
33
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;
38
39 import fj.data.Either;
40
41 public class FileUtils {
42         static Logger logger = LoggerFactory.getLogger(Utils.class.getName());
43
44         public static void writeToFile(String filePath, String content) {
45                 try {
46                         Files.write(Paths.get(filePath), content.getBytes());
47                 } catch (IOException e) {
48                         e.printStackTrace();
49                 }
50         }
51
52         public static String getFileName(String fullyQualified) {
53                 String fileName = fullyQualified;
54
55                 int i = fullyQualified.lastIndexOf('.');
56                 if (i > 0) {
57                         fileName = fullyQualified.substring(i + 1);
58                 }
59                 return fileName;
60
61         }
62
63         public static Either<String, Exception> getFileContentUTF8(String filePath) {
64                 Either<String, Exception> eitherResult;
65                 try {
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);
70                 }
71                 return eitherResult;
72         }
73
74         public static List<String> getFileListFromBaseDirectoryByTestName(String testResourcesPath) {
75
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());
83                                 }
84                         }
85                         return listFileName;
86                 }
87                 assertTrue("directory " + testResourcesPath + " is empty", false);
88                 return null;
89         }
90
91         public static String getFilePathFromListByPattern(List<String> fileList, String pattern) {
92
93                 for (String filePath : fileList) {
94                         if (filePath.contains(pattern)) {
95                                 return filePath;
96                         }
97                 }
98                 return null;
99         }
100
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);
105         }
106
107         public static String loadPayloadFile(List<String> listFileName, String pattern, Boolean isBase64)
108                         throws IOException {
109                 String fileName;
110                 String payload = null;
111                 fileName = FileUtils.getFilePathFromListByPattern(listFileName, pattern);
112                 logger.debug("fileName: {}", fileName);
113
114                 if (fileName != null) {
115                         payload = Decoder.readFileToString(fileName);
116                         if (isBase64) {
117                                 payload = Decoder.encode(payload.getBytes());
118                         }
119                 } else {
120                         assertTrue("file to upload not found", false);
121                 }
122                 return payload;
123         }
124
125         public static String getFileNameFromPath(String testResourcesPath) {
126
127                 File file = new File(testResourcesPath);
128                 String fileName = null;
129                 if (file.exists()) {
130                         return file.getName();
131                 } else {
132                         assertTrue("file to upload not found", false);
133                 }
134                 return fileName;
135
136         }
137 }