d3f94cd0c40f2b656c0c503ec53e7773c4ef8327
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.http.server.test;
23
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;
29
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;
40 import lombok.Getter;
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;
54
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";
69
70     private static final HashMap<String, String> savedValuesMap = new HashMap<>();
71
72     /**
73      * Setup before class method.
74      *
75      * @throws InterruptedException can be interrupted
76      */
77     @BeforeClass
78     public static void setUpBeforeClass() throws InterruptedException {
79         /* echo server - http + no auth */
80
81         final HttpServletServer echoServerNoAuth =
82                 HttpServletServerFactoryInstance.getServerFactory().build("echo", LOCALHOST, 6666, "/", false, true);
83         echoServerNoAuth.addServletPackage("/*", HttpClientTest.class.getPackage().getName());
84         echoServerNoAuth.waitedStart(5000);
85
86         if (!NetworkUtil.isTcpPortOpen(LOCALHOST, echoServerNoAuth.getPort(), 5, 10000L)) {
87             throw new IllegalStateException("cannot connect to port " + echoServerNoAuth.getPort());
88         }
89
90         String keyStoreSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
91         if (keyStoreSystemProperty != null) {
92             savedValuesMap.put(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, keyStoreSystemProperty);
93         }
94
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);
100         }
101
102         String trustStoreSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
103         if (trustStoreSystemProperty != null) {
104             savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, trustStoreSystemProperty);
105         }
106
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);
112         }
113
114         System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, "src/test/resources/keystore-test");
115         System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME, "kstest");
116
117         System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, "src/test/resources/keystore-test");
118         System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME, "kstest");
119
120         /* echo server - https + basic auth */
121
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);
131
132         if (!NetworkUtil.isTcpPortOpen(LOCALHOST, echoServerAuth.getPort(), 5, 10000L)) {
133             throw new IllegalStateException("cannot connect to port " + echoServerAuth.getPort());
134         }
135     }
136
137     /**
138      * Clear https clients and reset providers.
139      */
140     @Before
141     public void setUp() {
142         HttpClientFactoryInstance.getClientFactory().destroy();
143
144         MyGsonProvider.resetSome();
145         MyJacksonProvider.resetSome();
146     }
147
148     /**
149      * After the class is created method.
150      */
151     @AfterClass
152     public static void tearDownAfterClass() {
153         HttpServletServerFactoryInstance.getServerFactory().destroy();
154         HttpClientFactoryInstance.getClientFactory().destroy();
155
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);
160         } else {
161             System.clearProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME);
162         }
163
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);
168         } else {
169             System.clearProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
170         }
171
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);
176         } else {
177             System.clearProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
178         }
179
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);
184         } else {
185             System.clearProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
186         }
187
188
189     }
190
191     @Test
192     public void testHttpGetNoAuthClient() throws Exception {
193         final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
194             6666);
195         final Response response = client.get(HELLO);
196         final String body = HttpClient.getBody(response, String.class);
197
198         assertEquals(200, response.getStatus());
199         assertEquals(HELLO, body);
200     }
201
202     @Test
203     public void testHttpGetNoAuthClientAsync() throws Exception {
204         final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
205             6666);
206         MyCallback callback = new MyCallback();
207         final Response response = client.get(callback, HELLO).get();
208
209         verifyCallback("testHttpGetNoAuthClientAsync", callback, response);
210
211         final String body = HttpClient.getBody(response, String.class);
212
213         assertEquals(200, response.getStatus());
214         assertEquals(HELLO, body);
215     }
216
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());
222     }
223
224     @Test
225     public void testHttpPutNoAuthClient() throws Exception {
226         final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666);
227
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);
231
232         assertEquals(200, response.getStatus());
233         assertEquals(PUT_HELLO, body);
234     }
235
236     @Test
237     public void testHttpPutNoAuthClientAsync() throws Exception {
238         final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666);
239
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();
243
244         verifyCallback("testHttpPutNoAuthClientAsync", callback, response);
245
246         final String body = HttpClient.getBody(response, String.class);
247
248         assertEquals(200, response.getStatus());
249         assertEquals(PUT_HELLO, body);
250     }
251
252     @Test
253     public void testHttpPostNoAuthClient() throws Exception {
254         final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
255             6666);
256
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);
260
261         assertEquals(200, response.getStatus());
262         assertEquals("POST:hello:{myParameter=myValue}", body);
263     }
264
265     @Test
266     public void testHttpPostNoAuthClientAsync() throws Exception {
267         final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
268             6666);
269
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();
273
274         verifyCallback("testHttpPostNoAuthClientAsync", callback, response);
275
276         final String body = HttpClient.getBody(response, String.class);
277
278         assertEquals(200, response.getStatus());
279         assertEquals("POST:hello:{myParameter=myValue}", body);
280     }
281
282     @Test
283     public void testHttpDeletetNoAuthClient() throws Exception {
284         final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
285             6666);
286
287         final Response response = client.delete(HELLO, Collections.emptyMap());
288         final String body = HttpClient.getBody(response, String.class);
289
290         assertEquals(200, response.getStatus());
291         assertEquals("DELETE:hello", body);
292     }
293
294     @Test
295     public void testHttpDeletetNoAuthClientAsync() throws Exception {
296         final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false,
297             6666);
298
299         MyCallback callback = new MyCallback();
300         final Response response = client.delete(callback, HELLO, Collections.emptyMap()).get();
301
302         verifyCallback("testHttpDeletetNoAuthClientAsync", callback, response);
303
304         final String body = HttpClient.getBody(response, String.class);
305
306         assertEquals(200, response.getStatus());
307         assertEquals("DELETE:hello", body);
308     }
309
310     /**
311      * Perform one asynchronous test with auth client; don't need to test every method.
312      * @throws Exception if an error occurs
313      */
314     @Test
315     public void testHttpAsyncAuthClient() throws Exception {
316         final HttpClient client = getAuthHttpClient();
317
318         MyCallback callback = new MyCallback();
319         final Response response = client.get(callback, HELLO).get();
320
321         verifyCallback("testHttpAsyncAuthClient", callback, response);
322
323         final String body = HttpClient.getBody(response, String.class);
324
325         assertEquals(200, response.getStatus());
326         assertEquals(HELLO, body);
327     }
328
329     @Test
330     public void testHttpGetAuthClient() throws Exception {
331         final HttpClient client = getAuthHttpClient();
332
333         final Response response = client.get(HELLO);
334         final String body = HttpClient.getBody(response, String.class);
335
336         assertEquals(200, response.getStatus());
337         assertEquals(HELLO, body);
338     }
339
340     @Test
341     public void testHttpPutAuthClient() throws Exception {
342         final HttpClient client = getAuthHttpClient();
343
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);
347
348         assertEquals(200, response.getStatus());
349         assertEquals(PUT_HELLO, body);
350     }
351
352     @Test
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());
359
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);
363
364         assertEquals(200, response.getStatus());
365         assertEquals(PUT_HELLO, body);
366
367         assertTrue(MyJacksonProvider.hasWrittenSome());
368
369         assertFalse(MyGsonProvider.hasWrittenSome());
370     }
371
372     @Test
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());
379
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);
383
384         assertEquals(200, response.getStatus());
385         assertEquals(PUT_HELLO, body);
386
387         assertTrue(MyGsonProvider.hasWrittenSome());
388
389         assertFalse(MyJacksonProvider.hasWrittenSome());
390     }
391
392     @Test
393     public void testHttpAuthClient401() throws Exception {
394         final HttpClient client = getNoAuthHttpClient("testHttpAuthClient401", true,
395             6667);
396         final Response response = client.get(HELLO);
397         assertEquals(401, response.getStatus());
398     }
399
400     @Test
401     public void testHttpAuthClientProps() throws Exception {
402         final Properties httpProperties = new Properties();
403
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");
423
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");
438
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");
454
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");
469
470         final List<HttpServletServer> servers =
471                         HttpServletServerFactoryInstance.getServerFactory().build(httpProperties);
472         assertEquals(2, servers.size());
473
474         final List<HttpClient> clients = HttpClientFactoryInstance.getClientFactory().build(httpProperties);
475         assertEquals(2, clients.size());
476
477         for (final HttpServletServer server : servers) {
478             server.waitedStart(10000);
479         }
480
481         Response response;
482         final HttpClient clientPap = HttpClientFactoryInstance.getClientFactory().get("PAP");
483         response = clientPap.get();
484         assertEquals(200, response.getStatus());
485
486         final HttpClient clientPdp = HttpClientFactoryInstance.getClientFactory().get("PDP");
487         response = clientPdp.get("test");
488         assertEquals(500, response.getStatus());
489
490         assertFalse(MyJacksonProvider.hasWrittenSome());
491         assertFalse(MyGsonProvider.hasWrittenSome());
492
493         // try with empty path
494         response = clientPap.get("");
495         assertEquals(200, response.getStatus());
496
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());
502
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());
508     }
509
510     @Test
511     public void testHttpAuthClientProps_MixedProviders() throws Exception {
512         final Properties httpProperties = new Properties();
513
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());
529
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());
544
545         final List<HttpClient> clients = HttpClientFactoryInstance.getClientFactory().build(httpProperties);
546         assertEquals(2, clients.size());
547
548         Entity<MyEntity> entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON);
549
550         // use gson client
551         MyGsonProvider.resetSome();
552         MyJacksonProvider.resetSome();
553         HttpClient client = HttpClientFactoryInstance.getClientFactory().get("GSON");
554
555         Response response = client.put(HELLO, entity, Collections.emptyMap());
556         String body = HttpClient.getBody(response, String.class);
557
558         assertEquals(200, response.getStatus());
559         assertEquals(PUT_HELLO, body);
560
561         assertTrue(MyGsonProvider.hasWrittenSome());
562         assertFalse(MyJacksonProvider.hasWrittenSome());
563
564         // use jackson client
565         MyGsonProvider.resetSome();
566         MyJacksonProvider.resetSome();
567         client = HttpClientFactoryInstance.getClientFactory().get("JACKSON");
568
569         response = client.put(HELLO, entity, Collections.emptyMap());
570         body = HttpClient.getBody(response, String.class);
571
572         assertEquals(200, response.getStatus());
573         assertEquals(PUT_HELLO, body);
574
575         assertTrue(MyJacksonProvider.hasWrittenSome());
576         assertFalse(MyGsonProvider.hasWrittenSome());
577     }
578
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());
584     }
585
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());
592     }
593
594
595     class MyEntity {
596
597         private String myParameter;
598
599         public MyEntity(final String myParameter) {
600             this.myParameter = myParameter;
601         }
602
603         public void setMyParameter(final String myParameter) {
604             this.myParameter = myParameter;
605         }
606
607         public String getMyParameter() {
608             return myParameter;
609         }
610
611     }
612
613     class MyCallback implements InvocationCallback<Response> {
614         @Getter
615         private Response response;
616
617         @Getter
618         private Throwable throwable;
619
620         private CountDownLatch latch = new CountDownLatch(1);
621
622         @Override
623         public void completed(Response response) {
624             this.response = response;
625             latch.countDown();
626         }
627
628         @Override
629         public void failed(Throwable throwable) {
630             this.throwable = throwable;
631             latch.countDown();
632         }
633
634         public boolean await() throws InterruptedException {
635             return latch.await(5, TimeUnit.SECONDS);
636         }
637     }
638 }