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.HttpRequestBase;
32 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
33 import org.apache.http.entity.StringEntity;
34 import org.apache.http.impl.client.CloseableHttpClient;
35 import org.apache.http.impl.client.HttpClientBuilder;
36 import org.apache.http.impl.client.HttpClients;
37 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
38 import org.easymock.EasyMock;
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.ExpectedException;
43 import org.junit.runner.RunWith;
44 import org.onap.holmes.common.exception.CorrelationException;
45 import org.powermock.api.easymock.PowerMock;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
48 import org.powermock.reflect.Whitebox;
50 @PrepareForTest({CloseableHttpClient.class, HttpClientBuilder.class, HttpClients.class, CloseableHttpResponse.class,
52 @RunWith(PowerMockRunner.class)
53 public class HttpsUtilsTest {
56 public ExpectedException thrown = ExpectedException.none();
57 private HttpsUtils httpsUtils;
61 httpsUtils = new HttpsUtils();
66 public void testHttpsUtil_get_excepiton() throws Exception {
67 thrown.expect(CorrelationException.class);
68 thrown.expectMessage("Failed to query data from server through GET method!");
70 Map<String, String> header = new HashMap<>();
71 header.put("accept", "application/json");
72 HttpResponse httpResponse = HttpsUtils.get(url, header);
73 String response = HttpsUtils.extractResponseEntity(httpResponse);
74 assertThat(response, equalTo(""));
78 public void testHttpsUtil_get_normal() throws Exception {
79 HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
80 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
81 PowerMock.mockStatic(HttpClients.class);
82 EasyMock.expect(HttpClients.custom()).andReturn(hcb);
83 EasyMock.expect(hcb.setDefaultRequestConfig(EasyMock.anyObject(RequestConfig.class))).andReturn(hcb);
84 EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
85 EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
86 EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
87 EasyMock.expect(hcb.build()).andReturn(httpClient);
89 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
90 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
91 StatusLine sl = PowerMock.createMock(StatusLine.class);
92 EasyMock.expect(response.getStatusLine()).andReturn(sl);
93 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
94 HttpEntity responseEntity = new StringEntity("Test");
95 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
98 EasyMock.expectLastCall();
100 PowerMock.replayAll();
103 String url = "localhost";
104 Map<String, String> header = new HashMap<>();
105 header.put("accept", "application/json");
107 HttpEntity entity = new StringEntity("Test");
108 HttpResponse httpResponse = HttpsUtils.get(url, header);
109 String res = HttpsUtils.extractResponseEntity(httpResponse);
111 PowerMock.verifyAll();
113 assertThat(res, equalTo("Test"));
117 public void testHttpsUtil_delete_excepiton() throws Exception {
118 thrown.expect(CorrelationException.class);
119 thrown.expectMessage("Failed to query data from server through DELETE method!");
121 Map<String, String> header = new HashMap<>();
122 header.put("accept", "application/json");
123 HttpResponse httpResponse = HttpsUtils.delete(url, header);
124 String response = HttpsUtils.extractResponseEntity(httpResponse);
125 assertThat(response, equalTo(""));
129 public void testHttpsUtil_delete_normal() throws Exception {
130 HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
131 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
132 PowerMock.mockStatic(HttpClients.class);
133 EasyMock.expect(HttpClients.custom()).andReturn(hcb);
134 EasyMock.expect(hcb.setDefaultRequestConfig(EasyMock.anyObject(RequestConfig.class))).andReturn(hcb);
135 EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
136 EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
137 EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
138 EasyMock.expect(hcb.build()).andReturn(httpClient);
140 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
141 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
142 StatusLine sl = PowerMock.createMock(StatusLine.class);
143 EasyMock.expect(response.getStatusLine()).andReturn(sl);
144 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
145 HttpEntity responseEntity = new StringEntity("Test");
146 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
149 EasyMock.expectLastCall();
151 PowerMock.replayAll();
154 String url = "localhost";
155 Map<String, String> header = new HashMap<>();
156 header.put("accept", "application/json");
158 HttpEntity entity = new StringEntity("Test");
159 HttpResponse httpResponse = HttpsUtils.delete(url, header);
160 String res = HttpsUtils.extractResponseEntity(httpResponse);
162 PowerMock.verifyAll();
164 assertThat(res, equalTo("Test"));
168 public void testHttpsUtil_post_excepiton() throws Exception {
169 thrown.expect(CorrelationException.class);
170 thrown.expectMessage("Failed to query data from server through POST method!");
172 Map<String, String> header = new HashMap<>();
173 header.put("accept", "application/json");
174 Map<String, String> para = new HashMap<>();
175 para.put("tset", "1111");
177 HttpResponse httpResponse = HttpsUtils.post(url, header, para, null);
178 String response = HttpsUtils.extractResponseEntity(httpResponse);
179 assertThat(response, equalTo(""));
183 public void testHttpsUtil_post_normal() throws Exception {
184 HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
185 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
186 PowerMock.mockStatic(HttpClients.class);
187 EasyMock.expect(HttpClients.custom()).andReturn(hcb);
188 EasyMock.expect(hcb.setDefaultRequestConfig(EasyMock.anyObject(RequestConfig.class))).andReturn(hcb);
189 EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
190 EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
191 EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
192 EasyMock.expect(hcb.build()).andReturn(httpClient);
194 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
195 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
196 StatusLine sl = PowerMock.createMock(StatusLine.class);
197 EasyMock.expect(response.getStatusLine()).andReturn(sl);
198 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
199 HttpEntity responseEntity = new StringEntity("Test");
200 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
203 EasyMock.expectLastCall();
205 PowerMock.replayAll();
208 String url = "localhost";
209 Map<String, String> header = new HashMap<>();
210 header.put("accept", "application/json");
211 Map<String, String> para = new HashMap<>();
212 para.put("tset", "1111");
214 HttpEntity entity = new StringEntity("Test");
215 HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity);
216 String res = HttpsUtils.extractResponseEntity(httpResponse);
218 PowerMock.verifyAll();
220 assertThat(res, equalTo("Test"));
224 public void testHttpsUtil_put_excepiton() throws Exception {
225 thrown.expect(CorrelationException.class);
226 thrown.expectMessage("Failed to query data from server through PUT method!");
228 Map<String, String> header = new HashMap<>();
229 header.put("accept", "application/json");
230 Map<String, String> para = new HashMap<>();
231 para.put("tset", "1111");
233 HttpResponse httpResponse = HttpsUtils.put(url, header, para, null);
234 String response = HttpsUtils.extractResponseEntity(httpResponse);
235 assertThat(response, equalTo(""));
239 public void testHttpsUtil_put_normal() throws Exception {
240 HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
241 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
242 PowerMock.mockStatic(HttpClients.class);
243 EasyMock.expect(HttpClients.custom()).andReturn(hcb);
244 EasyMock.expect(hcb.setDefaultRequestConfig(EasyMock.anyObject(RequestConfig.class))).andReturn(hcb);
245 EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
246 EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
247 EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
248 EasyMock.expect(hcb.build()).andReturn(httpClient);
250 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
251 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
252 StatusLine sl = PowerMock.createMock(StatusLine.class);
253 EasyMock.expect(response.getStatusLine()).andReturn(sl);
254 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
255 HttpEntity responseEntity = new StringEntity("Test");
256 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
259 EasyMock.expectLastCall();
261 PowerMock.replayAll();
264 String url = "localhost";
265 Map<String, String> header = new HashMap<>();
266 header.put("accept", "application/json");
267 Map<String, String> para = new HashMap<>();
268 para.put("tset", "1111");
270 HttpEntity entity = new StringEntity("Test");
271 HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity);
272 String res = HttpsUtils.extractResponseEntity(httpResponse);
274 PowerMock.verifyAll();
276 assertThat(res, equalTo("Test"));
280 public void testHttpsUtil_getResponseEntity_input_null() throws Exception {
281 PowerMock.resetAll();
282 httpsUtils = PowerMock.createMock(HttpsUtils.class);
283 PowerMock.replayAll();
284 String actual = Whitebox.invokeMethod(httpsUtils, "extractResponseEntity", null);
285 PowerMock.verifyAll();
286 assertThat(actual, equalTo(""));
291 public void testHttpsUtil_getHttpClient_exception() throws Exception {
292 PowerMock.resetAll();
293 thrown.expect(Exception.class);
294 Whitebox.invokeMethod(HttpsUtils.class, "getHttpClient");
295 PowerMock.verifyAll();