package cz.senslog.connector.fetch.gfs;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Objects;
import java.util.Properties;

public final class TestResourceUtils {

    public static Path getResourcePath(String filePath) throws URISyntaxException {
        URL url = TestResourceUtils.class.getClassLoader().getResource(filePath);
        return Paths.get(Objects.requireNonNull(url).toURI());
    }

    public static byte[] loadResourceFile(String filePath) {
        try {
            return Files.readAllBytes(getResourcePath(filePath));
        } catch (URISyntaxException | IOException e) {
            System.err.println(Arrays.toString(e.getStackTrace()));
            return new byte[0];
        }
    }

    public static Properties loadProperties(String filename) {
        Properties props = new Properties();
        try {
            props.load(Files.newInputStream(getResourcePath((filename))));
        } catch (URISyntaxException | IOException e) {
            System.err.println(e.getMessage());
        }
        return props;
    }
}
