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.TreeMap;
35 import java.util.concurrent.CountDownLatch;
36 import java.util.concurrent.TimeUnit;
37 import javax.ws.rs.client.Entity;
38 import javax.ws.rs.client.InvocationCallback;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
47 import org.onap.policy.common.endpoints.http.client.HttpClient;
48 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
49 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
50 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
51 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
52 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
53 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
54 import org.onap.policy.common.utils.network.NetworkUtil;
56 public class HttpClientTest {
57 private static final String TEST_HTTP_NO_AUTH_CLIENT = "testHttpNoAuthClient";
58 private static final String TEST_HTTP_AUTH_CLIENT = "testHttpAuthClient";
59 private static final String LOCALHOST = "localhost";
60 private static final String JUNIT_ECHO = "junit/echo";
61 private static final String HELLO = "hello";
62 private static final String MY_VALUE = "myValue";
63 private static final String FALSE_STRING = "false";
64 private static final String ALPHA123 = "alpha123";
65 private static final String PUT_HELLO = "PUT:hello:{myParameter=myValue}";
66 private static final String DOT_GSON = "." + "GSON";
67 private static final String DOT_JACKSON = "." + "JACKSON";
68 private static final String DOT_PDP = "." + "PDP";
69 private static final String DOT_PAP = "." + "PAP";
71 private static final HashMap<String, String> savedValuesMap = new HashMap<>();
74 * Setup before class method.
76 * @throws InterruptedException can be interrupted
79 public static void setUpBeforeClass() throws InterruptedException {
80 /* echo server - http + no auth */
82 final HttpServletServer echoServerNoAuth =
83 HttpServletServerFactoryInstance.getServerFactory().build("echo", LOCALHOST, 6666, "/", false, true);
84 echoServerNoAuth.addServletPackage("/*", HttpClientTest.class.getPackage().getName());
85 echoServerNoAuth.waitedStart(5000);
87 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, echoServerNoAuth.getPort(), 5, 10000L)) {
88 throw new IllegalStateException("cannot connect to port " + echoServerNoAuth.getPort());
91 String keyStoreSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
92 if (keyStoreSystemProperty != null) {
93 savedValuesMap.put(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, keyStoreSystemProperty);
96 String keyStorePasswordSystemProperty =
97 System.getProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
98 if (keyStorePasswordSystemProperty != null) {
99 savedValuesMap.put(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME,
100 keyStorePasswordSystemProperty);
103 String trustStoreSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
104 if (trustStoreSystemProperty != null) {
105 savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, trustStoreSystemProperty);
108 String trustStorePasswordSystemProperty =
109 System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
110 if (trustStorePasswordSystemProperty != null) {
111 savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME,
112 trustStorePasswordSystemProperty);
115 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, "src/test/resources/keystore-test");
116 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME, "kstest");
118 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, "src/test/resources/keystore-test");
119 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME, "kstest");
121 /* echo server - https + basic auth */
123 final HttpServletServer echoServerAuth = HttpServletServerFactoryInstance.getServerFactory()
124 .build("echo", true, LOCALHOST, 6667, "/", false, true);
125 echoServerAuth.setBasicAuthentication("x", "y", null);
126 echoServerAuth.addServletPackage("/*", HttpClientTest.class.getPackage().getName());
127 echoServerAuth.addFilterClass("/*", TestFilter.class.getName());
128 echoServerAuth.addFilterClass("/*", TestAuthorizationFilter.class.getName());
129 echoServerAuth.addFilterClass("/*", TestAafAuthFilter.class.getName());
130 echoServerAuth.addFilterClass("/*", TestAafGranularAuthFilter.class.getName());
131 echoServerAuth.waitedStart(5000);
133 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, echoServerAuth.getPort(), 5, 10000L)) {
134 throw new IllegalStateException("cannot connect to port " + echoServerAuth.getPort());
139 * Clear https clients and reset providers.
142 public void setUp() {
143 HttpClientFactoryInstance.getClientFactory().destroy();
145 MyGsonProvider.resetSome();
146 MyJacksonProvider.resetSome();
150 * After the class is created method.
153 public static void tearDownAfterClass() {
154 HttpServletServerFactoryInstance.getServerFactory().destroy();
155 HttpClientFactoryInstance.getClientFactory().destroy();
157 if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME)) {
158 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME,
159 savedValuesMap.get(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME));
160 savedValuesMap.remove(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
162 System.clearProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
165 if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME)) {
166 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME,
167 savedValuesMap.get(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME));
168 savedValuesMap.remove(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
170 System.clearProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
173 if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME)) {
174 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME,
175 savedValuesMap.get(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME));
176 savedValuesMap.remove(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
178 System.clearProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
181 if (savedValuesMap.containsKey(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME)) {
182 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME,
183 savedValuesMap.get(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME));
184 savedValuesMap.remove(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
186 System.clearProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
193 public void testHttpGetNoAuthClient() throws Exception {
194 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
196 final Response response = client.get(HELLO);
197 final String body = HttpClient.getBody(response, String.class);
199 assertEquals(200, response.getStatus());
200 assertEquals(HELLO, body);
204 public void testHttpGetNoAuthClientAsync() throws Exception {
205 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
207 MyCallback callback = new MyCallback();
208 final Response response = client.get(callback, HELLO, new TreeMap<>()).get();
210 verifyCallback("testHttpGetNoAuthClientAsync", callback, response);
212 final String body = HttpClient.getBody(response, String.class);
214 assertEquals(200, response.getStatus());
215 assertEquals(HELLO, body);
218 private void verifyCallback(String testName, MyCallback callback, final Response response)
219 throws InterruptedException {
220 assertTrue(testName, callback.await());
221 assertNull(testName, callback.getThrowable());
222 assertSame(testName, response, callback.getResponse());
226 public void testHttpPutNoAuthClient() throws Exception {
227 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666);
229 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
230 final Response response = client.put(HELLO, entity, Collections.emptyMap());
231 final String body = HttpClient.getBody(response, String.class);
233 assertEquals(200, response.getStatus());
234 assertEquals(PUT_HELLO, body);
238 public void testHttpPutNoAuthClientAsync() throws Exception {
239 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666);
241 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
242 MyCallback callback = new MyCallback();
243 final Response response = client.put(callback, HELLO, entity, Collections.emptyMap()).get();
245 verifyCallback("testHttpPutNoAuthClientAsync", callback, response);
247 final String body = HttpClient.getBody(response, String.class);
249 assertEquals(200, response.getStatus());
250 assertEquals(PUT_HELLO, body);
254 public void testHttpPostNoAuthClient() throws Exception {
255 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
258 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
259 final Response response = client.post(HELLO, entity, Collections.emptyMap());
260 final String body = HttpClient.getBody(response, String.class);
262 assertEquals(200, response.getStatus());
263 assertEquals("POST:hello:{myParameter=myValue}", body);
267 public void testHttpPostNoAuthClientAsync() throws Exception {
268 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
271 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
272 MyCallback callback = new MyCallback();
273 final Response response = client.post(callback, HELLO, entity, Collections.emptyMap()).get();
275 verifyCallback("testHttpPostNoAuthClientAsync", callback, response);
277 final String body = HttpClient.getBody(response, String.class);
279 assertEquals(200, response.getStatus());
280 assertEquals("POST:hello:{myParameter=myValue}", body);
284 public void testHttpDeletetNoAuthClient() throws Exception {
285 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
288 final Response response = client.delete(HELLO, Collections.emptyMap());
289 final String body = HttpClient.getBody(response, String.class);
291 assertEquals(200, response.getStatus());
292 assertEquals("DELETE:hello", body);
296 public void testHttpDeletetNoAuthClientAsync() throws Exception {
297 final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
300 MyCallback callback = new MyCallback();
301 final Response response = client.delete(callback, HELLO, Collections.emptyMap()).get();
303 verifyCallback("testHttpDeletetNoAuthClientAsync", callback, response);
305 final String body = HttpClient.getBody(response, String.class);
307 assertEquals(200, response.getStatus());
308 assertEquals("DELETE:hello", body);
312 * Perform one asynchronous test with auth client; don't need to test every method.
313 * @throws Exception if an error occurs
316 public void testHttpAsyncAuthClient() throws Exception {
317 final HttpClient client = getAuthHttpClient();
319 MyCallback callback = new MyCallback();
320 final Response response = client.get(callback, HELLO, null).get();
322 verifyCallback("testHttpAsyncAuthClient", callback, response);
324 final String body = HttpClient.getBody(response, String.class);
326 assertEquals(200, response.getStatus());
327 assertEquals(HELLO, body);
331 public void testHttpGetAuthClient() throws Exception {
332 final HttpClient client = getAuthHttpClient();
334 final Response response = client.get(HELLO);
335 final String body = HttpClient.getBody(response, String.class);
337 assertEquals(200, response.getStatus());
338 assertEquals(HELLO, body);
342 public void testHttpPutAuthClient() throws Exception {
343 final HttpClient client = getAuthHttpClient();
345 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
346 final Response response = client.put(HELLO, entity, Collections.emptyMap());
347 final String body = HttpClient.getBody(response, String.class);
349 assertEquals(200, response.getStatus());
350 assertEquals(PUT_HELLO, body);
354 public void testHttpPutAuthClient_JacksonProvider() throws Exception {
355 final HttpClient client = HttpClientFactoryInstance.getClientFactory()
356 .build(BusTopicParams.builder().clientName(TEST_HTTP_AUTH_CLIENT).useHttps(true)
357 .allowSelfSignedCerts(true).hostname(LOCALHOST).port(6667).basePath(JUNIT_ECHO)
358 .userName("x").password("y").managed(true)
359 .serializationProvider(MyJacksonProvider.class.getName()).build());
361 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
362 final Response response = client.put(HELLO, entity, Collections.emptyMap());
363 final String body = HttpClient.getBody(response, String.class);
365 assertEquals(200, response.getStatus());
366 assertEquals(PUT_HELLO, body);
368 assertTrue(MyJacksonProvider.hasWrittenSome());
370 assertFalse(MyGsonProvider.hasWrittenSome());
374 public void testHttpPutAuthClient_GsonProvider() throws Exception {
375 final HttpClient client = HttpClientFactoryInstance.getClientFactory()
376 .build(BusTopicParams.builder().clientName(TEST_HTTP_AUTH_CLIENT).useHttps(true)
377 .allowSelfSignedCerts(true).hostname(LOCALHOST).port(6667).basePath(JUNIT_ECHO)
378 .userName("x").password("y").managed(true)
379 .serializationProvider(MyGsonProvider.class.getName()).build());
381 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
382 final Response response = client.put(HELLO, entity, Collections.emptyMap());
383 final String body = HttpClient.getBody(response, String.class);
385 assertEquals(200, response.getStatus());
386 assertEquals(PUT_HELLO, body);
388 assertTrue(MyGsonProvider.hasWrittenSome());
390 assertFalse(MyJacksonProvider.hasWrittenSome());
394 public void testHttpAuthClient401() throws Exception {
395 final HttpClient client = getNoAuthHttpClient("testHttpAuthClient401", true,
397 final Response response = client.get(HELLO);
398 assertEquals(401, response.getStatus());
402 public void testHttpAuthClientProps() throws Exception {
403 final Properties httpProperties = new Properties();
405 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, "PAP,PDP");
406 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
407 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
408 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
409 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777");
410 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
411 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpap");
412 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
413 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, ALPHA123);
414 httpProperties.setProperty(
415 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
416 + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
417 RestMockHealthCheck.class.getName());
418 httpProperties.setProperty(
419 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
420 + PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX,
421 TestFilter.class.getName());
422 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
423 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
425 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
426 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
427 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
428 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7778");
429 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
430 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpdp");
431 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
432 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, ALPHA123);
433 httpProperties.setProperty(
434 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
435 + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
436 RestMockHealthCheck.class.getName());
437 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
438 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
440 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES, "PAP,PDP");
441 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
442 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
443 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
444 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777");
445 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
446 + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, "pap/test");
447 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
448 + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING);
449 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
450 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpap");
451 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
452 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, ALPHA123);
453 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
454 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
456 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
457 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
458 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
459 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7778");
460 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
461 + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, "pdp");
462 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
463 + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING);
464 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
465 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpdp");
466 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
467 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, ALPHA123);
468 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
469 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
471 final List<HttpServletServer> servers =
472 HttpServletServerFactoryInstance.getServerFactory().build(httpProperties);
473 assertEquals(2, servers.size());
475 final List<HttpClient> clients = HttpClientFactoryInstance.getClientFactory().build(httpProperties);
476 assertEquals(2, clients.size());
478 for (final HttpServletServer server : servers) {
479 server.waitedStart(10000);
483 final HttpClient clientPap = HttpClientFactoryInstance.getClientFactory().get("PAP");
484 response = clientPap.get();
485 assertEquals(200, response.getStatus());
487 final HttpClient clientPdp = HttpClientFactoryInstance.getClientFactory().get("PDP");
488 response = clientPdp.get("test");
489 assertEquals(500, response.getStatus());
491 assertFalse(MyJacksonProvider.hasWrittenSome());
492 assertFalse(MyGsonProvider.hasWrittenSome());
494 // try with empty path
495 response = clientPap.get("");
496 assertEquals(200, response.getStatus());
498 // try it asynchronously, too
499 MyCallback callback = new MyCallback();
500 response = clientPap.get(callback, null).get();
501 verifyCallback("testHttpAuthClientProps", callback, response);
502 assertEquals(200, response.getStatus());
504 // try it asynchronously, with empty path
505 callback = new MyCallback();
506 response = clientPap.get(callback, "", null).get();
507 verifyCallback("testHttpAuthClientProps - empty path", callback, response);
508 assertEquals(200, response.getStatus());
512 public void testHttpAuthClientProps_MixedProviders() throws Exception {
513 final Properties httpProperties = new Properties();
515 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES, "GSON,JACKSON");
516 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON
517 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
518 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON
519 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "6666");
520 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON
521 + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, JUNIT_ECHO);
522 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON
523 + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING);
524 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON
525 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
526 httpProperties.setProperty(
527 PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_GSON
528 + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
529 MyGsonProvider.class.getName());
531 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON
532 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
533 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON
534 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "6666");
535 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON
536 + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, JUNIT_ECHO);
537 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON
538 + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING);
539 httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON
540 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
541 httpProperties.setProperty(
542 PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_JACKSON
543 + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
544 MyJacksonProvider.class.getName());
546 final List<HttpClient> clients = HttpClientFactoryInstance.getClientFactory().build(httpProperties);
547 assertEquals(2, clients.size());
549 Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
552 MyGsonProvider.resetSome();
553 MyJacksonProvider.resetSome();
554 HttpClient client = HttpClientFactoryInstance.getClientFactory().get("GSON");
556 Response response = client.put(HELLO, entity, Collections.emptyMap());
557 String body = HttpClient.getBody(response, String.class);
559 assertEquals(200, response.getStatus());
560 assertEquals(PUT_HELLO, body);
562 assertTrue(MyGsonProvider.hasWrittenSome());
563 assertFalse(MyJacksonProvider.hasWrittenSome());
565 // use jackson client
566 MyGsonProvider.resetSome();
567 MyJacksonProvider.resetSome();
568 client = HttpClientFactoryInstance.getClientFactory().get("JACKSON");
570 response = client.put(HELLO, entity, Collections.emptyMap());
571 body = HttpClient.getBody(response, String.class);
573 assertEquals(200, response.getStatus());
574 assertEquals(PUT_HELLO, body);
576 assertTrue(MyJacksonProvider.hasWrittenSome());
577 assertFalse(MyGsonProvider.hasWrittenSome());
580 private HttpClient getAuthHttpClient() throws HttpClientConfigException {
581 return HttpClientFactoryInstance.getClientFactory()
582 .build(BusTopicParams.builder().clientName(TEST_HTTP_AUTH_CLIENT).useHttps(true)
583 .allowSelfSignedCerts(true).hostname(LOCALHOST).port(6667).basePath(JUNIT_ECHO)
584 .userName("x").password("y").managed(true).build());
587 private HttpClient getNoAuthHttpClient(String clientName, boolean https, int port)
588 throws HttpClientConfigException {
589 return HttpClientFactoryInstance.getClientFactory()
590 .build(BusTopicParams.builder().clientName(clientName).useHttps(https)
591 .allowSelfSignedCerts(https).hostname(LOCALHOST).port(port).basePath(JUNIT_ECHO)
592 .userName(null).password(null).managed(true).build());
598 private String myParameter;
600 public MyEntity(final String myParameter) {
601 this.myParameter = myParameter;
604 public void setMyParameter(final String myParameter) {
605 this.myParameter = myParameter;
608 public String getMyParameter() {
614 class MyCallback implements InvocationCallback<Response> {
616 private Response response;
619 private Throwable throwable;
621 private CountDownLatch latch = new CountDownLatch(1);
624 public void completed(Response response) {
625 this.response = response;
630 public void failed(Throwable throwable) {
631 this.throwable = throwable;
635 public boolean await() throws InterruptedException {
636 return latch.await(5, TimeUnit.SECONDS);