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);
93 PowerMock.replayAll();
96 String url = "localhost";
97 Map<String, String> header = new HashMap<>();
98 header.put("accept", "application/json");
100 HttpEntity entity = new StringEntity("Test");
101 HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient);
102 String res = HttpsUtils.extractResponseEntity(httpResponse);
104 PowerMock.verifyAll();
106 assertThat(res, equalTo("Test"));
110 public void testHttpsUtil_delete_excepiton() throws Exception {
111 PowerMock.resetAll();
112 thrown.expect(CorrelationException.class);
113 thrown.expectMessage("Failed to query data from server through DELETE method!");
115 Map<String, String> header = new HashMap<>();
116 header.put("accept", "application/json");
117 CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
118 HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
119 String response = HttpsUtils.extractResponseEntity(httpResponse);
120 assertThat(response, equalTo(""));
124 public void testHttpsUtil_delete_normal() throws Exception {
125 PowerMock.resetAll();
126 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
127 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
128 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
129 StatusLine sl = PowerMock.createMock(StatusLine.class);
130 EasyMock.expect(response.getStatusLine()).andReturn(sl);
131 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
132 HttpEntity responseEntity = new StringEntity("Test");
133 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
135 PowerMock.replayAll();
138 String url = "localhost";
139 Map<String, String> header = new HashMap<>();
140 header.put("accept", "application/json");
142 HttpEntity entity = new StringEntity("Test");
143 HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
144 String res = HttpsUtils.extractResponseEntity(httpResponse);
146 PowerMock.verifyAll();
148 assertThat(res, equalTo("Test"));
152 public void testHttpsUtil_post_excepiton() throws Exception {
153 PowerMock.resetAll();
154 thrown.expect(CorrelationException.class);
155 thrown.expectMessage("Failed to query data from server through POST method!");
157 Map<String, String> header = new HashMap<>();
158 header.put("accept", "application/json");
159 Map<String, String> para = new HashMap<>();
160 para.put("tset", "1111");
161 CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
162 HttpResponse httpResponse = HttpsUtils.post(url, header, para, null, httpClient);
163 String response = HttpsUtils.extractResponseEntity(httpResponse);
164 assertThat(response, equalTo(""));
168 public void testHttpsUtil_post_normal() throws Exception {
169 PowerMock.resetAll();
170 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
171 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
172 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
173 StatusLine sl = PowerMock.createMock(StatusLine.class);
174 EasyMock.expect(response.getStatusLine()).andReturn(sl);
175 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
176 HttpEntity responseEntity = new StringEntity("Test");
177 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
179 PowerMock.replayAll();
182 String url = "localhost";
183 Map<String, String> header = new HashMap<>();
184 header.put("accept", "application/json");
185 Map<String, String> para = new HashMap<>();
186 para.put("tset", "1111");
188 HttpEntity entity = new StringEntity("Test");
189 HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity, httpClient);
190 String res = HttpsUtils.extractResponseEntity(httpResponse);
192 PowerMock.verifyAll();
194 assertThat(res, equalTo("Test"));
198 public void testHttpsUtil_put_excepiton() throws Exception {
199 thrown.expect(CorrelationException.class);
200 thrown.expectMessage("Failed to query data from server through PUT method!");
202 Map<String, String> header = new HashMap<>();
203 header.put("accept", "application/json");
204 Map<String, String> para = new HashMap<>();
205 para.put("tset", "1111");
206 CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
207 HttpResponse httpResponse = HttpsUtils.put(url, header, para, null, httpClient);
208 String response = HttpsUtils.extractResponseEntity(httpResponse);
209 assertThat(response, equalTo(""));
213 public void testHttpsUtil_put_normal() throws Exception {
214 PowerMock.resetAll();
215 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
216 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
217 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
218 StatusLine sl = PowerMock.createMock(StatusLine.class);
219 EasyMock.expect(response.getStatusLine()).andReturn(sl);
220 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
221 HttpEntity responseEntity = new StringEntity("Test");
222 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
224 PowerMock.replayAll();
227 String url = "localhost";
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 HttpEntity entity = new StringEntity("Test");
234 HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity, httpClient);
235 String res = HttpsUtils.extractResponseEntity(httpResponse);
237 PowerMock.verifyAll();
239 assertThat(res, equalTo("Test"));
243 public void testHttpsUtil_getResponseEntity_input_null() throws Exception {
244 PowerMock.resetAll();
245 httpsUtils = PowerMock.createMock(HttpsUtils.class);
246 PowerMock.replayAll();
247 String actual = Whitebox.invokeMethod(httpsUtils, "extractResponseEntity", null);
248 PowerMock.verifyAll();
249 assertThat(actual, equalTo(""));
254 public void testHttpsUtil_getHttpClient_exception() throws Exception {
255 PowerMock.resetAll();
256 thrown.expect(Exception.class);
257 Whitebox.invokeMethod(HttpsUtils.class, "getHttpClient");
258 PowerMock.verifyAll();
262 public void testHttpsUtil_getHttpClient_ok() throws Exception {
263 PowerMock.resetAll();
264 HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
265 PowerMock.verifyAll();