aboutsummaryrefslogtreecommitdiffstats
path: root/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java')
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java117
1 files changed, 98 insertions, 19 deletions
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java
index 68c9dc8a..8a2574e1 100644
--- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java
@@ -3,7 +3,8 @@
* ONAP
* ================================================================================
* Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2020 Nordix Foundation.
+ * Modifications Copyright (C) 2020, 2023-2024 Nordix Foundation.
+ * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,6 +32,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import com.google.gson.Gson;
+import io.prometheus.client.servlet.jakarta.exporter.MetricsServlet;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
@@ -38,6 +40,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
+import java.util.Objects;
import java.util.UUID;
import org.apache.commons.io.IOUtils;
import org.junit.AfterClass;
@@ -56,10 +59,13 @@ import org.slf4j.LoggerFactory;
* HttpServletServer JUNIT tests.
*/
public class HttpServerTest {
+ private static final String JVM_MEMORY_BYTES_USED = "jvm_memory_bytes_used";
+ private static final String METRICS_URI = "/metrics";
+ private static final String PROMETHEUS = "prometheus";
private static final String LOCALHOST = "localhost";
private static final String JSON_MEDIA = "application/json";
private static final String YAML_MEDIA = YamlMessageBodyHandler.APPLICATION_YAML;
- private static final String SWAGGER_JSON = "/swagger.json";
+ private static final String SWAGGER_JSON = "/openapi.json";
private static final String JUNIT_ECHO_HELLO = "/junit/echo/hello";
private static final String JUNIT_ECHO_FULL_REQUEST = "/junit/echo/full/request";
private static final String SOME_TEXT = "some text";
@@ -68,7 +74,7 @@ public class HttpServerTest {
/**
* Logger.
*/
- private static Logger logger = LoggerFactory.getLogger(HttpServerTest.class);
+ private static final Logger logger = LoggerFactory.getLogger(HttpServerTest.class);
private static final String LOCALHOST_PREFIX = "http://localhost:";
@@ -211,6 +217,79 @@ public class HttpServerTest {
assertEquals(reqText, response);
}
+ /**
+ * This test checks a server from a plain java servlet (note it uses prometheus as the sample server).
+ */
+ @Test
+ public void testStdServletServer() throws Exception {
+ logger.info("-- testStdServletServer() --");
+
+ HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
+ .build(PROMETHEUS, LOCALHOST, port, "/", false, true);
+
+ server.addStdServletClass("/prom-generic-servlet/metrics", MetricsServlet.class.getName());
+ server.waitedStart(5000);
+
+ assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
+ assertTrue(server.isPrometheus());
+
+ String response = http(portUrl + "/prom-generic-servlet/metrics");
+ assertThat(response).contains(JVM_MEMORY_BYTES_USED);
+ }
+
+ /**
+ * This test explicitly creates a prometheus server.
+ */
+ @Test
+ public void testExplicitPrometheusServer() throws Exception {
+ logger.info("-- testPrometheusServer() --");
+
+ HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
+ .build(PROMETHEUS, LOCALHOST, port, "/", false, true);
+ server.setPrometheus(METRICS_URI);
+ server.waitedStart(5000);
+
+ assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
+ assertTrue(server.isPrometheus());
+
+ String response = http(portUrl + METRICS_URI);
+ assertThat(response).contains(JVM_MEMORY_BYTES_USED);
+ }
+
+ /**
+ * This test is an all-in-one for a single server: prometheus, jax-rs, servlet, swagger, and filters.
+ */
+ @Test
+ public void testPrometheusJaxRsFilterSwaggerServer() throws Exception {
+ logger.info("-- testPrometheusServer() --");
+
+ HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
+ .build(PROMETHEUS, LOCALHOST, port, "/", true, true);
+
+ server.addServletClass("/*", RestEchoService.class.getName());
+ server.addFilterClass("/*", TestFilter.class.getName());
+ server.setPrometheus(METRICS_URI);
+
+ server.waitedStart(5000);
+
+ assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
+ assertTrue(server.isPrometheus());
+
+ String response = http(portUrl + METRICS_URI);
+ assertThat(response).contains(JVM_MEMORY_BYTES_USED);
+
+ RestEchoReqResp request = new RestEchoReqResp();
+ request.setRequestId(100);
+ request.setText(SOME_TEXT);
+ String reqText = gson.toJson(request);
+
+ response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
+ assertEquals(reqText, response);
+
+ response = http(portUrl + SWAGGER_JSON);
+ assertThat(response).contains("openapi");
+ }
+
@Test
public void testJacksonClassServer() throws Exception {
logger.info("-- testJacksonClassServer() --");
@@ -304,6 +383,7 @@ public class HttpServerTest {
server.addFilterClass("/*", TestFilter.class.getName());
// ensure we can serialize the server
+ new GsonTestUtils().compareGson(server, HttpServerTest.class);
assertThatCode(() -> new GsonTestUtils().compareGson(server, HttpServerTest.class)).doesNotThrowAnyException();
}
@@ -318,7 +398,6 @@ public class HttpServerTest {
server.waitedStart(5000);
assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
- assertFalse(HttpServletServerFactoryInstance.getServerFactory().get(port).isAaf());
String response = http(portUrl + JUNIT_ECHO_HELLO);
assertEquals(HELLO, response);
@@ -331,11 +410,6 @@ public class HttpServerTest {
assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
assertEquals(1, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
- System.setProperty("cadi_longitude", "0.0");
- System.setProperty("cadi_latitude", "0.0");
- server.setAafAuthentication("/*");
- assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAaf());
-
HttpServletServerFactoryInstance.getServerFactory().destroy(port);
assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
}
@@ -345,7 +419,7 @@ public class HttpServerTest {
logger.info("-- testMultipleServers() --");
HttpServletServer server1 = HttpServletServerFactoryInstance.getServerFactory()
- .build("echo-1", false, LOCALHOST, port, "/", true, true);
+ .build("echo-1", false, LOCALHOST, port, false, "/", true, true);
server1.addServletPackage("/*", this.getClass().getPackage().getName());
server1.waitedStart(5000);
@@ -445,13 +519,14 @@ public class HttpServerTest {
logger.info("-- testSingleStaticResourceServer() --");
HttpServletServer staticServer = HttpServletServerFactoryInstance.getServerFactory()
- .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, "/", true);
+ .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, false, "/", true);
Throwable thrown = catchThrowable(() -> staticServer.addServletResource("/*", null));
assertThat(thrown).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("No resourceBase provided");
staticServer.addServletResource(null,
- HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm());
+ Objects.requireNonNull(HttpServerTest.class.getClassLoader().getResource("webapps/root"))
+ .toExternalForm());
thrown = catchThrowable(() -> staticServer.addServletClass("/*", RestEchoService.class.getName()));
assertThat(thrown).isInstanceOf(UnsupportedOperationException.class)
@@ -482,11 +557,13 @@ public class HttpServerTest {
logger.info("-- testMultiStaticResourceServer() --");
HttpServletServer staticResourceServer = HttpServletServerFactoryInstance.getServerFactory()
- .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, "/", true);
+ .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, false, "/", true);
staticResourceServer.addServletResource("/root/*",
- HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm());
+ Objects.requireNonNull(HttpServerTest.class.getClassLoader().getResource("webapps/root"))
+ .toExternalForm());
staticResourceServer.addServletResource("/alt-root/*",
- HttpServerTest.class.getClassLoader().getResource("webapps/alt-root").toExternalForm());
+ Objects.requireNonNull(HttpServerTest.class.getClassLoader().getResource("webapps/alt-root"))
+ .toExternalForm());
staticResourceServer.waitedStart(5000);
assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
@@ -507,9 +584,10 @@ public class HttpServerTest {
logger.info("-- testMultiTypesServer() --");
HttpServletServer staticResourceServer = HttpServletServerFactoryInstance.getServerFactory()
- .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, "/", true);
+ .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, false, "/", true);
staticResourceServer.addServletResource("/root/*",
- HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm());
+ Objects.requireNonNull(HttpServerTest.class.getClassLoader().getResource("webapps/root"))
+ .toExternalForm());
staticResourceServer.waitedStart(5000);
int port2 = port + 1;
@@ -518,7 +596,8 @@ public class HttpServerTest {
jerseyServer.addServletPackage("/api/*", this.getClass().getPackage().getName());
Throwable thrown = catchThrowable(() -> jerseyServer.addServletResource("/root/*",
- HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm()));
+ Objects.requireNonNull(HttpServerTest.class.getClassLoader().getResource("webapps/root"))
+ .toExternalForm()));
assertThat(thrown).isInstanceOf(UnsupportedOperationException.class)
.hasMessageContaining("is not supported on this type of jetty server");
@@ -554,7 +633,7 @@ public class HttpServerTest {
}
/**
- * Performs an http request.
+ * Performs a http request.
*
* @throws MalformedURLException make sure URL is good
* @throws IOException thrown is IO exception occurs