2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 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 java.util.Collections;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Properties;
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.TimeUnit;
36 import javax.ws.rs.client.Entity;
37 import javax.ws.rs.client.InvocationCallback;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
46 import org.onap.policy.common.endpoints.http.client.HttpClient;
47 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
48 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
49 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
50 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
51 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
52 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
53 import org.onap.policy.common.utils.network.NetworkUtil;
55 public class HttpClientTest {
56 private static final String TEST_HTTP_NO_AUTH_CLIENT = "testHttpNoAuthClient";
57 private static final String TEST_HTTP_AUTH_CLIENT = "testHttpAuthClient";
58 private static final String LOCALHOST = "localhost";
59 private static final String JUNIT_ECHO = "junit/echo";
60 private static final String HELLO = "hello";
61 private static final String MY_VALUE = "myValue";
62 private static final String FALSE_STRING = "false";
63 private static final String ALPHA123 = "alpha123";
64 private static final String PUT_HELLO = "PUT:hello:{myParameter=myValue}";
65 private static final String DOT_GSON = "." + "GSON";
66 private static final String DOT_JACKSON = "." + "JACKSON";
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();
145 MyJacksonProvider.resetSome();
149 * After the class is created method.
152 public static void tearDownAfterClass() {
153 HttpServletServerFactoryInstance.getServerFactory().destroy();
154 HttpClientFactoryInstance.getClientFactory().destroy();
156 if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME)) {
157 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME,
158 savedValuesMap.get(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME));
159 savedValuesMap.remove(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
161 System.clearProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
164 if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME)) {
165 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME,
166 savedValuesMap.get(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME));
167 savedValuesMap.remove(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
169 System.clearProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
172 if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME)) {
173 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME,
174 savedValuesMap.get(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME));
175 savedValuesMap.remove(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
177 System.clearProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
180 if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME)) {
181 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME,
182 savedValuesMap.get(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME));
183 savedValuesMap.remove(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
185 System.clearProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
192 public void testHttpGetNoAuthClient() throws Exception {
193 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
195 final Response response = client.get(HELLO);
196 final String body = HttpClient.getBody(response, String.class);
198 assertEquals(200, response.getStatus());
199 assertEquals(HELLO, body);
203 public void testHttpGetNoAuthClientAsync() throws Exception {
204 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
206 MyCallback callback = new MyCallback();
207 final Response response = client.get(callback, HELLO).get();
209 verifyCallback("testHttpGetNoAuthClientAsync", callback, response);
211 final String body = HttpClient.getBody(response, String.class);
213 assertEquals(200, response.getStatus());
214 assertEquals(HELLO, body);
217 private void verifyCallback(String testName, MyCallback callback, final Response response)
218 throws InterruptedException {
219 assertTrue(testName, callback.await());
220 assertNull(testName, callback.getThrowable());
221 assertSame(testName, response, callback.getResponse());
225 public void testHttpPutNoAuthClient() throws Exception {
226 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666);
228 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
229 final Response response = client.put(HELLO, entity, Collections.emptyMap());
230 final String body = HttpClient.getBody(response, String.class);
232 assertEquals(200, response.getStatus());
233 assertEquals(PUT_HELLO, body);
237 public void testHttpPutNoAuthClientAsync() throws Exception {
238 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666);
240 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
241 MyCallback callback = new MyCallback();
242 final Response response = client.put(callback, HELLO, entity, Collections.emptyMap()).get();
244 verifyCallback("testHttpPutNoAuthClientAsync", callback, response);
246 final String body = HttpClient.getBody(response, String.class);
248 assertEquals(200, response.getStatus());
249 assertEquals(PUT_HELLO, body);
253 public void testHttpPostNoAuthClient() throws Exception {
254 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
257 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
258 final Response response = client.post(HELLO, entity, Collections.emptyMap());
259 final String body = HttpClient.getBody(response, String.class);
261 assertEquals(200, response.getStatus());
262 assertEquals("POST:hello:{myParameter=myValue}", body);
266 public void testHttpPostNoAuthClientAsync() throws Exception {
267 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
270 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
271 MyCallback callback = new MyCallback();
272 final Response response = client.post(callback, HELLO, entity, Collections.emptyMap()).get();
274 verifyCallback("testHttpPostNoAuthClientAsync", callback, response);
276 final String body = HttpClient.getBody(response, String.class);
278 assertEquals(200, response.getStatus());
279 assertEquals("POST:hello:{myParameter=myValue}", body);
283 public void testHttpDeletetNoAuthClient() throws Exception {
284 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
287 final Response response = client.delete(HELLO, Collections.emptyMap());
288 final String body = HttpClient.getBody(response, String.class);
290 assertEquals(200, response.getStatus());
291 assertEquals("DELETE:hello", body);
295 public void testHttpDeletetNoAuthClientAsync() throws Exception {
296 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
299 MyCallback callback = new MyCallback();
300 final Response response = client.delete(callback, HELLO, Collections.emptyMap()).get();
302 verifyCallback("testHttpDeletetNoAuthClientAsync", callback, response);
304 final String body = HttpClient.getBody(response, String.class);
306 assertEquals(200, response.getStatus());
307 assertEquals("DELETE:hello", body);
311 * Perform one asynchronous test with auth client; don't need to test every method.
312 * @throws Exception if an error occurs
315 public void testHttpAsyncAuthClient() throws Exception {
316 final HttpClient client = getAuthHttpClient();
318 MyCallback callback = new MyCallback();
319 final Response response = client.get(callback, HELLO).get();
321 verifyCallback("testHttpAsyncAuthClient", callback, response);
323 final String body = HttpClient.getBody(response, String.class);
325 assertEquals(200, response.getStatus());
326 assertEquals(HELLO, body);
330 public void testHttpGetAuthClient() throws Exception {
331 final HttpClient client = getAuthHttpClient();
333 final Response response = client.get(HELLO);
334 final String body = HttpClient.getBody(response, String.class);
336 assertEquals(200, response.getStatus());
337 assertEquals(HELLO, body);
341 public void testHttpPutAuthClient() throws Exception {
342 final HttpClient client = getAuthHttpClient();
344 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
345 final Response response = client.put(HELLO, entity, Collections.emptyMap());
346 final String body = HttpClient.getBody(response, String.class);
348 assertEquals(200, response.getStatus());
349 assertEquals(PUT_HELLO, body);
353 public void testHttpPutAuthClient_JacksonProvider() throws Exception {
354 final HttpClient client = HttpClientFactoryInstance.getClientFactory()
355 .build(BusTopicParams.builder().clientName(TEST_HTTP_AUTH_CLIENT).useHttps(true)
356 .allowSelfSignedCerts(true).hostname(LOCALHOST).port(6667).basePath(JUNIT_ECHO)
357 .userName("x").password("y").managed(true)
358 .serializationProvider(MyJacksonProvider.class.getName()).build());
360 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
361 final Response response = client.put(HELLO, entity, Collections.emptyMap());
362 final String body = HttpClient.getBody(response, String.class);
364 assertEquals(200, response.getStatus());
365 assertEquals(PUT_HELLO, body);
367 assertTrue(MyJacksonProvider.hasWrittenSome());
369 assertFalse(MyGsonProvider.hasWrittenSome());
373 public void testHttpPutAuthClient_GsonProvider() throws Exception {
374 final HttpClient client = HttpClientFactoryInstance.getClientFactory()
375 .build(BusTopicParams.builder().clientName(TEST_HTTP_AUTH_CLIENT).useHttps(true)
376 .allowSelfSignedCerts(true).hostname(LOCALHOST).port(6667).basePath(JUNIT_ECHO)
377 .userName("x").password("y").managed(true)
378 .serializationProvider(MyGsonProvider.class.getName()).build());
380 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
381 final Response response = client.put(HELLO, entity, Collections.emptyMap());
382 final String body = HttpClient.getBody(response, String.class);
384 assertEquals(200, response.getStatus());
385 assertEquals(PUT_HELLO, body);
387 assertTrue(MyGsonProvider.hasWrittenSome());
389 assertFalse(MyJacksonProvider.hasWrittenSome());
393 public void testHttpAuthClient401() throws Exception {
394 final HttpClient client = getNoAuthHttpClient("testHttpAuthClient401", true,
396 final Response response = client.get(HELLO);
397 assertEquals(401, response.getStatus());
401 public void testHttpAuthClientProps() throws Exception {
402 final Properties httpProperties = new Properties();
404 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, "PAP,PDP");
405 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
406 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
407 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
408 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777");
409 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
410 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpap");
411 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
412 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, ALPHA123);
413 httpProperties.setProperty(
414 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
415 + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
416 RestMockHealthCheck.class.getName());
417 httpProperties.setProperty(
418 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
419 + PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX,
420 TestFilter.class.getName());
421 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
422 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
424 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
425 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
426 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
427 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7778");
428 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
429 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpdp");
430 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
431 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, ALPHA123);
432 httpProperties.setProperty(
433 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
434 + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
435 RestMockHealthCheck.class.getName());
436 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
437 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
439 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES, "PAP,PDP");
440 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
441 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
442 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
443 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777");
444 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
445 + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, "pap/test");
446 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
447 + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING);
448 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
449 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpap");
450 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
451 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, ALPHA123);
452 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
453 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
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_URL_SUFFIX, "pdp");
461 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
462 + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING);
463 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
464 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpdp");
465 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
466 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, ALPHA123);
467 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
468 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
470 final List<HttpServletServer> servers =
471 HttpServletServerFactoryInstance.getServerFactory().build(httpProperties);
472 assertEquals(2, servers.size());
474 final List<HttpClient> clients = HttpClientFactoryInstance.getClientFactory().build(httpProperties);
475 assertEquals(2, clients.size());
477 for (final HttpServletServer server : servers) {
478 server.waitedStart(10000);
482 final HttpClient clientPap = HttpClientFactoryInstance.getClientFactory().get("PAP");
483 response = clientPap.get();
484 assertEquals(200, response.getStatus());
486 final HttpClient clientPdp = HttpClientFactoryInstance.getClientFactory().get("PDP");
487 response = clientPdp.get("test");
488 assertEquals(500, response.getStatus());
490 assertFalse(MyJacksonProvider.hasWrittenSome());
491 assertFalse(MyGsonProvider.hasWrittenSome());
493 // try with empty path
494 response = clientPap.get("");
495 assertEquals(200, response.getStatus());
497 // try it asynchronously, too
498 MyCallback callback = new MyCallback();
499 response = clientPap.get(callback).get();
500 verifyCallback("testHttpAuthClientProps", callback, response);
501 assertEquals(200, response.getStatus());
503 // try it asynchronously, with empty path
504 callback = new MyCallback();
505 response = clientPap.get(callback, "").get();
506 verifyCallback("testHttpAuthClientProps - empty path", callback, response);
507 assertEquals(200, response.getStatus());
511 public void testHttpAuthClientProps_MixedProviders() throws Exception {
512 final Properties httpProperties = new Properties();
514 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES, "GSON,JACKSON");
515 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON
516 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
517 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON
518 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "6666");
519 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON
520 + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, JUNIT_ECHO);
521 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON
522 + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING);
523 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON
524 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
525 httpProperties.setProperty(
526 PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON
527 + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
528 MyGsonProvider.class.getName());
530 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON
531 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
532 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON
533 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "6666");
534 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON
535 + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, JUNIT_ECHO);
536 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON
537 + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING);
538 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON
539 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
540 httpProperties.setProperty(
541 PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON
542 + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
543 MyJacksonProvider.class.getName());
545 final List<HttpClient> clients = HttpClientFactoryInstance.getClientFactory().build(httpProperties);
546 assertEquals(2, clients.size());
548 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
551 MyGsonProvider.resetSome();
552 MyJacksonProvider.resetSome();
553 HttpClient client = HttpClientFactoryInstance.getClientFactory().get("GSON");
555 Response response = client.put(HELLO, entity, Collections.emptyMap());
556 String body = HttpClient.getBody(response, String.class);
558 assertEquals(200, response.getStatus());
559 assertEquals(PUT_HELLO, body);
561 assertTrue(MyGsonProvider.hasWrittenSome());
562 assertFalse(MyJacksonProvider.hasWrittenSome());
564 // use jackson client
565 MyGsonProvider.resetSome();
566 MyJacksonProvider.resetSome();
567 client = HttpClientFactoryInstance.getClientFactory().get("JACKSON");
569 response = client.put(HELLO, entity, Collections.emptyMap());
570 body = HttpClient.getBody(response, String.class);
572 assertEquals(200, response.getStatus());
573 assertEquals(PUT_HELLO, body);
575 assertTrue(MyJacksonProvider.hasWrittenSome());
576 assertFalse(MyGsonProvider.hasWrittenSome());
579 private HttpClient getAuthHttpClient() throws HttpClientConfigException {
580 return HttpClientFactoryInstance.getClientFactory()
581 .build(BusTopicParams.builder().clientName(TEST_HTTP_AUTH_CLIENT).useHttps(true)
582 .allowSelfSignedCerts(true).hostname(LOCALHOST).port(6667).basePath(JUNIT_ECHO)
583 .userName("x").password("y").managed(true).build());
586 private HttpClient getNoAuthHttpClient(String clientName, boolean https, int port)
587 throws HttpClientConfigException {
588 return HttpClientFactoryInstance.getClientFactory()
589 .build(BusTopicParams.builder().clientName(clientName).useHttps(https)
590 .allowSelfSignedCerts(https).hostname(LOCALHOST).port(port).basePath(JUNIT_ECHO)
591 .userName(null).password(null).managed(true).build());
597 private String myParameter;
599 public MyEntity(final String myParameter) {
600 this.myParameter = myParameter;
603 public void setMyParameter(final String myParameter) {
604 this.myParameter = myParameter;
607 public String getMyParameter() {
613 class MyCallback implements InvocationCallback<Response> {
615 private Response response;
618 private Throwable throwable;
620 private CountDownLatch latch = new CountDownLatch(1);
623 public void completed(Response response) {
624 this.response = response;
629 public void failed(Throwable throwable) {
630 this.throwable = throwable;
634 public boolean await() throws InterruptedException {
635 return latch.await(5, TimeUnit.SECONDS);