2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 * Modifications Copyright 2023-2024 Nordix Foundation.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.common.endpoints.http.server.test;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNull;
28 import static org.junit.jupiter.api.Assertions.assertSame;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import io.prometheus.client.servlet.jakarta.exporter.MetricsServlet;
32 import jakarta.ws.rs.client.Entity;
33 import jakarta.ws.rs.client.InvocationCallback;
34 import jakarta.ws.rs.core.MediaType;
35 import jakarta.ws.rs.core.Response;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Properties;
40 import java.util.TreeMap;
41 import java.util.concurrent.CountDownLatch;
42 import java.util.concurrent.TimeUnit;
45 import org.junit.jupiter.api.AfterAll;
46 import org.junit.jupiter.api.BeforeAll;
47 import org.junit.jupiter.api.BeforeEach;
48 import org.junit.jupiter.api.Test;
49 import org.onap.policy.common.endpoints.http.client.HttpClient;
50 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
51 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
52 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
53 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
54 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
55 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
56 import org.onap.policy.common.parameters.topic.BusTopicParams;
57 import org.onap.policy.common.utils.network.NetworkUtil;
59 class HttpClientTest {
60 private static final String TEST_HTTP_NO_AUTH_CLIENT = "testHttpNoAuthClient";
61 private static final String TEST_HTTP_AUTH_CLIENT = "testHttpAuthClient";
62 private static final String LOCALHOST = "localhost";
63 private static final String JUNIT_ECHO = "junit/echo";
64 private static final String HELLO = "hello";
65 private static final String MY_VALUE = "myValue";
66 private static final String FALSE_STRING = "false";
67 private static final String ALPHA123 = "alpha123";
68 private static final String PUT_HELLO = "PUT:hello:{myParameter=myValue}";
69 private static final String DOT_PDP = "." + "PDP";
70 private static final String DOT_PAP = "." + "PAP";
72 private static final HashMap<String, String> savedValuesMap = new HashMap<>();
75 * Setup before class method.
77 * @throws InterruptedException can be interrupted
80 public static void setUpBeforeClass() throws InterruptedException {
81 /* echo server - http + no auth */
83 final HttpServletServer echoServerNoAuth =
84 HttpServletServerFactoryInstance.getServerFactory().build("echo", LOCALHOST, 6666, "/", false, true);
85 echoServerNoAuth.addServletPackage("/*", HttpClientTest.class.getPackage().getName());
86 echoServerNoAuth.waitedStart(5000);
88 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, echoServerNoAuth.getPort(), 5, 10000L)) {
89 throw new IllegalStateException("cannot connect to port " + echoServerNoAuth.getPort());
92 String keyStoreSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
93 if (keyStoreSystemProperty != null) {
94 savedValuesMap.put(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, keyStoreSystemProperty);
97 String keyStorePasswordSystemProperty =
98 System.getProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
99 if (keyStorePasswordSystemProperty != null) {
100 savedValuesMap.put(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME,
101 keyStorePasswordSystemProperty);
104 String trustStoreSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
105 if (trustStoreSystemProperty != null) {
106 savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, trustStoreSystemProperty);
109 String trustStorePasswordSystemProperty =
110 System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
111 if (trustStorePasswordSystemProperty != null) {
112 savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME,
113 trustStorePasswordSystemProperty);
116 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, "src/test/resources/keystore-test");
117 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME, "kstest");
119 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, "src/test/resources/keystore-test");
120 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME, "kstest");
122 /* echo server - https + basic auth */
124 final HttpServletServer echoServerAuth = HttpServletServerFactoryInstance.getServerFactory()
125 .build("echo", true, LOCALHOST, 6667, false, "/", false, true);
126 echoServerAuth.setBasicAuthentication("x", "y", null);
127 echoServerAuth.addServletPackage("/*", HttpClientTest.class.getPackage().getName());
128 echoServerAuth.addFilterClass("/*", TestFilter.class.getName());
129 echoServerAuth.addFilterClass("/*", TestAuthorizationFilter.class.getName());
130 echoServerAuth.waitedStart(5000);
132 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, echoServerAuth.getPort(), 5, 10000L)) {
133 throw new IllegalStateException("cannot connect to port " + echoServerAuth.getPort());
138 * Clear https clients and reset providers.
141 public void setUp() {
142 HttpClientFactoryInstance.getClientFactory().destroy();
144 MyGsonProvider.resetSome();
148 * After the class is created method.
151 public static void tearDownAfterClass() {
152 HttpServletServerFactoryInstance.getServerFactory().destroy();
153 HttpClientFactoryInstance.getClientFactory().destroy();
155 if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME)) {
156 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME,
157 savedValuesMap.get(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME));
158 savedValuesMap.remove(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
160 System.clearProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
163 if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME)) {
164 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME,
165 savedValuesMap.get(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME));
166 savedValuesMap.remove(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
168 System.clearProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
171 if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME)) {
172 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME,
173 savedValuesMap.get(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME));
174 savedValuesMap.remove(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
176 System.clearProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
179 if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME)) {
180 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME,
181 savedValuesMap.get(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME));
182 savedValuesMap.remove(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
184 System.clearProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
191 void testHttpGetNoAuthClient() throws Exception {
192 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
194 final Response response = client.get(HELLO);
195 final String body = HttpClient.getBody(response, String.class);
197 assertEquals(200, response.getStatus());
198 assertEquals(HELLO, body);
202 void testHttpGetNoAuthClientAsync() throws Exception {
203 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
205 MyCallback callback = new MyCallback();
206 final Response response = client.get(callback, HELLO, new TreeMap<>()).get();
208 verifyCallback("testHttpGetNoAuthClientAsync", callback, response);
210 final String body = HttpClient.getBody(response, String.class);
212 assertEquals(200, response.getStatus());
213 assertEquals(HELLO, body);
216 private void verifyCallback(String testName, MyCallback callback, final Response response)
217 throws InterruptedException {
218 assertTrue(callback.await(), testName);
219 assertNull(callback.getThrowable(), testName);
220 assertSame(response, callback.getResponse(), testName);
224 void testHttpPutNoAuthClient() throws Exception {
225 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666);
227 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
228 final Response response = client.put(HELLO, entity, Collections.emptyMap());
229 final String body = HttpClient.getBody(response, String.class);
231 assertEquals(200, response.getStatus());
232 assertEquals(PUT_HELLO, body);
236 void testHttpPutNoAuthClientAsync() throws Exception {
237 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666);
239 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
240 MyCallback callback = new MyCallback();
241 final Response response = client.put(callback, HELLO, entity, Collections.emptyMap()).get();
243 verifyCallback("testHttpPutNoAuthClientAsync", callback, response);
245 final String body = HttpClient.getBody(response, String.class);
247 assertEquals(200, response.getStatus());
248 assertEquals(PUT_HELLO, body);
252 void testHttpPostNoAuthClient() throws Exception {
253 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
256 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
257 final Response response = client.post(HELLO, entity, Collections.emptyMap());
258 final String body = HttpClient.getBody(response, String.class);
260 assertEquals(200, response.getStatus());
261 assertEquals("POST:hello:{myParameter=myValue}", body);
265 void testHttpPostNoAuthClientAsync() throws Exception {
266 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
269 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
270 MyCallback callback = new MyCallback();
271 final Response response = client.post(callback, HELLO, entity, Collections.emptyMap()).get();
273 verifyCallback("testHttpPostNoAuthClientAsync", callback, response);
275 final String body = HttpClient.getBody(response, String.class);
277 assertEquals(200, response.getStatus());
278 assertEquals("POST:hello:{myParameter=myValue}", body);
282 void testHttpDeletetNoAuthClient() throws Exception {
283 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
286 final Response response = client.delete(HELLO, Collections.emptyMap());
287 final String body = HttpClient.getBody(response, String.class);
289 assertEquals(200, response.getStatus());
290 assertEquals("DELETE:hello", body);
294 void testHttpDeletetNoAuthClientAsync() throws Exception {
295 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
298 MyCallback callback = new MyCallback();
299 final Response response = client.delete(callback, HELLO, Collections.emptyMap()).get();
301 verifyCallback("testHttpDeletetNoAuthClientAsync", callback, response);
303 final String body = HttpClient.getBody(response, String.class);
305 assertEquals(200, response.getStatus());
306 assertEquals("DELETE:hello", body);
310 * Perform one asynchronous test with auth client; don't need to test every method.
311 * @throws Exception if an error occurs
314 void testHttpAsyncAuthClient() throws Exception {
315 final HttpClient client = getAuthHttpClient();
317 MyCallback callback = new MyCallback();
318 final Response response = client.get(callback, HELLO, null).get();
320 verifyCallback("testHttpAsyncAuthClient", callback, response);
322 final String body = HttpClient.getBody(response, String.class);
324 assertEquals(200, response.getStatus());
325 assertEquals(HELLO, body);
329 void testHttpGetAuthClient() throws Exception {
330 final HttpClient client = getAuthHttpClient();
332 final Response response = client.get(HELLO);
333 final String body = HttpClient.getBody(response, String.class);
335 assertEquals(200, response.getStatus());
336 assertEquals(HELLO, body);
340 void testHttpPutAuthClient() throws Exception {
341 final HttpClient client = getAuthHttpClient();
343 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
344 final Response response = client.put(HELLO, entity, Collections.emptyMap());
345 final String body = HttpClient.getBody(response, String.class);
347 assertEquals(200, response.getStatus());
348 assertEquals(PUT_HELLO, body);
352 void testHttpPutAuthClient_GsonProvider() throws Exception {
353 final HttpClient client = HttpClientFactoryInstance.getClientFactory()
354 .build(BusTopicParams.builder().clientName(TEST_HTTP_AUTH_CLIENT).useHttps(true)
355 .allowSelfSignedCerts(true).hostname(LOCALHOST).port(6667).basePath(JUNIT_ECHO)
356 .userName("x").password("y").managed(true)
357 .serializationProvider(MyGsonProvider.class.getName()).build());
359 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
360 final Response response = client.put(HELLO, entity, Collections.emptyMap());
361 final String body = HttpClient.getBody(response, String.class);
363 assertEquals(200, response.getStatus());
364 assertEquals(PUT_HELLO, body);
366 assertTrue(MyGsonProvider.hasWrittenSome());
370 void testHttpAuthClient401() throws Exception {
371 final HttpClient client = getNoAuthHttpClient("testHttpAuthClient401", true,
373 final Response response = client.get(HELLO);
374 assertEquals(401, response.getStatus());
378 void testHttpAuthClientProps() throws Exception {
379 final Properties httpProperties = new Properties();
381 /* PAP and PDP services */
383 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, "PAP,PDP");
385 /* PAP server service configuration */
387 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
388 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
389 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
390 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777");
391 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
392 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpap");
393 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
394 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, ALPHA123);
395 httpProperties.setProperty(
396 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
397 + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
398 RestMockHealthCheck.class.getName());
399 httpProperties.setProperty(
400 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
401 + PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX,
402 TestFilter.class.getName());
403 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
404 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
405 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
406 + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_CLASS_SUFFIX, MetricsServlet.class.getName());
407 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
408 + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_URIPATH_SUFFIX,
409 "/pap/test/random/metrics");
411 /* PDP server service configuration */
413 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
414 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
415 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
416 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7778");
417 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
418 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpdp");
419 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
420 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, ALPHA123);
421 httpProperties.setProperty(
422 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
423 + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
424 RestMockHealthCheck.class.getName());
425 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
426 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
427 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
428 + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
429 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
430 + PolicyEndPointProperties.PROPERTY_HTTP_PROMETHEUS_SUFFIX, "true");
432 /* PDP and PAP client services */
434 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES, "PAP,PDP");
436 /* PAP client service configuration */
438 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
439 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
440 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
441 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777");
442 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
443 + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, "pap/test");
444 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
445 + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING);
446 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
447 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpap");
448 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
449 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, ALPHA123);
450 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
451 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
453 /* PDP client service configuration */
455 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
456 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
457 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
458 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7778");
459 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
460 + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING);
461 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
462 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpdp");
463 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
464 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, ALPHA123);
465 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
466 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
468 final List<HttpServletServer> servers =
469 HttpServletServerFactoryInstance.getServerFactory().build(httpProperties);
470 assertEquals(2, servers.size());
472 final List<HttpClient> clients = HttpClientFactoryInstance.getClientFactory().build(httpProperties);
473 assertEquals(2, clients.size());
475 for (final HttpServletServer server : servers) {
476 server.waitedStart(10000);
480 final HttpClient clientPap = HttpClientFactoryInstance.getClientFactory().get("PAP");
481 response = clientPap.get();
482 assertEquals(200, response.getStatus());
484 final HttpClient clientPdp = HttpClientFactoryInstance.getClientFactory().get("PDP");
486 response = clientPdp.get("pdp/test");
487 assertEquals(500, response.getStatus());
489 response = clientPdp.get("metrics");
490 assertEquals(200, response.getStatus());
492 response = clientPdp.get("openapi.json");
493 assertEquals(200, response.getStatus());
495 assertFalse(MyGsonProvider.hasWrittenSome());
497 // try with empty path
498 response = clientPap.get("");
499 assertEquals(200, response.getStatus());
501 response = clientPap.get("random/metrics");
502 assertEquals(200, response.getStatus());
504 response = clientPap.get("metrics");
505 assertEquals(404, response.getStatus());
507 // try it asynchronously, too
508 MyCallback callback = new MyCallback();
509 response = clientPap.get(callback, null).get();
510 verifyCallback("testHttpAuthClientProps", callback, response);
511 assertEquals(200, response.getStatus());
513 // try it asynchronously, with empty path
514 callback = new MyCallback();
515 response = clientPap.get(callback, "", null).get();
516 verifyCallback("testHttpAuthClientProps - empty path", callback, response);
517 assertEquals(200, response.getStatus());
520 private HttpClient getAuthHttpClient() throws HttpClientConfigException {
521 return HttpClientFactoryInstance.getClientFactory()
522 .build(BusTopicParams.builder().clientName(TEST_HTTP_AUTH_CLIENT).useHttps(true)
523 .allowSelfSignedCerts(true).hostname(LOCALHOST).port(6667).basePath(JUNIT_ECHO)
524 .userName("x").password("y").managed(true).build());
527 private HttpClient getNoAuthHttpClient(String clientName, boolean https, int port)
528 throws HttpClientConfigException {
529 return HttpClientFactoryInstance.getClientFactory()
530 .build(BusTopicParams.builder().clientName(clientName).useHttps(https)
531 .allowSelfSignedCerts(https).hostname(LOCALHOST).port(port).basePath(JUNIT_ECHO)
532 .userName(null).password(null).managed(true).build());
538 static class MyEntity {
540 private String myParameter;
542 public MyEntity(final String myParameter) {
543 this.myParameter = myParameter;
548 static class MyCallback implements InvocationCallback<Response> {
550 private Response response;
553 private Throwable throwable;
555 private final CountDownLatch latch = new CountDownLatch(1);
558 public void completed(Response response) {
559 this.response = response;
564 public void failed(Throwable throwable) {
565 this.throwable = throwable;
569 public boolean await() throws InterruptedException {
570 return latch.await(5, TimeUnit.SECONDS);