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 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.common.endpoints.http.server.test;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
30 import io.prometheus.client.exporter.MetricsServlet;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Properties;
35 import java.util.TreeMap;
36 import java.util.concurrent.CountDownLatch;
37 import java.util.concurrent.TimeUnit;
38 import javax.ws.rs.client.Entity;
39 import javax.ws.rs.client.InvocationCallback;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.Response;
43 import org.junit.AfterClass;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
48 import org.onap.policy.common.endpoints.http.client.HttpClient;
49 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
50 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
51 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
52 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
53 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
54 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
55 import org.onap.policy.common.utils.network.NetworkUtil;
57 public class HttpClientTest {
58 private static final String TEST_HTTP_NO_AUTH_CLIENT = "testHttpNoAuthClient";
59 private static final String TEST_HTTP_AUTH_CLIENT = "testHttpAuthClient";
60 private static final String LOCALHOST = "localhost";
61 private static final String JUNIT_ECHO = "junit/echo";
62 private static final String HELLO = "hello";
63 private static final String MY_VALUE = "myValue";
64 private static final String FALSE_STRING = "false";
65 private static final String ALPHA123 = "alpha123";
66 private static final String PUT_HELLO = "PUT:hello:{myParameter=myValue}";
67 private static final String DOT_PDP = "." + "PDP";
68 private static final String DOT_PAP = "." + "PAP";
70 private static final HashMap<String, String> savedValuesMap = new HashMap<>();
73 * Setup before class method.
75 * @throws InterruptedException can be interrupted
78 public static void setUpBeforeClass() throws InterruptedException {
79 /* echo server - http + no auth */
81 final HttpServletServer echoServerNoAuth =
82 HttpServletServerFactoryInstance.getServerFactory().build("echo", LOCALHOST, 6666, "/", false, true);
83 echoServerNoAuth.addServletPackage("/*", HttpClientTest.class.getPackage().getName());
84 echoServerNoAuth.waitedStart(5000);
86 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, echoServerNoAuth.getPort(), 5, 10000L)) {
87 throw new IllegalStateException("cannot connect to port " + echoServerNoAuth.getPort());
90 String keyStoreSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
91 if (keyStoreSystemProperty != null) {
92 savedValuesMap.put(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, keyStoreSystemProperty);
95 String keyStorePasswordSystemProperty =
96 System.getProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
97 if (keyStorePasswordSystemProperty != null) {
98 savedValuesMap.put(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME,
99 keyStorePasswordSystemProperty);
102 String trustStoreSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
103 if (trustStoreSystemProperty != null) {
104 savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, trustStoreSystemProperty);
107 String trustStorePasswordSystemProperty =
108 System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
109 if (trustStorePasswordSystemProperty != null) {
110 savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME,
111 trustStorePasswordSystemProperty);
114 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, "src/test/resources/keystore-test");
115 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME, "kstest");
117 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, "src/test/resources/keystore-test");
118 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME, "kstest");
120 /* echo server - https + basic auth */
122 final HttpServletServer echoServerAuth = HttpServletServerFactoryInstance.getServerFactory()
123 .build("echo", true, LOCALHOST, 6667, "/", false, true);
124 echoServerAuth.setBasicAuthentication("x", "y", null);
125 echoServerAuth.addServletPackage("/*", HttpClientTest.class.getPackage().getName());
126 echoServerAuth.addFilterClass("/*", TestFilter.class.getName());
127 echoServerAuth.addFilterClass("/*", TestAuthorizationFilter.class.getName());
128 echoServerAuth.addFilterClass("/*", TestAafAuthFilter.class.getName());
129 echoServerAuth.addFilterClass("/*", TestAafGranularAuthFilter.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 public 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 public 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(testName, callback.await());
219 assertNull(testName, callback.getThrowable());
220 assertSame(testName, response, callback.getResponse());
224 public 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 public 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 public 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 public 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 public 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 public 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 public 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 public 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 public 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 public 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 public void testHttpAuthClient401() throws Exception {
371 final HttpClient client = getNoAuthHttpClient("testHttpAuthClient401", true,
373 final Response response = client.get(HELLO);
374 assertEquals(401, response.getStatus());
378 public 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("swagger.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 private String myParameter;
540 public MyEntity(final String myParameter) {
541 this.myParameter = myParameter;
544 public void setMyParameter(final String myParameter) {
545 this.myParameter = myParameter;
548 public String getMyParameter() {
554 class MyCallback implements InvocationCallback<Response> {
556 private Response response;
559 private Throwable throwable;
561 private CountDownLatch latch = new CountDownLatch(1);
564 public void completed(Response response) {
565 this.response = response;
570 public void failed(Throwable throwable) {
571 this.throwable = throwable;
575 public boolean await() throws InterruptedException {
576 return latch.await(5, TimeUnit.SECONDS);