aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/vid/automation/test/utils/ReadFile.java
blob: 1f6cd032b8b31292779962d2168b114e45866bf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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.onap.sdc.ci.tests.utilities.FileHandling;
import vid.automation.test.infra.Input;

import java.io.IOException;
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 {

    private static Path tempDirectory;

    public static <T> T getJsonFile(String fileName, Class<T> clazz) {
        ObjectMapper mapper = new ObjectMapper();
        T list;
        try {
            java.io.File testCaseFile = FileHandling.getConfigFile(fileName);
            if(!testCaseFile.exists()) {
                String basePath = System.getProperty("BASE_PATH");
                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 (Exception e) {
            e.printStackTrace();
            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);
        }
    }
}