aboutsummaryrefslogtreecommitdiffstats
path: root/metrics-proxy
diff options
context:
space:
mode:
authorgjoranv <gv@verizonmedia.com>2019-05-03 17:17:39 +0200
committergjoranv <gv@verizonmedia.com>2019-05-03 17:17:39 +0200
commitc5246ed7f8a1f5aff399f494d09c26a21f0765f4 (patch)
tree265cead035d8a1fa23256986eebc3a547f210929 /metrics-proxy
parentb0fa6235ad92648590d641b090df143f1c049cfc (diff)
Resolve test files from class loader.
Diffstat (limited to 'metrics-proxy')
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/TestUtil.java27
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/IntegrationTester.java2
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcHealthMetricsTest.java8
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java6
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ContainerServiceTest.java9
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/MetricsFetcherTest.java2
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/VespaServiceTest.java2
7 files changed, 20 insertions, 36 deletions
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/TestUtil.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/TestUtil.java
index c5104d65b26..5997f85a46f 100644
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/TestUtil.java
+++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/TestUtil.java
@@ -8,29 +8,20 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.stream.Collectors;
/**
- * Utilities for tests in this module.
- *
- * @author hmusum
+ * @author gjoranv
*/
public class TestUtil {
- public static String getContents(File aFile) {
- //...checks on aFile are elided
- StringBuilder contents = new StringBuilder();
-
- try (BufferedReader input = new BufferedReader(new FileReader(aFile))) {
- String line;
- while ((line = input.readLine()) != null) {
- contents.append(line);
- contents.append(System.getProperty("line.separator"));
- }
- } catch (IOException ex) {
- ex.printStackTrace();
+ public static String getContents(String filename) {
+ InputStream in = TestUtil.class.getClassLoader().getResourceAsStream(filename);
+ if (in == null) {
+ throw new RuntimeException("File not found: " + filename);
}
-
- return contents.toString();
+ return new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));
}
-
}
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/IntegrationTester.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/IntegrationTester.java
index 04e44924de5..6bbf4ae1ef5 100644
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/IntegrationTester.java
+++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/IntegrationTester.java
@@ -39,8 +39,6 @@ import static ai.vespa.metricsproxy.service.HttpMetricFetcher.STATE_PATH;
*/
public class IntegrationTester implements AutoCloseable {
- static final String root = "src/test/resources/";
-
static final String MONITORING_SYSTEM = "test-system";
static final ConsumerId CUSTOM_CONSUMER_ID = toConsumerId("custom-consumer");
static final String SERVICE_1_CONFIG_ID = "container/qrserver.0";
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcHealthMetricsTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcHealthMetricsTest.java
index d41aaf984f5..20fb69e410e 100644
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcHealthMetricsTest.java
+++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcHealthMetricsTest.java
@@ -16,10 +16,8 @@ import com.yahoo.jrt.Target;
import com.yahoo.jrt.Transport;
import org.junit.Test;
-import java.io.File;
import java.util.List;
-import static ai.vespa.metricsproxy.rpc.IntegrationTester.root;
import static ai.vespa.metricsproxy.rpc.IntegrationTester.SERVICE_1_CONFIG_ID;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
@@ -32,11 +30,11 @@ import static org.junit.Assert.assertThat;
public class RpcHealthMetricsTest {
private static final String HEALTH_OK_RESPONSE =
- TestUtil.getContents(new File(root + "health-check.response.json"));
+ TestUtil.getContents("health-check.response.json");
private static final String HEALTH_FAILED_RESPONSE =
- TestUtil.getContents(new File(root + "health-check-failed.response.json"));
+ TestUtil.getContents("health-check-failed.response.json");
private static final String WANTED_RPC_RESPONSE =
- TestUtil.getContents(new File(root + "rpc-json-output-check.json")).trim();
+ TestUtil.getContents("rpc-json-output-check.json").trim();
// see factory/doc/port-ranges.txt
private static final int httpPort = 18635;
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java
index 8c6a448d3a2..f264fd13ddc 100644
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java
+++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java
@@ -20,16 +20,14 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
-import java.io.File;
import java.util.List;
import static ai.vespa.metricsproxy.core.VespaMetrics.VESPA_CONSUMER_ID;
import static ai.vespa.metricsproxy.metric.model.DimensionId.toDimensionId;
import static ai.vespa.metricsproxy.rpc.IntegrationTester.CUSTOM_CONSUMER_ID;
import static ai.vespa.metricsproxy.rpc.IntegrationTester.MONITORING_SYSTEM;
-import static ai.vespa.metricsproxy.rpc.IntegrationTester.root;
-import static ai.vespa.metricsproxy.rpc.IntegrationTester.SERVICE_2_CONFIG_ID;
import static ai.vespa.metricsproxy.rpc.IntegrationTester.SERVICE_1_CONFIG_ID;
+import static ai.vespa.metricsproxy.rpc.IntegrationTester.SERVICE_2_CONFIG_ID;
import static ai.vespa.metricsproxy.service.VespaServices.ALL_SERVICES;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -44,7 +42,7 @@ import static org.junit.Assert.assertThat;
public class RpcMetricsTest {
private static final String METRICS_RESPONSE_CCL =
- TestUtil.getContents(new File(root + "metrics-storage-simple.json")).trim();
+ TestUtil.getContents("metrics-storage-simple.json").trim();
// see factory/doc/port-ranges.txt
private static final int httpPort = 18633;
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ContainerServiceTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ContainerServiceTest.java
index 126aedb6a7f..4174b18f3a7 100644
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ContainerServiceTest.java
+++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ContainerServiceTest.java
@@ -9,10 +9,9 @@ import ai.vespa.metricsproxy.metric.Metric;
import org.json.JSONException;
import org.junit.After;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
-import java.io.File;
-
import static ai.vespa.metricsproxy.metric.model.DimensionId.toDimensionId;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -24,10 +23,9 @@ public class ContainerServiceTest {
private MockHttpServer service;
private int csPort;
- private static final String response;
- static {
- response = TestUtil.getContents(new File("src/test/resources/metrics-container-state-multi-chain.json"));
+ @BeforeClass
+ public static void init() {
HttpMetricFetcher.CONNECTION_TIMEOUT = 60000; // 60 secs in unit tests
}
@@ -35,6 +33,7 @@ public class ContainerServiceTest {
public void setupHTTPServer() {
csPort = 18637; // see factory/doc/port-ranges.txt
try {
+ String response = TestUtil.getContents("metrics-container-state-multi-chain.json");
service = new MockHttpServer(csPort, response, HttpMetricFetcher.METRICS_PATH);
} catch (Exception e) {
e.printStackTrace();
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/MetricsFetcherTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/MetricsFetcherTest.java
index f5ffd715c96..27e1bb97943 100644
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/MetricsFetcherTest.java
+++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/MetricsFetcherTest.java
@@ -21,7 +21,7 @@ public class MetricsFetcherTest {
@Test
public void testStateFormatMetricsParse() {
- String jsonData = TestUtil.getContents(new File("src/test/resources/metrics-state.json"));
+ String jsonData = TestUtil.getContents("metrics-state.json");
RemoteMetricsFetcher fetcher = new RemoteMetricsFetcher(new DummyService(0, "dummy/id/0"), port);
Metrics metrics = fetcher.createMetrics(jsonData, 0);
assertThat("Wrong number of metrics", metrics.size(), is(10));
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/VespaServiceTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/VespaServiceTest.java
index 8644f6bb28f..13be98db23a 100644
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/VespaServiceTest.java
+++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/VespaServiceTest.java
@@ -24,7 +24,7 @@ public class VespaServiceTest {
private static final String response;
static {
- response = TestUtil.getContents(new File("src/test/resources/metrics-state.json"));
+ response = TestUtil.getContents("metrics-state.json");
HttpMetricFetcher.CONNECTION_TIMEOUT = 60000; // 60 secs in unit tests
}