2 * ===============================LICENSE_START======================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============================LICENSE_END===========================================
21 package org.openecomp.dcae.apod.analytics.aai.service;
23 import org.apache.http.HttpHost;
24 import org.apache.http.config.ConnectionConfig;
25 import org.apache.http.config.Registry;
26 import org.apache.http.conn.ssl.DefaultHostnameVerifier;
27 import org.apache.http.conn.ssl.NoopHostnameVerifier;
28 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
29 import org.apache.http.impl.client.CloseableHttpClient;
30 import org.apache.http.impl.conn.DefaultHttpClientConnectionOperator;
31 import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
32 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
33 import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
34 import org.junit.Assert;
35 import org.junit.Test;
36 import org.openecomp.dcae.apod.analytics.aai.BaseAnalyticsAAIUnitTest;
37 import org.openecomp.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig;
38 import sun.security.ssl.SSLContextImpl;
39 import sun.security.ssl.SSLSocketFactoryImpl;
41 import javax.net.ssl.HostnameVerifier;
43 import static org.hamcrest.CoreMatchers.is;
44 import static org.junit.Assert.assertThat;
45 import static org.junit.Assert.assertTrue;
48 * @author Rajiv Singla . Creation Date: 9/25/2017.
50 public class AAIHttpClientImplTest extends BaseAnalyticsAAIUnitTest {
53 public void getAAIHttpClientWithIgnoredSSLErrorsAndProxySettings() throws Exception {
55 final AAIHttpClientConfig aaiHttpClientTestConfig =
56 getAAIHttpClientTestConfig(true, PROXY_URL);
57 final AAIHttpClientImpl aaiHttpClient = new AAIHttpClientImpl(aaiHttpClientTestConfig);
58 final CloseableHttpClient closeableHttpClient = aaiHttpClient.getAAIHttpClient();
60 final HostnameVerifier hostnameVerifier = getHostNameVerifier(closeableHttpClient);
62 assertTrue("HostName Verifier must be NoOpHostnameVerifier",
63 NoopHostnameVerifier.INSTANCE.getClass().equals(hostnameVerifier.getClass()));
65 final DefaultProxyRoutePlanner routePlanner = getPrivateFiledValue(closeableHttpClient, "routePlanner",
66 DefaultProxyRoutePlanner.class);
68 final HttpHost proxyHost = getPrivateFiledValue(routePlanner, "proxy", HttpHost.class);
70 final String hostName = proxyHost.getHostName();
71 final int port = proxyHost.getPort();
73 assertThat("Proxy Host name must match", hostName, is(PROXY_HOST));
74 assertThat("Proxy Port number must match", port, is(PROXY_PORT));
79 public void getAAIHttpClientWithNoIgnoredSSLErrorsAndProxySettings() throws Exception {
81 final AAIHttpClientConfig aaiHttpClientTestConfig =
82 getAAIHttpClientTestConfig(false, null);
83 final AAIHttpClientImpl aaiHttpClient = new AAIHttpClientImpl(aaiHttpClientTestConfig);
84 final CloseableHttpClient closeableHttpClient = aaiHttpClient.getAAIHttpClient();
85 final HostnameVerifier hostnameVerifier = getHostNameVerifier(closeableHttpClient);
87 assertTrue("HostName Verifier must be DefaultHostNameVerifier",
88 hostnameVerifier instanceof DefaultHostnameVerifier);
90 getPrivateFiledValue(closeableHttpClient, "routePlanner",
91 SystemDefaultRoutePlanner.class);
95 private HostnameVerifier getHostNameVerifier(final CloseableHttpClient closeableHttpClient) {
96 final PoolingHttpClientConnectionManager connManager =
97 getPrivateFiledValue(closeableHttpClient, "connManager",
98 PoolingHttpClientConnectionManager.class);
99 final DefaultHttpClientConnectionOperator connectionOperator = getPrivateFiledValue(connManager,
100 "connectionOperator", DefaultHttpClientConnectionOperator.class);
101 final Registry socketFactoryRegistry = getPrivateFiledValue(connectionOperator, "socketFactoryRegistry",
103 final SSLConnectionSocketFactory sslConnectionSocketFactory = (SSLConnectionSocketFactory)
104 socketFactoryRegistry.lookup("https");
105 final HostnameVerifier hostnameVerifier = getPrivateFiledValue(sslConnectionSocketFactory,
106 "hostnameVerifier", HostnameVerifier.class);
107 return hostnameVerifier;