1 package vid.automation.test.utils;
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import org.apache.commons.io.FilenameUtils;
5 import org.apache.commons.io.IOUtils;
6 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
7 import vid.automation.test.infra.Input;
9 import java.io.IOException;
10 import java.io.InputStream;
12 import java.nio.file.Path;
13 import java.nio.file.StandardCopyOption;
15 import static java.nio.file.Files.copy;
16 import static java.nio.file.Files.createTempDirectory;
18 public class ReadFile {
20 private static Path tempDirectory;
22 public static <T> T getJsonFile(String fileName, Class<T> clazz) {
23 ObjectMapper mapper = new ObjectMapper();
26 java.io.File testCaseFile = FileHandling.getConfigFile(fileName);
27 if(!testCaseFile.exists()) {
28 String basePath = System.getProperty("BASE_PATH");
29 testCaseFile = new java.io.File( basePath + java.io.File.separator + "conf" + java.io.File.separator + fileName);
31 list = (T) mapper.readValue(testCaseFile, clazz);
33 } catch (Exception e) {
35 throw new RuntimeException("cannot read file '" + fileName + "' file as '" + clazz.getCanonicalName() + "'", e);
39 public static String copyOfFileFromResources(String pathInResources) {
41 // takes a file from resources, copies it to a temp folder, then
42 // returns the newly created path file
44 URL resource = Input.class.getClassLoader().getResource(pathInResources);
45 if (resource == null) {
46 throw new RuntimeException("file not found in resources: " + pathInResources);
51 tempDirectory = (tempDirectory != null) ? tempDirectory : createTempDirectory("resources-");
52 target = tempDirectory.resolve(FilenameUtils.getName(resource.getPath()));
53 copy(resource.openStream(), target, StandardCopyOption.REPLACE_EXISTING);
54 } catch (IOException e) {
55 throw new RuntimeException(e);
58 return target.toString();
61 public static String loadResourceAsString(String relativePath){
62 final InputStream resource = ReadFile.class.getClassLoader().getResourceAsStream(relativePath);
63 if (resource == null) throw new RuntimeException("template file not found: " + relativePath);
65 return IOUtils.toString(resource, "UTF-8");
66 } catch (IOException e) {
67 throw new RuntimeException(e);