aboutsummaryrefslogtreecommitdiffstats
path: root/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java')
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java272
1 files changed, 175 insertions, 97 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);
+ }
+ }
}