TCA: Support for VES/A&AI enrichment
[dcaegen2/analytics/tca.git] / dcae-analytics-aai / src / test / java / org / openecomp / dcae / apod / analytics / aai / service / AAIHttpClientImplTest.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
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
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
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===========================================
19  */
20
21 package org.openecomp.dcae.apod.analytics.aai.service;
22
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;
40
41 import javax.net.ssl.HostnameVerifier;
42
43 import static org.hamcrest.CoreMatchers.is;
44 import static org.junit.Assert.assertThat;
45 import static org.junit.Assert.assertTrue;
46
47 /**
48  * @author Rajiv Singla . Creation Date: 9/25/2017.
49  */
50 public class AAIHttpClientImplTest extends BaseAnalyticsAAIUnitTest {
51
52     @Test
53     public void getAAIHttpClientWithIgnoredSSLErrorsAndProxySettings() throws Exception {
54
55         final AAIHttpClientConfig aaiHttpClientTestConfig =
56                 getAAIHttpClientTestConfig(true, PROXY_URL);
57         final AAIHttpClientImpl aaiHttpClient = new AAIHttpClientImpl(aaiHttpClientTestConfig);
58         final CloseableHttpClient closeableHttpClient = aaiHttpClient.getAAIHttpClient();
59
60         final HostnameVerifier hostnameVerifier = getHostNameVerifier(closeableHttpClient);
61
62         assertTrue("HostName Verifier must be NoOpHostnameVerifier",
63                 NoopHostnameVerifier.INSTANCE.getClass().equals(hostnameVerifier.getClass()));
64
65         final DefaultProxyRoutePlanner routePlanner = getPrivateFiledValue(closeableHttpClient, "routePlanner",
66                 DefaultProxyRoutePlanner.class);
67
68         final HttpHost proxyHost = getPrivateFiledValue(routePlanner, "proxy", HttpHost.class);
69
70         final String hostName = proxyHost.getHostName();
71         final int port = proxyHost.getPort();
72
73         assertThat("Proxy Host name must match", hostName, is(PROXY_HOST));
74         assertThat("Proxy Port number must match", port, is(PROXY_PORT));
75
76     }
77
78     @Test
79     public void getAAIHttpClientWithNoIgnoredSSLErrorsAndProxySettings() throws Exception {
80
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);
86
87         assertTrue("HostName Verifier must be DefaultHostNameVerifier",
88                 hostnameVerifier instanceof DefaultHostnameVerifier);
89
90         getPrivateFiledValue(closeableHttpClient, "routePlanner",
91                 SystemDefaultRoutePlanner.class);
92     }
93
94
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",
102                 Registry.class);
103         final SSLConnectionSocketFactory sslConnectionSocketFactory = (SSLConnectionSocketFactory)
104                 socketFactoryRegistry.lookup("https");
105         final HostnameVerifier hostnameVerifier = getPrivateFiledValue(sslConnectionSocketFactory,
106                 "hostnameVerifier", HostnameVerifier.class);
107         return hostnameVerifier;
108     }
109 }