2 * Copyright 2017 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.holmes.common.utils;
19 import static org.hamcrest.CoreMatchers.equalTo;
20 import static org.hamcrest.MatcherAssert.assertThat;
22 import java.util.HashMap;
25 import org.apache.http.HttpEntity;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.HttpStatus;
28 import org.apache.http.StatusLine;
29 import org.apache.http.client.config.RequestConfig;
30 import org.apache.http.client.methods.CloseableHttpResponse;
31 import org.apache.http.client.methods.HttpDelete;
32 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
33 import org.apache.http.client.methods.HttpGet;
34 import org.apache.http.client.methods.HttpPost;
35 import org.apache.http.client.methods.HttpPut;
36 import org.apache.http.client.methods.HttpRequestBase;
37 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
38 import org.apache.http.entity.StringEntity;
39 import org.apache.http.impl.client.CloseableHttpClient;
40 import org.apache.http.impl.client.HttpClientBuilder;
41 import org.apache.http.impl.client.HttpClients;
42 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
43 import org.easymock.EasyMock;
44 import org.junit.Before;
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.junit.rules.ExpectedException;
48 import org.junit.runner.RunWith;
49 import org.onap.holmes.common.exception.CorrelationException;
50 import org.powermock.api.easymock.PowerMock;
51 import org.powermock.core.classloader.annotations.PowerMockIgnore;
52 import org.powermock.core.classloader.annotations.PrepareForTest;
53 import org.powermock.modules.junit4.PowerMockRunner;
54 import org.powermock.reflect.Whitebox;
56 @PrepareForTest({CloseableHttpClient.class, HttpClientBuilder.class, HttpClients.class, CloseableHttpResponse.class,
58 @RunWith(PowerMockRunner.class)
59 @PowerMockIgnore("javax.net.ssl.*")
60 public class HttpsUtilsTest {
63 public ExpectedException thrown = ExpectedException.none();
64 private HttpsUtils httpsUtils;
68 httpsUtils = new HttpsUtils();
73 public void testHttpsUtil_get_excepiton() throws Exception {
75 thrown.expect(CorrelationException.class);
76 thrown.expectMessage("Failed to connect to server");
78 Map<String, String> header = new HashMap<>();
79 header.put("accept", "application/json");
80 CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
81 HttpGet httpRequestBase = new HttpGet(url);
82 HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient);
83 String response = HttpsUtils.extractResponseEntity(httpResponse);
84 assertThat(response, equalTo(""));
88 public void testHttpsUtil_get_normal() throws Exception {
90 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
91 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
92 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
93 StatusLine sl = PowerMock.createMock(StatusLine.class);
94 EasyMock.expect(response.getStatusLine()).andReturn(sl);
95 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
96 HttpEntity responseEntity = new StringEntity("Test");
97 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
99 PowerMock.replayAll();
102 String url = "localhost";
103 Map<String, String> header = new HashMap<>();
104 header.put("accept", "application/json");
106 HttpGet httpRequestBase = new HttpGet(url);
107 HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient);
108 String res = HttpsUtils.extractResponseEntity(httpResponse);
110 PowerMock.verifyAll();
112 assertThat(res, equalTo("Test"));
116 public void testHttpsUtil_delete_excepiton() throws Exception {
117 PowerMock.resetAll();
118 thrown.expect(CorrelationException.class);
119 thrown.expectMessage("Failed to connect to server");
121 Map<String, String> header = new HashMap<>();
122 header.put("accept", "application/json");
123 HttpDelete httpRequestBase = new HttpDelete(url);
124 CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
125 HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, header, httpClient);
126 String response = HttpsUtils.extractResponseEntity(httpResponse);
127 assertThat(response, equalTo(""));
131 public void testHttpsUtil_delete_normal() throws Exception {
132 PowerMock.resetAll();
133 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
134 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
135 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
136 StatusLine sl = PowerMock.createMock(StatusLine.class);
137 EasyMock.expect(response.getStatusLine()).andReturn(sl);
138 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
139 HttpEntity responseEntity = new StringEntity("Test");
140 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
142 PowerMock.replayAll();
145 String url = "localhost";
146 Map<String, String> header = new HashMap<>();
147 header.put("accept", "application/json");
149 HttpDelete httpRequestBase = new HttpDelete(url);
150 HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, header, httpClient);
151 String res = HttpsUtils.extractResponseEntity(httpResponse);
153 PowerMock.verifyAll();
155 assertThat(res, equalTo("Test"));
159 public void testHttpsUtil_post_excepiton() throws Exception {
160 PowerMock.resetAll();
161 thrown.expect(CorrelationException.class);
162 thrown.expectMessage("Failed to connect to server");
164 Map<String, String> header = new HashMap<>();
165 header.put("accept", "application/json");
166 Map<String, String> para = new HashMap<>();
167 para.put("tset", "1111");
168 CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
169 HttpPost httpPost = new HttpPost(url);
170 HttpResponse httpResponse = HttpsUtils.post(httpPost, header, para, null, httpClient);
171 String response = HttpsUtils.extractResponseEntity(httpResponse);
172 assertThat(response, equalTo(""));
176 public void testHttpsUtil_post_normal() throws Exception {
177 PowerMock.resetAll();
178 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
179 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
180 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
181 StatusLine sl = PowerMock.createMock(StatusLine.class);
182 EasyMock.expect(response.getStatusLine()).andReturn(sl);
183 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
184 HttpEntity responseEntity = new StringEntity("Test");
185 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
187 PowerMock.replayAll();
190 String url = "localhost";
191 Map<String, String> header = new HashMap<>();
192 header.put("accept", "application/json");
193 Map<String, String> para = new HashMap<>();
194 para.put("tset", "1111");
196 HttpEntity entity = new StringEntity("Test");
197 HttpPost httpPost = new HttpPost(url);
198 HttpResponse httpResponse = HttpsUtils.post(httpPost, header, para, entity, httpClient);
199 String res = HttpsUtils.extractResponseEntity(httpResponse);
201 PowerMock.verifyAll();
203 assertThat(res, equalTo("Test"));
207 public void testHttpsUtil_put_excepiton() throws Exception {
208 thrown.expect(CorrelationException.class);
209 thrown.expectMessage("Failed to connect to server");
211 Map<String, String> header = new HashMap<>();
212 header.put("accept", "application/json");
213 Map<String, String> para = new HashMap<>();
214 para.put("tset", "1111");
215 CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
216 HttpPut httpPut = new HttpPut(url);
217 HttpResponse httpResponse = HttpsUtils.put(httpPut, header, para, null, httpClient);
218 String response = HttpsUtils.extractResponseEntity(httpResponse);
219 assertThat(response, equalTo(""));
223 public void testHttpsUtil_put_normal() throws Exception {
224 PowerMock.resetAll();
225 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
226 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
227 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
228 StatusLine sl = PowerMock.createMock(StatusLine.class);
229 EasyMock.expect(response.getStatusLine()).andReturn(sl);
230 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
231 HttpEntity responseEntity = new StringEntity("Test");
232 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
234 PowerMock.replayAll();
237 String url = "localhost";
238 Map<String, String> header = new HashMap<>();
239 header.put("accept", "application/json");
240 Map<String, String> para = new HashMap<>();
241 para.put("tset", "1111");
243 HttpEntity entity = new StringEntity("Test");
244 HttpPut httpPut = new HttpPut(url);
245 HttpResponse httpResponse = HttpsUtils.put(httpPut, header, para, entity, httpClient);
246 String res = HttpsUtils.extractResponseEntity(httpResponse);
248 PowerMock.verifyAll();
250 assertThat(res, equalTo("Test"));
254 public void testHttpsUtil_getResponseEntity_input_null() throws Exception {
255 PowerMock.resetAll();
256 httpsUtils = PowerMock.createMock(HttpsUtils.class);
257 PowerMock.replayAll();
258 String actual = Whitebox.invokeMethod(httpsUtils, "extractResponseEntity", null);
259 PowerMock.verifyAll();
260 assertThat(actual, equalTo(""));
265 public void testHttpsUtil_getHttpClient_exception() throws Exception {
266 PowerMock.resetAll();
267 thrown.expect(Exception.class);
268 Whitebox.invokeMethod(HttpsUtils.class, "getHttpClient");
269 PowerMock.verifyAll();
273 public void testHttpsUtil_getHttpClient_ok() throws Exception {
274 PowerMock.resetAll();
275 HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
276 PowerMock.verifyAll();