diff options
author | Bjørn Christian Seime <bjorn.christian@seime.no> | 2021-02-01 23:51:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-01 23:51:00 +0100 |
commit | 42794e42e8ce223705e0a8e0e3e9293d65adaf6c (patch) | |
tree | a337188686fe568fd0dc04db5234c7ffa2ccbb63 /metrics-proxy/src/test/java | |
parent | 7f04da806204b7727f377b4099bd10571c61e5da (diff) |
Revert "Remove org.json usage [run-systemtest]"
Diffstat (limited to 'metrics-proxy/src/test/java')
6 files changed, 75 insertions, 86 deletions
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandlerTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandlerTest.java index cf1eac3c691..d7576718e8a 100644 --- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandlerTest.java +++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandlerTest.java @@ -8,11 +8,13 @@ import ai.vespa.metricsproxy.metric.model.json.GenericApplicationModel; import ai.vespa.metricsproxy.metric.model.json.GenericJsonModel; import ai.vespa.metricsproxy.metric.model.json.GenericMetrics; import ai.vespa.metricsproxy.metric.model.json.GenericService; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; import com.github.tomakehurst.wiremock.junit.WireMockRule; import com.yahoo.container.jdisc.RequestHandlerTestDriver; +import java.util.regex.Pattern; + +import org.json.JSONArray; +import org.json.JSONObject; import org.junit.Before; import org.junit.Ignore; import org.junit.Rule; @@ -22,7 +24,6 @@ import java.io.IOException; import java.util.Arrays; import java.util.Map; import java.util.concurrent.Executors; -import java.util.regex.Pattern; import static ai.vespa.metricsproxy.TestUtil.getFileContents; import static ai.vespa.metricsproxy.http.ValuesFetcher.defaultMetricsConsumerId; @@ -48,8 +49,6 @@ import static org.junit.Assert.fail; @SuppressWarnings("UnstableApiUsage") public class ApplicationMetricsHandlerTest { - private static final ObjectMapper jsonMapper = new ObjectMapper(); - private static final String HOST = "localhost"; private static final String URI_BASE = "http://" + HOST; private static final String METRICS_V1_URI = URI_BASE + METRICS_V1_PATH; @@ -103,16 +102,16 @@ public class ApplicationMetricsHandlerTest { @Test public void v1_response_contains_values_uri() throws Exception { String response = testDriver.sendRequest(METRICS_V1_URI).readAll(); - JsonNode root = jsonMapper.readTree(response); + JSONObject root = new JSONObject(response); assertTrue(root.has("resources")); - ArrayNode resources = (ArrayNode) root.get("resources"); - assertEquals(2, resources.size()); + JSONArray resources = root.getJSONArray("resources"); + assertEquals(2, resources.length()); - JsonNode valuesUrl = resources.get(0); - assertEquals(METRICS_VALUES_URI, valuesUrl.get("url").textValue()); - JsonNode prometheusUrl = resources.get(1); - assertEquals(PROMETHEUS_VALUES_URI, prometheusUrl.get("url").textValue()); + JSONObject valuesUrl = resources.getJSONObject(0); + assertEquals(METRICS_VALUES_URI, valuesUrl.getString("url")); + JSONObject prometheusUrl = resources.getJSONObject(1); + assertEquals(PROMETHEUS_VALUES_URI, prometheusUrl.getString("url")); } @Ignore @@ -200,7 +199,7 @@ public class ApplicationMetricsHandlerTest { @Test public void invalid_path_yields_error_response() throws Exception { String response = testDriver.sendRequest(METRICS_V1_URI + "/invalid").readAll(); - JsonNode root = jsonMapper.readTree(response); + JSONObject root = new JSONObject(response); assertTrue(root.has("error")); } diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/metrics/MetricsHandlerTestBase.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/metrics/MetricsHandlerTestBase.java index 379ef04d38d..1c5ce695155 100644 --- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/metrics/MetricsHandlerTestBase.java +++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/metrics/MetricsHandlerTestBase.java @@ -6,9 +6,9 @@ import ai.vespa.metricsproxy.metric.model.json.GenericJsonModel; import ai.vespa.metricsproxy.metric.model.json.GenericMetrics; import ai.vespa.metricsproxy.metric.model.json.GenericService; import ai.vespa.metricsproxy.service.DownService; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; +import org.json.JSONArray; +import org.json.JSONObject; import org.junit.Ignore; import org.junit.Test; @@ -32,8 +32,6 @@ import static org.junit.Assert.fail; */ public abstract class MetricsHandlerTestBase<MODEL> extends HttpHandlerTestBase { - private static final ObjectMapper jsonMapper = new ObjectMapper(); - static String rootUri; static String valuesUri; @@ -58,21 +56,21 @@ public abstract class MetricsHandlerTestBase<MODEL> extends HttpHandlerTestBase @Test public void invalid_path_yields_error_response() throws Exception { String response = testDriver.sendRequest(rootUri + "/invalid").readAll(); - JsonNode root = jsonMapper.readTree(response); + JSONObject root = new JSONObject(response); assertTrue(root.has("error")); } @Test public void root_response_contains_values_uri() throws Exception { String response = testDriver.sendRequest(rootUri).readAll(); - JsonNode root = jsonMapper.readTree(response); + JSONObject root = new JSONObject(response); assertTrue(root.has("resources")); - ArrayNode resources = (ArrayNode) root.get("resources"); - assertEquals(1, resources.size()); + JSONArray resources = root.getJSONArray("resources"); + assertEquals(1, resources.length()); - JsonNode valuesUrl = resources.get(0); - assertEquals(valuesUri, valuesUrl.get("url").textValue()); + JSONObject valuesUrl = resources.getJSONObject(0); + assertEquals(valuesUri, valuesUrl.getString("url")); } @Ignore diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandlerTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandlerTest.java index 89186e63b93..a224c4090b3 100644 --- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandlerTest.java +++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandlerTest.java @@ -3,10 +3,9 @@ package ai.vespa.metricsproxy.http.prometheus; import ai.vespa.metricsproxy.http.HttpHandlerTestBase; import ai.vespa.metricsproxy.service.DummyService; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; import com.yahoo.container.jdisc.RequestHandlerTestDriver; +import org.json.JSONArray; +import org.json.JSONObject; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; @@ -23,8 +22,6 @@ import static org.junit.Assert.assertTrue; @SuppressWarnings("UnstableApiUsage") public class PrometheusHandlerTest extends HttpHandlerTestBase { - private static final ObjectMapper jsonMapper = new ObjectMapper(); - private static final String V1_URI = URI_BASE + PrometheusHandler.V1_PATH; private static final String VALUES_URI = URI_BASE + PrometheusHandler.VALUES_PATH; @@ -43,14 +40,14 @@ public class PrometheusHandlerTest extends HttpHandlerTestBase { @Test public void v1_response_contains_values_uri() throws Exception { String response = testDriver.sendRequest(V1_URI).readAll(); - JsonNode root = jsonMapper.readTree(response); + JSONObject root = new JSONObject(response); assertTrue(root.has("resources")); - ArrayNode resources = (ArrayNode) root.get("resources"); - assertEquals(1, resources.size()); + JSONArray resources = root.getJSONArray("resources"); + assertEquals(1, resources.length()); - JsonNode valuesUrl = resources.get(0); - assertEquals(VALUES_URI, valuesUrl.get("url").textValue()); + JSONObject valuesUrl = resources.getJSONObject(0); + assertEquals(VALUES_URI, valuesUrl.getString("url")); } @Ignore diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/node/NodeMetricGathererTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/node/NodeMetricGathererTest.java index c2fc23a878d..e2ad0ccd504 100644 --- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/node/NodeMetricGathererTest.java +++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/node/NodeMetricGathererTest.java @@ -3,9 +3,8 @@ package ai.vespa.metricsproxy.node; import ai.vespa.metricsproxy.metric.model.MetricId; import ai.vespa.metricsproxy.metric.model.MetricsPacket; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; +import org.json.JSONException; +import org.json.JSONObject; import org.junit.Test; import java.util.ArrayList; @@ -18,12 +17,10 @@ import static org.junit.Assert.assertEquals; */ public class NodeMetricGathererTest { - private static final ObjectMapper jsonMapper = new ObjectMapper(); - @Test - public void testJSONObjectIsCorrectlyConvertedToMetricsPacket() { + public void testJSONObjectIsCorrectlyConvertedToMetricsPacket() throws JSONException { List<MetricsPacket.Builder> builders = new ArrayList<>(); - JsonNode hostLifePacket = generateHostLifePacket(); + JSONObject hostLifePacket = generateHostLifePacket(); NodeMetricGatherer.addObjectToBuilders(builders, hostLifePacket); MetricsPacket packet = builders.remove(0).build(); @@ -35,17 +32,17 @@ public class NodeMetricGathererTest { assertEquals(1l, packet.metrics().get(MetricId.toMetricId("alive"))); } - private JsonNode generateHostLifePacket() { + private JSONObject generateHostLifePacket() throws JSONException { - ObjectNode jsonObject = jsonMapper.createObjectNode(); + JSONObject jsonObject = new JSONObject(); jsonObject.put("status_code", 0); jsonObject.put("status_msg", "OK"); jsonObject.put("timestamp", 123); jsonObject.put("application", "host_life"); - ObjectNode metrics = jsonMapper.createObjectNode(); + JSONObject metrics = new JSONObject(); metrics.put("uptime", 12); metrics.put("alive", 1); - jsonObject.set("metrics", metrics); + jsonObject.put("metrics", metrics); return jsonObject; } } 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 70970bfe8da..8d5bba77844 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 @@ -5,18 +5,17 @@ import ai.vespa.metricsproxy.metric.Metric; import ai.vespa.metricsproxy.metric.Metrics; import ai.vespa.metricsproxy.metric.model.ConsumerId; import ai.vespa.metricsproxy.service.VespaService; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; import com.yahoo.jrt.Request; import com.yahoo.jrt.Spec; import com.yahoo.jrt.StringValue; import com.yahoo.jrt.Supervisor; import com.yahoo.jrt.Target; import com.yahoo.jrt.Transport; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import org.junit.Test; -import java.io.IOException; import java.util.List; import static ai.vespa.metricsproxy.TestUtil.getFileContents; @@ -41,8 +40,6 @@ import static org.junit.Assert.assertTrue; */ public class RpcMetricsTest { - private static final ObjectMapper jsonMapper = new ObjectMapper(); - private static final String METRICS_RESPONSE = getFileContents("metrics-storage-simple.json").trim(); private static final String EXTRA_APP = "extra"; @@ -70,9 +67,9 @@ public class RpcMetricsTest { String allServicesResponse = getMetricsForYamas(ALL_SERVICES, rpcClient).trim(); // Verify that application is used as serviceId, and that metric exists. - JsonNode extraMetrics = findExtraMetricsObject(allServicesResponse); - assertThat(extraMetrics.get("metrics").get("foo.count").intValue(), is(3)); - assertThat(extraMetrics.get("dimensions").get("role").textValue(), is("extra-role")); + JSONObject extraMetrics = findExtraMetricsObject(allServicesResponse); + assertThat(extraMetrics.getJSONObject("metrics").getInt("foo.count"), is(3)); + assertThat(extraMetrics.getJSONObject("dimensions").getString("role"), is("extra-role")); } } } @@ -88,7 +85,7 @@ public class RpcMetricsTest { // Verify that no extra metrics exists String allServicesResponse = getMetricsForYamas(ALL_SERVICES, rpcClient).trim(); - JsonNode extraMetrics = findExtraMetricsObject(allServicesResponse); + JSONObject extraMetrics = findExtraMetricsObject(allServicesResponse); assertEquals(extraMetrics.toString(), "{}"); } } @@ -133,28 +130,28 @@ public class RpcMetricsTest { } } - private static void verifyMetricsFromRpcRequest(VespaService service, RpcClient client) throws IOException { + private static void verifyMetricsFromRpcRequest(VespaService service, RpcClient client) throws JSONException { String jsonResponse = getMetricsForYamas(service.getMonitoringName(), client).trim(); - ArrayNode metrics = (ArrayNode) jsonMapper.readTree(jsonResponse).get("metrics"); - assertThat("Expected 3 metric messages", metrics.size(), is(3)); - for (int i = 0; i < metrics.size() - 1; i++) { // The last "metric message" contains only status code/message - JsonNode jsonObject = metrics.get(i); + JSONArray metrics = new JSONObject(jsonResponse).getJSONArray("metrics"); + assertThat("Expected 3 metric messages", metrics.length(), is(3)); + for (int i = 0; i < metrics.length() - 1; i++) { // The last "metric message" contains only status code/message + JSONObject jsonObject = metrics.getJSONObject(i); assertFalse(jsonObject.has("status_code")); assertFalse(jsonObject.has("status_msg")); - assertThat(jsonObject.get("dimensions").get("foo").textValue(), is("bar")); - assertThat(jsonObject.get("dimensions").get("bar").textValue(), is("foo")); - assertThat(jsonObject.get("dimensions").get("serviceDim").textValue(), is("serviceDimValue")); - assertThat(jsonObject.get("routing").get("yamas").get("namespaces").size(), is(1)); - if (jsonObject.get("metrics").has("foo_count")) { - assertThat(jsonObject.get("metrics").get("foo_count").intValue(), is(1)); - assertThat(jsonObject.get("routing").get("yamas").get("namespaces").get(0).textValue(), is(vespaMetricsConsumerId.id)); + assertThat(jsonObject.getJSONObject("dimensions").getString("foo"), is("bar")); + assertThat(jsonObject.getJSONObject("dimensions").getString("bar"), is("foo")); + assertThat(jsonObject.getJSONObject("dimensions").getString("serviceDim"), is("serviceDimValue")); + assertThat(jsonObject.getJSONObject("routing").getJSONObject("yamas").getJSONArray("namespaces").length(), is(1)); + if (jsonObject.getJSONObject("metrics").has("foo_count")) { + assertThat(jsonObject.getJSONObject("metrics").getInt("foo_count"), is(1)); + assertThat(jsonObject.getJSONObject("routing").getJSONObject("yamas").getJSONArray("namespaces").get(0), is(vespaMetricsConsumerId.id)); } else { - assertThat(jsonObject.get("metrics").get("foo.count").intValue(), is(1)); - assertThat(jsonObject.get("routing").get("yamas").get("namespaces").get(0).textValue(), is(CUSTOM_CONSUMER_ID.id)); + assertThat(jsonObject.getJSONObject("metrics").getInt("foo.count"), is(1)); + assertThat(jsonObject.getJSONObject("routing").getJSONObject("yamas").getJSONArray("namespaces").get(0), is(CUSTOM_CONSUMER_ID.id)); } } - verifyStatusMessage(metrics.get(metrics.size() - 1)); + verifyStatusMessage(metrics.getJSONObject(metrics.length() - 1)); } private void verfiyMetricsFromServiceObject(VespaService service) { @@ -169,15 +166,15 @@ public class RpcMetricsTest { assertThat("Metric foo did not contain correct dimension for key = bar", foo.getDimensions().get(toDimensionId("bar")), is("foo")); } - private void verifyMetricsFromRpcRequestForAllServices(RpcClient client) throws IOException { + private void verifyMetricsFromRpcRequestForAllServices(RpcClient client) throws JSONException { // Verify that metrics for all services can be retrieved in one request. String allServicesResponse = getMetricsForYamas(ALL_SERVICES, client).trim(); - ArrayNode allServicesMetrics = (ArrayNode) jsonMapper.readTree(allServicesResponse).get("metrics"); - assertThat(allServicesMetrics.size(), is(5)); + JSONArray allServicesMetrics = new JSONObject(allServicesResponse).getJSONArray("metrics"); + assertThat(allServicesMetrics.length(), is(5)); } @Test - public void testGetAllMetricNames() { + public void testGetAllMetricNames() throws Exception { try (IntegrationTester tester = new IntegrationTester()) { tester.httpServer().setResponse(METRICS_RESPONSE); @@ -208,14 +205,14 @@ public class RpcMetricsTest { invoke(req, rpcClient, false); } - private JsonNode findExtraMetricsObject(String jsonResponse) throws IOException { - ArrayNode metrics = (ArrayNode) jsonMapper.readTree(jsonResponse).get("metrics"); - for (int i = 0; i < metrics.size(); i++) { - JsonNode jsonObject = metrics.get(i); + private JSONObject findExtraMetricsObject(String jsonResponse) throws JSONException { + JSONArray metrics = new JSONObject(jsonResponse).getJSONArray("metrics"); + for (int i = 0; i < metrics.length(); i++) { + JSONObject jsonObject = metrics.getJSONObject(i); assertTrue(jsonObject.has("application")); - if (jsonObject.get("application").textValue().equals(EXTRA_APP)) return jsonObject; + if (jsonObject.getString("application").equals(EXTRA_APP)) return jsonObject; } - return jsonMapper.createObjectNode(); + return new JSONObject(); } private static String getMetricsForYamas(String service, RpcClient client) { @@ -253,12 +250,12 @@ public class RpcMetricsTest { return returnValue; } - private static void verifyStatusMessage(JsonNode jsonObject) { - assertThat(jsonObject.get("status_code").intValue(), is(0)); - assertThat(jsonObject.get("status_msg").textValue(), notNullValue()); - assertThat(jsonObject.get("application").textValue(), notNullValue()); - assertThat(jsonObject.get("routing"), notNullValue()); - assertThat(jsonObject.size(), is(4)); + private static void verifyStatusMessage(JSONObject jsonObject) throws JSONException { + assertThat(jsonObject.getInt("status_code"), is(0)); + assertThat(jsonObject.getString("status_msg"), notNullValue()); + assertThat(jsonObject.getString("application"), notNullValue()); + assertThat(jsonObject.getString("routing"), notNullValue()); + assertThat(jsonObject.length(), is(4)); } } 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 7ff179e5528..0d53f988ac7 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 @@ -2,6 +2,7 @@ package ai.vespa.metricsproxy.service; import ai.vespa.metricsproxy.metric.Metric; +import org.json.JSONException; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; @@ -36,7 +37,7 @@ public class ContainerServiceTest { } @Test - public void testMultipleQueryDimensions() { + public void testMultipleQueryDimensions() throws JSONException { int count = 0; VespaService service = VespaService.create("service1", "id", httpServer.port()); for (Metric m : service.getMetrics().getMetrics()) { |