summaryrefslogtreecommitdiffstats
path: root/sparkybe-onap-service/src/test/java/org/onap/aai/sparky/util/TestResourceLoader.java
blob: 7b1b198c6faf955fb3f1ec4342d62662fa620bdf (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
package org.onap.aai.sparky.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

public class TestResourceLoader {

	public static String getTestResourceDataJson(String resourcePath)
			throws JsonParseException, JsonMappingException, IOException {

		// will look for resource using "src/test/resources" as the base folder
		URL url = TestResourceLoader.class.getResource(resourcePath);
		File file = new File(url.getFile());

		byte[] payload = Files.readAllBytes(file.toPath());

		if (payload.length == 0) {
			throw new FileNotFoundException("Could not load '" + resourcePath + "' test data");
		}

		return new String(payload);

	}

}