diff options
author | Ofir Sonsino <os0695@intl.att.com> | 2018-08-12 12:20:55 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-08-12 12:20:55 +0000 |
commit | 91b057331b92ecf7f3c2d495681b05295dc8346c (patch) | |
tree | 0ce3f821ada8ad518784f7f9869db4b537ba3237 /vid-automation/src/main/java/vid/automation/test/utils/ReadFile.java | |
parent | 9152b2ae03c4751b492a219993d85d522c58b383 (diff) | |
parent | 4a4dcc5185f8ba5a28c7f9fef509f32c0c2389e6 (diff) |
Merge "vid-automation selenium tests"
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.java | 59 |
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 7652e09b..655cc20a 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); + } + } } |