aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/vid/automation/test/utils/ReadFile.java
diff options
context:
space:
mode:
Diffstat (limited to 'vid-automation/src/main/java/vid/automation/test/utils/ReadFile.java')
-rw-r--r--vid-automation/src/main/java/vid/automation/test/utils/ReadFile.java59
1 files changed, 48 insertions, 11 deletions
diff --git a/vid-automation/src/main/java/vid/automation/test/utils/ReadFile.java b/vid-automation/src/main/java/vid/automation/test/utils/ReadFile.java
index 7652e09b7..655cc20a6 100644
--- a/vid-automation/src/main/java/vid/automation/test/utils/ReadFile.java
+++ b/vid-automation/src/main/java/vid/automation/test/utils/ReadFile.java
@@ -1,33 +1,70 @@
package vid.automation.test.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOUtils;
import org.openecomp.sdc.ci.tests.utilities.FileHandling;
-import vid.automation.test.model.User;
-import vid.automation.test.model.UsersObject;
+import vid.automation.test.infra.Input;
-import java.io.File;
import java.io.IOException;
-import java.util.HashMap;
+import java.io.InputStream;
+import java.net.URL;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+
+import static java.nio.file.Files.copy;
+import static java.nio.file.Files.createTempDirectory;
public class ReadFile {
- public static <T> T getJsonFile(String fileName, Class<T> clazz) throws IOException {
+
+ private static Path tempDirectory;
+
+ public static <T> T getJsonFile(String fileName, Class<T> clazz) {
ObjectMapper mapper = new ObjectMapper();
T list;
try {
- File testCaseFile = FileHandling.getConfigFile(fileName);
+ java.io.File testCaseFile = FileHandling.getConfigFile(fileName);
if(!testCaseFile.exists()) {
String basePath = System.getProperty("BASE_PATH");
- testCaseFile = new File( basePath + File.separator + "conf" + File.separator + fileName);
+ testCaseFile = new java.io.File( basePath + java.io.File.separator + "conf" + java.io.File.separator + fileName);
}
list = (T) mapper.readValue(testCaseFile, clazz);
return list;
- } catch (IOException e) {
- e.printStackTrace();
- return null;
} catch (Exception e) {
e.printStackTrace();
- return null;
+ throw new RuntimeException("cannot read file '" + fileName + "' file as '" + clazz.getCanonicalName() + "'", e);
}
}
+ public static String copyOfFileFromResources(String pathInResources) {
+
+ // takes a file from resources, copies it to a temp folder, then
+ // returns the newly created path file
+
+ URL resource = Input.class.getClassLoader().getResource(pathInResources);
+ if (resource == null) {
+ throw new RuntimeException("file not found in resources: " + pathInResources);
+ }
+
+ Path target;
+ try {
+ tempDirectory = (tempDirectory != null) ? tempDirectory : createTempDirectory("resources-");
+ target = tempDirectory.resolve(FilenameUtils.getName(resource.getPath()));
+ copy(resource.openStream(), target, StandardCopyOption.REPLACE_EXISTING);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+ return target.toString();
+ }
+
+ public static String loadResourceAsString(String relativePath){
+ final InputStream resource = ReadFile.class.getClassLoader().getResourceAsStream(relativePath);
+ if (resource == null) throw new RuntimeException("template file not found: " + relativePath);
+ try {
+ return IOUtils.toString(resource, "UTF-8");
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
}