diff options
Diffstat (limited to 'policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server')
18 files changed, 521 insertions, 474 deletions
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java index 2aaf1367..11067974 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java @@ -2,8 +2,9 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. + * Modifications Copyright 2023-2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,15 +24,23 @@ package org.onap.policy.common.endpoints.http.server.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import io.prometheus.client.servlet.jakarta.exporter.MetricsServlet; +import jakarta.ws.rs.client.Entity; +import jakarta.ws.rs.client.InvocationCallback; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Properties; -import javax.ws.rs.client.Entity; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; +import java.util.TreeMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import lombok.Getter; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -56,8 +65,6 @@ public class HttpClientTest { private static final String FALSE_STRING = "false"; private static final String ALPHA123 = "alpha123"; private static final String PUT_HELLO = "PUT:hello:{myParameter=myValue}"; - private static final String DOT_GSON = "." + "GSON"; - private static final String DOT_JACKSON = "." + "JACKSON"; private static final String DOT_PDP = "." + "PDP"; private static final String DOT_PAP = "." + "PAP"; @@ -114,13 +121,11 @@ public class HttpClientTest { /* echo server - https + basic auth */ final HttpServletServer echoServerAuth = HttpServletServerFactoryInstance.getServerFactory() - .build("echo", true, LOCALHOST, 6667, "/", false, true); + .build("echo", true, LOCALHOST, 6667, false, "/", false, true); echoServerAuth.setBasicAuthentication("x", "y", null); echoServerAuth.addServletPackage("/*", HttpClientTest.class.getPackage().getName()); echoServerAuth.addFilterClass("/*", TestFilter.class.getName()); echoServerAuth.addFilterClass("/*", TestAuthorizationFilter.class.getName()); - echoServerAuth.addFilterClass("/*", TestAafAuthFilter.class.getName()); - echoServerAuth.addFilterClass("/*", TestAafGranularAuthFilter.class.getName()); echoServerAuth.waitedStart(5000); if (!NetworkUtil.isTcpPortOpen(LOCALHOST, echoServerAuth.getPort(), 5, 10000L)) { @@ -136,7 +141,6 @@ public class HttpClientTest { HttpClientFactoryInstance.getClientFactory().destroy(); MyGsonProvider.resetSome(); - MyJacksonProvider.resetSome(); } /** @@ -194,6 +198,28 @@ public class HttpClientTest { } @Test + public void testHttpGetNoAuthClientAsync() throws Exception { + final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, + 6666); + MyCallback callback = new MyCallback(); + final Response response = client.get(callback, HELLO, new TreeMap<>()).get(); + + verifyCallback("testHttpGetNoAuthClientAsync", callback, response); + + final String body = HttpClient.getBody(response, String.class); + + assertEquals(200, response.getStatus()); + assertEquals(HELLO, body); + } + + private void verifyCallback(String testName, MyCallback callback, final Response response) + throws InterruptedException { + assertTrue(testName, callback.await()); + assertNull(testName, callback.getThrowable()); + assertSame(testName, response, callback.getResponse()); + } + + @Test public void testHttpPutNoAuthClient() throws Exception { final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666); @@ -206,6 +232,22 @@ public class HttpClientTest { } @Test + public void testHttpPutNoAuthClientAsync() throws Exception { + final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666); + + Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON); + MyCallback callback = new MyCallback(); + final Response response = client.put(callback, HELLO, entity, Collections.emptyMap()).get(); + + verifyCallback("testHttpPutNoAuthClientAsync", callback, response); + + final String body = HttpClient.getBody(response, String.class); + + assertEquals(200, response.getStatus()); + assertEquals(PUT_HELLO, body); + } + + @Test public void testHttpPostNoAuthClient() throws Exception { final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666); @@ -219,6 +261,23 @@ public class HttpClientTest { } @Test + public void testHttpPostNoAuthClientAsync() throws Exception { + final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, + 6666); + + Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON); + MyCallback callback = new MyCallback(); + final Response response = client.post(callback, HELLO, entity, Collections.emptyMap()).get(); + + verifyCallback("testHttpPostNoAuthClientAsync", callback, response); + + final String body = HttpClient.getBody(response, String.class); + + assertEquals(200, response.getStatus()); + assertEquals("POST:hello:{myParameter=myValue}", body); + } + + @Test public void testHttpDeletetNoAuthClient() throws Exception { final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666); @@ -231,10 +290,34 @@ public class HttpClientTest { } @Test - public void testHttpGetAuthClient() throws Exception { + public void testHttpDeletetNoAuthClientAsync() throws Exception { + final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, + 6666); + + MyCallback callback = new MyCallback(); + final Response response = client.delete(callback, HELLO, Collections.emptyMap()).get(); + + verifyCallback("testHttpDeletetNoAuthClientAsync", callback, response); + + final String body = HttpClient.getBody(response, String.class); + + assertEquals(200, response.getStatus()); + assertEquals("DELETE:hello", body); + } + + /** + * Perform one asynchronous test with auth client; don't need to test every method. + * @throws Exception if an error occurs + */ + @Test + public void testHttpAsyncAuthClient() throws Exception { final HttpClient client = getAuthHttpClient(); - final Response response = client.get(HELLO); + MyCallback callback = new MyCallback(); + final Response response = client.get(callback, HELLO, null).get(); + + verifyCallback("testHttpAsyncAuthClient", callback, response); + final String body = HttpClient.getBody(response, String.class); assertEquals(200, response.getStatus()); @@ -242,24 +325,19 @@ public class HttpClientTest { } @Test - public void testHttpPutAuthClient() throws Exception { + public void testHttpGetAuthClient() throws Exception { final HttpClient client = getAuthHttpClient(); - Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON); - final Response response = client.put(HELLO, entity, Collections.emptyMap()); + final Response response = client.get(HELLO); final String body = HttpClient.getBody(response, String.class); assertEquals(200, response.getStatus()); - assertEquals(PUT_HELLO, body); + assertEquals(HELLO, body); } @Test - public void testHttpPutAuthClient_JacksonProvider() throws Exception { - final HttpClient client = HttpClientFactoryInstance.getClientFactory() - .build(BusTopicParams.builder().clientName(TEST_HTTP_AUTH_CLIENT).useHttps(true) - .allowSelfSignedCerts(true).hostname(LOCALHOST).port(6667).basePath(JUNIT_ECHO) - .userName("x").password("y").managed(true) - .serializationProvider(MyJacksonProvider.class.getName()).build()); + public void testHttpPutAuthClient() throws Exception { + final HttpClient client = getAuthHttpClient(); Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON); final Response response = client.put(HELLO, entity, Collections.emptyMap()); @@ -267,10 +345,6 @@ public class HttpClientTest { assertEquals(200, response.getStatus()); assertEquals(PUT_HELLO, body); - - assertTrue(MyJacksonProvider.hasWrittenSome()); - - assertFalse(MyGsonProvider.hasWrittenSome()); } @Test @@ -289,8 +363,6 @@ public class HttpClientTest { assertEquals(PUT_HELLO, body); assertTrue(MyGsonProvider.hasWrittenSome()); - - assertFalse(MyJacksonProvider.hasWrittenSome()); } @Test @@ -305,7 +377,12 @@ public class HttpClientTest { public void testHttpAuthClientProps() throws Exception { final Properties httpProperties = new Properties(); + /* PAP and PDP services */ + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, "PAP,PDP"); + + /* PAP server service configuration */ + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP @@ -322,8 +399,15 @@ public class HttpClientTest { PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP + PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX, TestFilter.class.getName()); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP + + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_CLASS_SUFFIX, MetricsServlet.class.getName()); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP + + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_URIPATH_SUFFIX, + "/pap/test/random/metrics"); + + /* PDP server service configuration */ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST); @@ -337,10 +421,19 @@ public class HttpClientTest { PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, RestMockHealthCheck.class.getName()); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP + + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP + + PolicyEndPointProperties.PROPERTY_HTTP_PROMETHEUS_SUFFIX, "true"); + + /* PDP and PAP client services */ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES, "PAP,PDP"); + + /* PAP client service configuration */ + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP @@ -356,13 +449,13 @@ public class HttpClientTest { httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); + /* PDP client service configuration */ + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7778"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP - + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, "pdp"); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpdp"); @@ -382,85 +475,45 @@ public class HttpClientTest { server.waitedStart(10000); } + Response response; final HttpClient clientPap = HttpClientFactoryInstance.getClientFactory().get("PAP"); - final Response response = clientPap.get(); + response = clientPap.get(); assertEquals(200, response.getStatus()); final HttpClient clientPdp = HttpClientFactoryInstance.getClientFactory().get("PDP"); - final Response response2 = clientPdp.get("test"); - assertEquals(500, response2.getStatus()); - assertFalse(MyJacksonProvider.hasWrittenSome()); - assertFalse(MyGsonProvider.hasWrittenSome()); - } - - @Test - public void testHttpAuthClientProps_MixedProviders() throws Exception { - final Properties httpProperties = new Properties(); + response = clientPdp.get("pdp/test"); + assertEquals(500, response.getStatus()); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES, "GSON,JACKSON"); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON - + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON - + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "6666"); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON - + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, JUNIT_ECHO); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON - + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON - + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); - httpProperties.setProperty( - PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON - + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, - MyGsonProvider.class.getName()); - - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON - + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON - + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "6666"); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON - + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, JUNIT_ECHO); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON - + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON - + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); - httpProperties.setProperty( - PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON - + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, - MyJacksonProvider.class.getName()); - - final List<HttpClient> clients = HttpClientFactoryInstance.getClientFactory().build(httpProperties); - assertEquals(2, clients.size()); - - Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON); + response = clientPdp.get("metrics"); + assertEquals(200, response.getStatus()); - // use gson client - MyGsonProvider.resetSome(); - MyJacksonProvider.resetSome(); - HttpClient client = HttpClientFactoryInstance.getClientFactory().get("GSON"); + response = clientPdp.get("openapi.json"); + assertEquals(200, response.getStatus()); - Response response = client.put(HELLO, entity, Collections.emptyMap()); - String body = HttpClient.getBody(response, String.class); + assertFalse(MyGsonProvider.hasWrittenSome()); + // try with empty path + response = clientPap.get(""); assertEquals(200, response.getStatus()); - assertEquals(PUT_HELLO, body); - assertTrue(MyGsonProvider.hasWrittenSome()); - assertFalse(MyJacksonProvider.hasWrittenSome()); - - // use jackson client - MyGsonProvider.resetSome(); - MyJacksonProvider.resetSome(); - client = HttpClientFactoryInstance.getClientFactory().get("JACKSON"); + response = clientPap.get("random/metrics"); + assertEquals(200, response.getStatus()); - response = client.put(HELLO, entity, Collections.emptyMap()); - body = HttpClient.getBody(response, String.class); + response = clientPap.get("metrics"); + assertEquals(404, response.getStatus()); + // try it asynchronously, too + MyCallback callback = new MyCallback(); + response = clientPap.get(callback, null).get(); + verifyCallback("testHttpAuthClientProps", callback, response); assertEquals(200, response.getStatus()); - assertEquals(PUT_HELLO, body); - assertTrue(MyJacksonProvider.hasWrittenSome()); - assertFalse(MyGsonProvider.hasWrittenSome()); + // try it asynchronously, with empty path + callback = new MyCallback(); + response = clientPap.get(callback, "", null).get(); + verifyCallback("testHttpAuthClientProps - empty path", callback, response); + assertEquals(200, response.getStatus()); } private HttpClient getAuthHttpClient() throws HttpClientConfigException { @@ -479,7 +532,7 @@ public class HttpClientTest { } - class MyEntity { + static class MyEntity { private String myParameter; @@ -497,4 +550,29 @@ public class HttpClientTest { } + static class MyCallback implements InvocationCallback<Response> { + @Getter + private Response response; + + @Getter + private Throwable throwable; + + private CountDownLatch latch = new CountDownLatch(1); + + @Override + public void completed(Response response) { + this.response = response; + latch.countDown(); + } + + @Override + public void failed(Throwable throwable) { + this.throwable = throwable; + latch.countDown(); + } + + public boolean await() throws InterruptedException { + return latch.await(5, TimeUnit.SECONDS); + } + } } 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 82faa052..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 @@ -2,7 +2,9 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * 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. @@ -20,18 +22,25 @@ package org.onap.policy.common.endpoints.http.server.test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.catchThrowable; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +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; 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; @@ -50,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"; @@ -62,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:"; @@ -85,7 +97,6 @@ public class HttpServerTest { HttpServletServerFactoryInstance.getServerFactory().destroy(); - MyJacksonProvider.resetSome(); MyGsonProvider.resetSome(); MyYamlProvider.resetSome(); } @@ -94,9 +105,14 @@ public class HttpServerTest { port += 10; } + /** + * To delete temporary properties cadi_longitude,and cadi_latitude. + */ @AfterClass public static void tearDownAfterClass() { HttpServletServerFactoryInstance.getServerFactory().destroy(); + System.clearProperty("cadi_longitude"); + System.clearProperty("cadi_latitude"); } @Test @@ -121,38 +137,6 @@ public class HttpServerTest { } @Test - public void testJacksonPackageServer() throws Exception { - logger.info("-- testJacksonPackageServer() --"); - - HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory() - .build("echo", LOCALHOST, port, "/", false, true); - - server.setSerializationProvider(MyJacksonProvider.class.getName()); - server.addServletPackage("/*", this.getClass().getPackage().getName()); - server.addFilterClass("/*", TestFilter.class.getName()); - server.waitedStart(5000); - - assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive()); - - RestEchoReqResp request = new RestEchoReqResp(); - request.setRequestId(100); - request.setText(SOME_TEXT); - String reqText = gson.toJson(request); - - String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText); - assertEquals(reqText, response); - - assertTrue(MyJacksonProvider.hasReadSome()); - assertTrue(MyJacksonProvider.hasWrittenSome()); - - assertFalse(MyGsonProvider.hasReadSome()); - assertFalse(MyGsonProvider.hasWrittenSome()); - - assertFalse(MyYamlProvider.hasReadSome()); - assertFalse(MyYamlProvider.hasWrittenSome()); - } - - @Test public void testGsonPackageServer() throws Exception { logger.info("-- testGsonPackageServer() --"); @@ -177,9 +161,6 @@ public class HttpServerTest { assertTrue(MyGsonProvider.hasReadSome()); assertTrue(MyGsonProvider.hasWrittenSome()); - assertFalse(MyJacksonProvider.hasReadSome()); - assertFalse(MyJacksonProvider.hasWrittenSome()); - assertFalse(MyYamlProvider.hasReadSome()); assertFalse(MyYamlProvider.hasWrittenSome()); } @@ -213,9 +194,6 @@ public class HttpServerTest { assertFalse(MyGsonProvider.hasReadSome()); assertFalse(MyGsonProvider.hasWrittenSome()); - - assertFalse(MyJacksonProvider.hasReadSome()); - assertFalse(MyJacksonProvider.hasWrittenSome()); } @Test @@ -239,13 +217,85 @@ 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() --"); HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory() .build("echo", LOCALHOST, port, "/", false, true); - server.setSerializationProvider(MyJacksonProvider.class.getName()); server.addServletClass("/*", RestEchoService.class.getName()); server.addFilterClass("/*", TestFilter.class.getName()); server.waitedStart(5000); @@ -260,9 +310,6 @@ public class HttpServerTest { String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText); assertEquals(reqText, response); - assertTrue(MyJacksonProvider.hasReadSome()); - assertTrue(MyJacksonProvider.hasWrittenSome()); - assertFalse(MyGsonProvider.hasReadSome()); assertFalse(MyGsonProvider.hasWrittenSome()); @@ -294,9 +341,6 @@ public class HttpServerTest { assertTrue(MyGsonProvider.hasReadSome()); assertTrue(MyGsonProvider.hasWrittenSome()); - assertFalse(MyJacksonProvider.hasReadSome()); - assertFalse(MyJacksonProvider.hasWrittenSome()); - assertFalse(MyYamlProvider.hasReadSome()); assertFalse(MyYamlProvider.hasWrittenSome()); } @@ -329,9 +373,6 @@ public class HttpServerTest { assertFalse(MyGsonProvider.hasReadSome()); assertFalse(MyGsonProvider.hasWrittenSome()); - - assertFalse(MyJacksonProvider.hasReadSome()); - assertFalse(MyJacksonProvider.hasWrittenSome()); } @Test @@ -343,6 +384,7 @@ public class HttpServerTest { // ensure we can serialize the server new GsonTestUtils().compareGson(server, HttpServerTest.class); + assertThatCode(() -> new GsonTestUtils().compareGson(server, HttpServerTest.class)).doesNotThrowAnyException(); } @Test @@ -356,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); @@ -369,9 +410,6 @@ public class HttpServerTest { assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive()); assertEquals(1, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); - server.setAafAuthentication("/*"); - assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAaf()); - HttpServletServerFactoryInstance.getServerFactory().destroy(port); assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); } @@ -381,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); @@ -396,13 +434,13 @@ public class HttpServerTest { assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port2).isAlive()); String response = http(portUrl + JUNIT_ECHO_HELLO); - assertTrue(HELLO.equals(response)); + assertEquals(HELLO, response); response = http(portUrl + SWAGGER_JSON); - assertTrue(response != null); + assertNotNull(response); response = http(LOCALHOST_PREFIX + port2 + JUNIT_ECHO_HELLO); - assertTrue(HELLO.equals(response)); + assertEquals(HELLO, response); assertThatThrownBy(() -> http(LOCALHOST_PREFIX + port2 + SWAGGER_JSON)).isInstanceOf(IOException.class); @@ -424,7 +462,7 @@ public class HttpServerTest { assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive()); String response = http(portUrl + JUNIT_ECHO_HELLO); - assertTrue(HELLO.equals(response)); + assertEquals(HELLO, response); response = http(portUrl + "/junit/endpoints/http/servers"); assertTrue(response.contains(randomName)); @@ -446,7 +484,7 @@ public class HttpServerTest { assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive()); String response = http(portUrl + JUNIT_ECHO_HELLO); - assertTrue(HELLO.equals(response)); + assertEquals(HELLO, response); HttpServletServerFactoryInstance.getServerFactory().destroy(); assertTrue(HttpServletServerFactoryInstance.getServerFactory().inventory().isEmpty()); @@ -467,7 +505,7 @@ public class HttpServerTest { assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive()); String response = http(portUrl + JUNIT_ECHO_HELLO); - assertTrue(HELLO.equals(response)); + assertEquals(HELLO, response); response = http(portUrl + "/junit/endpoints/http/servers"); assertTrue(response.contains(randomName)); @@ -476,6 +514,108 @@ public class HttpServerTest { assertTrue(HttpServletServerFactoryInstance.getServerFactory().inventory().isEmpty()); } + @Test + public void testSingleStaticResourceServer() throws Exception { + logger.info("-- testSingleStaticResourceServer() --"); + + HttpServletServer staticServer = HttpServletServerFactoryInstance.getServerFactory() + .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, + Objects.requireNonNull(HttpServerTest.class.getClassLoader().getResource("webapps/root")) + .toExternalForm()); + + thrown = catchThrowable(() -> staticServer.addServletClass("/*", RestEchoService.class.getName())); + assertThat(thrown).isInstanceOf(UnsupportedOperationException.class) + .hasMessageContaining("is not supported on this type of jetty server"); + + thrown = catchThrowable(() -> staticServer.addServletPackage("/api/*", this.getClass().getPackage().getName())); + assertThat(thrown).isInstanceOf(UnsupportedOperationException.class) + .hasMessageContaining("is not supported on this type of jetty server"); + + thrown = catchThrowable(() -> staticServer.setSerializationProvider(MyGsonProvider.class.getName())); + assertThat(thrown).isInstanceOf(UnsupportedOperationException.class) + .hasMessageContaining("is not supported on this type of jetty server"); + + staticServer.waitedStart(5000); + + assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive()); + assertEquals(1, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); + + String response = http(portUrl); + assertThat(response).contains("Test Jetty Static Resources Root"); + + HttpServletServerFactoryInstance.getServerFactory().destroy(port); + assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); + } + + @Test + public void testMultiStaticResourceServer() throws Exception { + logger.info("-- testMultiStaticResourceServer() --"); + + HttpServletServer staticResourceServer = HttpServletServerFactoryInstance.getServerFactory() + .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, false, "/", true); + staticResourceServer.addServletResource("/root/*", + Objects.requireNonNull(HttpServerTest.class.getClassLoader().getResource("webapps/root")) + .toExternalForm()); + staticResourceServer.addServletResource("/alt-root/*", + Objects.requireNonNull(HttpServerTest.class.getClassLoader().getResource("webapps/alt-root")) + .toExternalForm()); + staticResourceServer.waitedStart(5000); + + assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive()); + assertEquals(1, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); + + String response = http(portUrl + "/root/"); + assertThat(response).contains("Test Jetty Static Resources Root"); + + response = http(portUrl + "/alt-root/"); + assertThat(response).contains("Test Jetty Static Resources Alt-Root"); + + HttpServletServerFactoryInstance.getServerFactory().destroy(port); + assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); + } + + @Test + public void testMultiTypesServer() throws Exception { + logger.info("-- testMultiTypesServer() --"); + + HttpServletServer staticResourceServer = HttpServletServerFactoryInstance.getServerFactory() + .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, false, "/", true); + staticResourceServer.addServletResource("/root/*", + Objects.requireNonNull(HttpServerTest.class.getClassLoader().getResource("webapps/root")) + .toExternalForm()); + staticResourceServer.waitedStart(5000); + + int port2 = port + 1; + HttpServletServer jerseyServer = + HttpServletServerFactoryInstance.getServerFactory().build("echo", LOCALHOST, port2, "/", false, true); + jerseyServer.addServletPackage("/api/*", this.getClass().getPackage().getName()); + + Throwable thrown = catchThrowable(() -> jerseyServer.addServletResource("/root/*", + Objects.requireNonNull(HttpServerTest.class.getClassLoader().getResource("webapps/root")) + .toExternalForm())); + assertThat(thrown).isInstanceOf(UnsupportedOperationException.class) + .hasMessageContaining("is not supported on this type of jetty server"); + + jerseyServer.waitedStart(5000); + + assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive()); + assertEquals(2, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); + + String response = http(portUrl + "/root/"); + assertThat(response).contains("Test Jetty Static Resources Root"); + + response = http(LOCALHOST_PREFIX + port2 + "/api" + JUNIT_ECHO_HELLO); + assertEquals(HELLO, response); + + HttpServletServerFactoryInstance.getServerFactory().destroy(); + assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); + } + /** * performs an http request. * @@ -486,14 +626,14 @@ public class HttpServerTest { private String http(String urlString) throws IOException, InterruptedException { URL url = new URL(urlString); - if (!NetworkUtil.isTcpPortOpen(url.getHost(), url.getPort(), 25, 2)) { + if (!NetworkUtil.isTcpPortOpen(url.getHost(), url.getPort(), 25, 100)) { throw new IllegalStateException("port never opened: " + url); } return response(url.openConnection()); } /** - * Performs an http request. + * Performs a http request. * * @throws MalformedURLException make sure URL is good * @throws IOException thrown is IO exception occurs @@ -502,7 +642,7 @@ public class HttpServerTest { private String http(String urlString, String mediaType, String post) throws IOException, InterruptedException { URL url = new URL(urlString); - if (!NetworkUtil.isTcpPortOpen(url.getHost(), url.getPort(), 25, 2)) { + if (!NetworkUtil.isTcpPortOpen(url.getHost(), url.getPort(), 25, 100)) { throw new IllegalStateException("port never opened: " + url); } HttpURLConnection conn = (HttpURLConnection) url.openConnection(); @@ -510,7 +650,7 @@ public class HttpServerTest { conn.setDoOutput(true); conn.setRequestProperty("Content-Type", mediaType); conn.setRequestProperty("Accept", mediaType); - IOUtils.write(post, conn.getOutputStream()); + IOUtils.write(post, conn.getOutputStream(), StandardCharsets.UTF_8); return response(conn); } @@ -523,7 +663,7 @@ public class HttpServerTest { */ private String response(URLConnection conn) throws IOException { try (InputStream inpstr = conn.getInputStream()) { - return String.join("", IOUtils.readLines(inpstr)); + return String.join("", IOUtils.readLines(inpstr, StandardCharsets.UTF_8)); } } diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/JsonExceptionMapperTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/JsonExceptionMapperTest.java index 59ce0c1c..27e98670 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/JsonExceptionMapperTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/JsonExceptionMapperTest.java @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +24,7 @@ package org.onap.policy.common.endpoints.http.server.test; import static org.junit.Assert.assertEquals; import com.google.gson.JsonSyntaxException; -import javax.ws.rs.core.Response; +import jakarta.ws.rs.core.Response; import org.junit.Before; import org.junit.Test; import org.onap.policy.common.endpoints.http.server.JsonExceptionMapper; diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyGsonProvider.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyGsonProvider.java index 8343d02d..78c41cc2 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyGsonProvider.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyGsonProvider.java @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,13 +21,13 @@ package org.onap.policy.common.endpoints.http.server.test; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.MultivaluedMap; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; import lombok.AccessLevel; import lombok.Setter; import org.onap.policy.common.gson.GsonMessageBodyHandler; diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyJacksonProvider.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyJacksonProvider.java deleted file mode 100644 index 8c4eea5a..00000000 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyJacksonProvider.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.http.server.test; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.lang.annotation.Annotation; -import java.lang.reflect.Type; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; - -import lombok.AccessLevel; -import lombok.Setter; - -import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider; - -/** - * JacksonJsonProvider that tracks activities. - */ -public class MyJacksonProvider extends JacksonJsonProvider { - - @Setter(AccessLevel.PRIVATE) - private static boolean readSome = false; - - @Setter(AccessLevel.PRIVATE) - private static boolean wroteSome = false; - - /** - * Constructs the object and resets the variables to indicate that no activity has - * occurred yet. - */ - public MyJacksonProvider() { - super(); - } - - @Override - public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, - MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException { - - setReadSome(true); - return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream); - } - - @Override - public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, - MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { - - setWroteSome(true); - super.writeTo(object, type, genericType, annotations, mediaType, httpHeaders, entityStream); - } - - public static boolean hasReadSome() { - return readSome; - } - - public static boolean hasWrittenSome() { - return wroteSome; - } - - public static void resetSome() { - MyJacksonProvider.readSome = false; - MyJacksonProvider.wroteSome = false; - } - -} diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyYamlProvider.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyYamlProvider.java index 098ecb44..624d7686 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyYamlProvider.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyYamlProvider.java @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,13 +21,13 @@ package org.onap.policy.common.endpoints.http.server.test; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.MultivaluedMap; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; import lombok.AccessLevel; import lombok.Setter; import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler; diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoReqResp.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoReqResp.java index c211881f..2748fadd 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoReqResp.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoReqResp.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. 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. @@ -20,21 +20,20 @@ package org.onap.policy.common.endpoints.http.server.test; -import com.fasterxml.jackson.annotation.JsonProperty; import com.google.gson.annotations.SerializedName; +import lombok.ToString; import org.onap.policy.common.gson.annotation.GsonJsonProperty; /** * "ECHO" request and response supporting serialization and de-serialization via * both jackson and gson. */ +@ToString public class RestEchoReqResp { - @JsonProperty("reqId") @GsonJsonProperty("reqId") @SerializedName("reqId") private int requestId; - @JsonProperty("textValue") @GsonJsonProperty("textValue") @SerializedName("textValue") private String text; @@ -54,9 +53,4 @@ public class RestEchoReqResp { public void setText(String text) { this.text = text; } - - @Override - public String toString() { - return "RestEchoReqResp [requestId=" + requestId + ", text=" + text + "]"; - } } diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java index 373950b2..b4a652aa 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,29 +21,30 @@ package org.onap.policy.common.endpoints.http.server.test; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.Status; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import jakarta.ws.rs.DELETE; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.PUT; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.core.Response.Status; import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler; -@Api(value = "echo") @Path("/junit/echo") public class RestEchoService { @GET @Path("{word}") @Produces(MediaType.TEXT_PLAIN) - @ApiOperation(value = "echoes back whatever received") + @Operation(summary = "echoes back whatever received") public String echo(@PathParam("word") String word) { return word; } @@ -50,7 +52,7 @@ public class RestEchoService { @PUT @Path("{word}") @Produces(MediaType.TEXT_PLAIN) - @ApiOperation(value = "echoes back whatever received") + @Operation(summary = "echoes back whatever received") public String echoPut(@PathParam("word") String word, Object entity) { return "PUT:" + word + ":" + entity.toString(); } @@ -58,7 +60,8 @@ public class RestEchoService { @POST @Path("/full/request") @Produces({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML}) - @ApiOperation(value = "echoes back the request structure", response = RestEchoReqResp.class) + @Operation(summary = "echoes back the request structure") + @ApiResponse(content = {@Content(schema = @Schema(implementation = RestEchoReqResp.class))}) public Response echoFullyPost(RestEchoReqResp reqResp) { return Response.status(Status.OK).entity(reqResp).build(); } @@ -66,7 +69,7 @@ public class RestEchoService { @POST @Path("{word}") @Produces(MediaType.TEXT_PLAIN) - @ApiOperation(value = "echoes back whatever received") + @Operation(summary = "echoes back whatever received") public String echoPost(@PathParam("word") String word, Object entity) { return "POST:" + word + ":" + entity.toString(); } @@ -74,7 +77,7 @@ public class RestEchoService { @DELETE @Path("{word}") @Produces(MediaType.TEXT_PLAIN) - @ApiOperation(value = "echoes back whatever received") + @Operation(summary = "echoes back whatever received") public String echoDelete(@PathParam("word") String word) { return "DELETE:" + word; } diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEndpoints.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEndpoints.java index 6ada15c5..cbcf4c9a 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEndpoints.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEndpoints.java @@ -2,7 +2,8 @@ * ============LICENSE_START======================================================= * policy-endpoints * ================================================================================ - * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017, 2019-2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,13 +21,11 @@ package org.onap.policy.common.endpoints.http.server.test; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; import java.util.List; - -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; - import org.onap.policy.common.endpoints.http.server.HttpServletServer; import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestMockHealthCheck.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestMockHealthCheck.java index f0d68c0c..f476dbaf 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestMockHealthCheck.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestMockHealthCheck.java @@ -3,6 +3,7 @@ * policy-endpoints * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,12 +21,12 @@ package org.onap.policy.common.endpoints.http.server.test; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.Status; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.core.Response.Status; @Path("/") public class RestMockHealthCheck { @@ -40,7 +41,7 @@ public class RestMockHealthCheck { @GET @Path("pdp/test") @Produces(MediaType.APPLICATION_JSON) - public Response pdpHealthCheck() { + public Response pdpHealthCheck() { return Response.status(Status.INTERNAL_SERVER_ERROR).entity("At least some Dead").build(); } diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java index 16f17652..a789a66f 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java @@ -2,7 +2,9 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. + * Modifications Copyright (C) 2023-2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +33,16 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import io.prometheus.client.servlet.jakarta.exporter.MetricsServlet; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; import java.io.IOException; import java.io.PrintWriter; import java.net.HttpURLConnection; @@ -38,14 +50,8 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Base64; +import java.util.List; import java.util.Properties; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.Consumes; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import lombok.Getter; import org.apache.commons.io.IOUtils; import org.junit.AfterClass; @@ -60,15 +66,15 @@ import org.onap.policy.common.endpoints.http.server.RestServer; import org.onap.policy.common.endpoints.http.server.RestServer.Factory; import org.onap.policy.common.endpoints.http.server.YamlExceptionMapper; import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler; -import org.onap.policy.common.endpoints.http.server.aaf.AafAuthFilter; import org.onap.policy.common.endpoints.parameters.RestServerParameters; import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; import org.onap.policy.common.gson.GsonMessageBodyHandler; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.network.NetworkUtil; -import org.powermock.reflect.Whitebox; +import org.springframework.test.util.ReflectionTestUtils; public class RestServerTest { + private static final String METRICS_URI = "/metrics"; private static final String SERVER1 = "my-server-A"; private static final String SERVER2 = "my-server-B"; private static final String FACTORY_FIELD = "factory"; @@ -96,13 +102,13 @@ public class RestServerTest { */ @BeforeClass public static void setUpBeforeClass() throws Exception { - saveFactory = Whitebox.getInternalState(RestServer.class, FACTORY_FIELD); + saveFactory = (Factory) ReflectionTestUtils.getField(RestServer.class, FACTORY_FIELD); realPort = NetworkUtil.allocPort(); initRealParams(); - realRest = new RestServer(params, null, RealProvider.class) { + realRest = new RestServer(params, RealProvider.class) { @Override protected Properties getServerProperties(RestServerParameters restServerParameters, String names) { Properties props = super.getServerProperties(restServerParameters, names); @@ -124,7 +130,7 @@ public class RestServerTest { */ @AfterClass public static void tearDownAfterClass() { - Whitebox.setInternalState(RestServer.class, FACTORY_FIELD, saveFactory); + ReflectionTestUtils.setField(RestServer.class, FACTORY_FIELD, saveFactory); realRest.stop(); } @@ -147,12 +153,12 @@ public class RestServerTest { when(server1.getName()).thenReturn(SERVER1); when(server2.getName()).thenReturn(SERVER2); - Whitebox.setInternalState(RestServer.class, FACTORY_FIELD, factory); + ReflectionTestUtils.setField(RestServer.class, FACTORY_FIELD, factory); } @Test public void testRestServer() { - rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class); + rest = new RestServer(params, Filter2.class, Provider1.class, Provider2.class); rest.start(); verify(server1).start(); @@ -164,52 +170,26 @@ public class RestServerTest { } @Test - public void testRestServer_NoAaf() { - rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class); - verify(server1, never()).addFilterClass(any(), any()); - verify(server2, never()).addFilterClass(any(), any()); - } - - @Test - public void testRestServer_OnlyOneAaf() { - when(server2.isAaf()).thenReturn(true); - - rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class); - - verify(server1, never()).addFilterClass(any(), any()); - verify(server2).addFilterClass(null, Filter.class.getName()); - } - - @Test - public void testRestServer_BothAaf() { - when(server1.isAaf()).thenReturn(true); - when(server2.isAaf()).thenReturn(true); - - rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class); - - verify(server1).addFilterClass(null, Filter.class.getName()); - verify(server2).addFilterClass(null, Filter.class.getName()); - } + public void testRestServerListList() { + rest = new RestServer(params, List.of(Filter2.class), List.of(Provider1.class, Provider2.class)); - @Test - public void testRestServer_BothAaf_NoFilter() { - when(server1.isAaf()).thenReturn(true); - when(server2.isAaf()).thenReturn(true); - - rest = new RestServer(params, null, Provider1.class, Provider2.class); + rest.start(); + verify(server1).start(); + verify(server2).start(); - verify(server1, never()).addFilterClass(any(), any()); - verify(server2, never()).addFilterClass(any(), any()); + rest.stop(); + verify(server1).stop(); + verify(server2).stop(); } @Test public void testRestServer_MissingProviders() { - assertThatIllegalArgumentException().isThrownBy(() -> new RestServer(params, Filter.class)); + assertThatIllegalArgumentException().isThrownBy(() -> new RestServer(params, List.of(Filter2.class), null)); } @Test public void testGetServerProperties_testGetProviderNames() { - rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class); + rest = new RestServer(params, Provider1.class, Provider2.class); ArgumentCaptor<Properties> cap = ArgumentCaptor.forClass(Properties.class); verify(serverFactory).build(cap.capture()); @@ -227,10 +207,43 @@ public class RestServerTest { assertEquals(USER, props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX)); assertEquals(PASS, props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX)); assertEquals("true", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX)); - assertEquals("true", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX)); assertEquals(String.join(",", GsonMessageBodyHandler.class.getName(), YamlMessageBodyHandler.class.getName(), JsonExceptionMapper.class.getName(), YamlExceptionMapper.class.getName()), props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER)); + assertEquals("false", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PROMETHEUS_SUFFIX)); + } + + @Test + public void testExplicitPrometheusAddedToProperty() { + when(params.isPrometheus()).thenReturn(true); + rest = new RestServer(params, Filter2.class, Provider1.class, Provider2.class); + ArgumentCaptor<Properties> cap = ArgumentCaptor.forClass(Properties.class); + verify(serverFactory).build(cap.capture()); + + Properties props = cap.getValue(); + String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + PARAM_NAME; + + assertEquals("true", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PROMETHEUS_SUFFIX)); + assertThat(props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_URIPATH_SUFFIX)).isBlank(); + assertThat(props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_CLASS_SUFFIX)).isBlank(); + } + + @Test + public void testStandardServletAddedToProperty() { + when(params.getServletUriPath()).thenReturn("/metrics"); + when(params.getServletClass()).thenReturn(MetricsServlet.class.getName()); + rest = new RestServer(params, Filter2.class, Provider1.class, Provider2.class); + ArgumentCaptor<Properties> cap = ArgumentCaptor.forClass(Properties.class); + verify(serverFactory).build(cap.capture()); + + Properties props = cap.getValue(); + String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + PARAM_NAME; + + assertEquals("false", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PROMETHEUS_SUFFIX)); + assertEquals(METRICS_URI, + props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_URIPATH_SUFFIX)); + assertEquals(MetricsServlet.class.getName(), + props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_CLASS_SUFFIX)); } @Test @@ -285,7 +298,7 @@ public class RestServerTest { @Test public void testToString() { - rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class); + rest = new RestServer(params, Filter2.class, Provider1.class, Provider2.class); assertNotNull(rest.toString()); } @@ -316,15 +329,10 @@ public class RestServerTest { when(params.isHttps()).thenReturn(true); } - private static class Filter extends AafAuthFilter { + private static class Filter2 implements jakarta.servlet.Filter { @Override - protected String getPermissionType(HttpServletRequest request) { - return ""; - } - - @Override - protected String getPermissionInstance(HttpServletRequest request) { - return ""; + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { + // do nothing } } diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestAafAuthFilter.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestAafAuthFilter.java deleted file mode 100644 index 27d45c8d..00000000 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestAafAuthFilter.java +++ /dev/null @@ -1,47 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.http.server.test; - -import javax.servlet.http.HttpServletRequest; -import org.onap.policy.common.endpoints.http.server.aaf.AafAuthFilter; - -public class TestAafAuthFilter extends AafAuthFilter { - - @Override - protected String getRole(HttpServletRequest request) { - String expectedPerm = "test|test|" + request.getMethod().toLowerCase(); - if (!expectedPerm.equals(super.getRole(request))) { - throw new IllegalStateException("unexpected permission"); - } else { - return "user"; - } - } - - @Override - protected String getPermissionType(HttpServletRequest request) { - return "test"; - } - - @Override - protected String getPermissionInstance(HttpServletRequest request) { - return "test"; - } -}
\ No newline at end of file diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestAafGranularAuthFilter.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestAafGranularAuthFilter.java deleted file mode 100644 index 183e5ae7..00000000 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestAafGranularAuthFilter.java +++ /dev/null @@ -1,46 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.http.server.test; - -import javax.servlet.http.HttpServletRequest; -import org.onap.policy.common.endpoints.http.server.aaf.AafGranularAuthFilter; -import org.onap.policy.common.utils.network.NetworkUtil; - -public class TestAafGranularAuthFilter extends AafGranularAuthFilter { - - @Override - protected String getRole(HttpServletRequest request) { - String expectedPerm = this.getPermissionTypeRoot() - + request.getRequestURI().replace('/', '.') + "|" - + NetworkUtil.getHostname() + "|" - + request.getMethod().toLowerCase(); - if (!expectedPerm.equals(super.getRole(request))) { - throw new IllegalStateException("unexpected AAF granular permission"); - } else { - return "user"; - } - } - - @Override - public String getPermissionTypeRoot() { - return "org.onap.policy"; - } -}
\ No newline at end of file diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestAuthorizationFilter.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestAuthorizationFilter.java index f3cd7424..59ec6f13 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestAuthorizationFilter.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestAuthorizationFilter.java @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +21,7 @@ package org.onap.policy.common.endpoints.http.server.test; -import javax.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequest; import org.onap.policy.common.endpoints.http.server.AuthorizationFilter; public class TestAuthorizationFilter extends AuthorizationFilter { diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestFilter.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestFilter.java index 44b37656..155e12bb 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestFilter.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/TestFilter.java @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,12 +21,12 @@ package org.onap.policy.common.endpoints.http.server.test; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; import java.io.IOException; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; public class TestFilter implements Filter { diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlExceptionMapperTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlExceptionMapperTest.java index 96c23ed3..5317eadb 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlExceptionMapperTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlExceptionMapperTest.java @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +23,7 @@ package org.onap.policy.common.endpoints.http.server.test; import static org.junit.Assert.assertEquals; -import javax.ws.rs.core.Response; +import jakarta.ws.rs.core.Response; import org.junit.Before; import org.junit.Test; import org.onap.policy.common.endpoints.http.server.YamlExceptionMapper; diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlJacksonHandlerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlJacksonHandlerTest.java index 7ca131ec..b2b81c26 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlJacksonHandlerTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlJacksonHandlerTest.java @@ -2,7 +2,8 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,12 +26,14 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import com.google.gson.JsonObject; +import jakarta.ws.rs.core.MediaType; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; -import javax.ws.rs.core.MediaType; +import lombok.ToString; import org.junit.Test; import org.onap.policy.common.endpoints.http.server.YamlJacksonHandler; import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler; @@ -65,7 +68,7 @@ public class YamlJacksonHandlerTest { ByteArrayOutputStream outstr = new ByteArrayOutputStream(); hdlr.writeTo(data, Data.class, Data.class, null, null, null, outstr); - assertEquals("abc: def\nhello: world\nmyId: 100\nvalue: a value\n", outstr.toString("UTF-8")); + assertEquals("abc: def\nhello: world\nmyId: 100\nvalue: a value\n", outstr.toString(StandardCharsets.UTF_8)); /* * Ensure everything deserializes as expected. @@ -106,6 +109,7 @@ public class YamlJacksonHandlerTest { /** * This class includes all policy-specific gson annotations. */ + @ToString public static class Data { protected int id; @@ -144,11 +148,6 @@ public class YamlJacksonHandlerTest { props.put(name, value); } - - @Override - public String toString() { - return "Data [id=" + id + ", value=" + value + ", props=" + props + "]"; - } } private static class MyMap { diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlMessageBodyHandlerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlMessageBodyHandlerTest.java index 70b620c0..962c09db 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlMessageBodyHandlerTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlMessageBodyHandlerTest.java @@ -2,7 +2,8 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +26,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import jakarta.ws.rs.core.MediaType; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -32,7 +34,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; -import javax.ws.rs.core.MediaType; +import lombok.ToString; import org.junit.Before; import org.junit.Test; import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler; @@ -115,8 +117,7 @@ public class YamlMessageBodyHandlerTest { }; MyObject obj1 = new MyObject(10); - - assertThatThrownBy(() -> hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr)) + assertThatThrownBy(() -> hdlr.writeTo(obj1, MyObject.class, CLASS_OBJ, null, null, null, outstr)) .isInstanceOf(YAMLException.class); outstr.close(); @@ -213,6 +214,7 @@ public class YamlMessageBodyHandlerTest { } } + @ToString public static class MyObject { private int id; @@ -223,11 +225,6 @@ public class YamlMessageBodyHandlerTest { public MyObject(int id) { this.id = id; } - - @Override - public String toString() { - return "MyObject [id=" + id + "]"; - } } private static class MyMap { |