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.PowerMockIgnore;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
49 import org.powermock.reflect.Whitebox;
51 @PrepareForTest({CloseableHttpClient.class, HttpClientBuilder.class, HttpClients.class, CloseableHttpResponse.class,
53 @RunWith(PowerMockRunner.class)
54 @PowerMockIgnore("javax.net.ssl.*")
55 public class HttpsUtilsTest {
58 public ExpectedException thrown = ExpectedException.none();
59 private HttpsUtils httpsUtils;
63 httpsUtils = new HttpsUtils();
68 public void testHttpsUtil_get_excepiton() throws Exception {
70 thrown.expect(CorrelationException.class);
71 thrown.expectMessage("Failed to query data from server through GET method!");
73 Map<String, String> header = new HashMap<>();
74 header.put("accept", "application/json");
75 CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
76 HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient);
77 String response = HttpsUtils.extractResponseEntity(httpResponse);
78 assertThat(response, equalTo(""));
82 public void testHttpsUtil_get_normal() throws Exception {
84 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
85 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
86 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
87 StatusLine sl = PowerMock.createMock(StatusLine.class);
88 EasyMock.expect(response.getStatusLine()).andReturn(sl);
89 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
90 HttpEntity responseEntity = new StringEntity("Test");
91 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
94 EasyMock.expectLastCall();
96 PowerMock.replayAll();
99 String url = "localhost";
100 Map<String, String> header = new HashMap<>();
101 header.put("accept", "application/json");
103 HttpEntity entity = new StringEntity("Test");
104 HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient);
105 String res = HttpsUtils.extractResponseEntity(httpResponse);
107 PowerMock.verifyAll();
109 assertThat(res, equalTo("Test"));
113 public void testHttpsUtil_delete_excepiton() throws Exception {
114 PowerMock.resetAll();
115 thrown.expect(CorrelationException.class);
116 thrown.expectMessage("Failed to query data from server through DELETE method!");
118 Map<String, String> header = new HashMap<>();
119 header.put("accept", "application/json");
120 CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
121 HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
122 String response = HttpsUtils.extractResponseEntity(httpResponse);
123 assertThat(response, equalTo(""));
127 public void testHttpsUtil_delete_normal() throws Exception {
128 PowerMock.resetAll();
129 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
130 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
131 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
132 StatusLine sl = PowerMock.createMock(StatusLine.class);
133 EasyMock.expect(response.getStatusLine()).andReturn(sl);
134 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
135 HttpEntity responseEntity = new StringEntity("Test");
136 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
139 EasyMock.expectLastCall();
141 PowerMock.replayAll();
144 String url = "localhost";
145 Map<String, String> header = new HashMap<>();
146 header.put("accept", "application/json");
148 HttpEntity entity = new StringEntity("Test");
149 HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
150 String res = HttpsUtils.extractResponseEntity(httpResponse);
152 PowerMock.verifyAll();
154 assertThat(res, equalTo("Test"));
158 public void testHttpsUtil_post_excepiton() throws Exception {
159 PowerMock.resetAll();
160 thrown.expect(CorrelationException.class);
161 thrown.expectMessage("Failed to query data from server through POST method!");
163 Map<String, String> header = new HashMap<>();
164 header.put("accept", "application/json");
165 Map<String, String> para = new HashMap<>();
166 para.put("tset", "1111");
167 CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
168 HttpResponse httpResponse = HttpsUtils.post(url, header, para, null, httpClient);
169 String response = HttpsUtils.extractResponseEntity(httpResponse);
170 assertThat(response, equalTo(""));
174 public void testHttpsUtil_post_normal() throws Exception {
175 PowerMock.resetAll();
176 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
177 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
178 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
179 StatusLine sl = PowerMock.createMock(StatusLine.class);
180 EasyMock.expect(response.getStatusLine()).andReturn(sl);
181 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
182 HttpEntity responseEntity = new StringEntity("Test");
183 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
186 EasyMock.expectLastCall();
188 PowerMock.replayAll();
191 String url = "localhost";
192 Map<String, String> header = new HashMap<>();
193 header.put("accept", "application/json");
194 Map<String, String> para = new HashMap<>();
195 para.put("tset", "1111");
197 HttpEntity entity = new StringEntity("Test");
198 HttpResponse httpResponse = HttpsUtils.post(url, 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 query data from server through PUT method!");
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 HttpResponse httpResponse = HttpsUtils.put(url, header, para, null, httpClient);
217 String response = HttpsUtils.extractResponseEntity(httpResponse);
218 assertThat(response, equalTo(""));
222 public void testHttpsUtil_put_normal() throws Exception {
223 PowerMock.resetAll();
224 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
225 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
226 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
227 StatusLine sl = PowerMock.createMock(StatusLine.class);
228 EasyMock.expect(response.getStatusLine()).andReturn(sl);
229 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
230 HttpEntity responseEntity = new StringEntity("Test");
231 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
234 EasyMock.expectLastCall();
236 PowerMock.replayAll();
239 String url = "localhost";
240 Map<String, String> header = new HashMap<>();
241 header.put("accept", "application/json");
242 Map<String, String> para = new HashMap<>();
243 para.put("tset", "1111");
245 HttpEntity entity = new StringEntity("Test");
246 HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity, httpClient);
247 String res = HttpsUtils.extractResponseEntity(httpResponse);
249 PowerMock.verifyAll();
251 assertThat(res, equalTo("Test"));
255 public void testHttpsUtil_getResponseEntity_input_null() throws Exception {
256 PowerMock.resetAll();
257 httpsUtils = PowerMock.createMock(HttpsUtils.class);
258 PowerMock.replayAll();
259 String actual = Whitebox.invokeMethod(httpsUtils, "extractResponseEntity", null);
260 PowerMock.verifyAll();
261 assertThat(actual, equalTo(""));
266 public void testHttpsUtil_getHttpClient_exception() throws Exception {
267 PowerMock.resetAll();
268 thrown.expect(Exception.class);
269 Whitebox.invokeMethod(HttpsUtils.class, "getHttpClient");
270 PowerMock.verifyAll();
274 public void testHttpsUtil_getHttpClient_ok() throws Exception {
275 PowerMock.resetAll();
276 HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
277 PowerMock.verifyAll();