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.methods.CloseableHttpResponse;
30 import org.apache.http.client.methods.HttpDelete;
31 import org.apache.http.client.methods.HttpGet;
32 import org.apache.http.client.methods.HttpPost;
33 import org.apache.http.client.methods.HttpPut;
34 import org.apache.http.client.methods.HttpRequestBase;
35 import org.apache.http.entity.StringEntity;
36 import org.apache.http.impl.client.CloseableHttpClient;
37 import org.apache.http.impl.client.HttpClientBuilder;
38 import org.apache.http.impl.client.HttpClients;
39 import org.easymock.EasyMock;
40 import org.junit.Before;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.rules.ExpectedException;
44 import org.junit.runner.RunWith;
45 import org.onap.holmes.common.exception.CorrelationException;
46 import org.powermock.api.easymock.PowerMock;
47 import org.powermock.core.classloader.annotations.PowerMockIgnore;
48 import org.powermock.core.classloader.annotations.PrepareForTest;
49 import org.powermock.modules.junit4.PowerMockRunner;
50 import org.powermock.reflect.Whitebox;
52 @PrepareForTest({CloseableHttpClient.class, HttpClientBuilder.class, HttpClients.class, CloseableHttpResponse.class,
54 @RunWith(PowerMockRunner.class)
55 @PowerMockIgnore("javax.net.ssl.*")
56 public class HttpsUtilsTest {
59 public ExpectedException thrown = ExpectedException.none();
60 private HttpsUtils httpsUtils;
64 httpsUtils = new HttpsUtils();
69 public void testHttpsUtil_get_excepiton() throws Exception {
71 thrown.expect(CorrelationException.class);
72 thrown.expectMessage("Failed to connect to server");
74 Map<String, String> header = new HashMap<>();
75 header.put("accept", "application/json");
76 CloseableHttpClient httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
77 HttpGet httpRequestBase = new HttpGet(url);
78 HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient);
79 String response = HttpsUtils.extractResponseEntity(httpResponse);
80 assertThat(response, equalTo(""));
84 public void testHttpsUtil_get_normal() throws Exception {
86 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
87 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
88 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
89 StatusLine sl = PowerMock.createMock(StatusLine.class);
90 EasyMock.expect(response.getStatusLine()).andReturn(sl);
91 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
92 HttpEntity responseEntity = new StringEntity("Test");
93 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
95 PowerMock.replayAll();
98 String url = "localhost";
99 Map<String, String> header = new HashMap<>();
100 header.put("accept", "application/json");
102 HttpGet httpRequestBase = new HttpGet(url);
103 HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient);
104 String res = HttpsUtils.extractResponseEntity(httpResponse);
106 PowerMock.verifyAll();
108 assertThat(res, equalTo("Test"));
112 public void testHttpsUtil_delete_excepiton() throws Exception {
113 PowerMock.resetAll();
114 thrown.expect(CorrelationException.class);
115 thrown.expectMessage("Failed to connect to server");
117 Map<String, String> header = new HashMap<>();
118 header.put("accept", "application/json");
119 HttpDelete httpRequestBase = new HttpDelete(url);
120 CloseableHttpClient httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
121 HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, 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);
138 PowerMock.replayAll();
141 String url = "localhost";
142 Map<String, String> header = new HashMap<>();
143 header.put("accept", "application/json");
145 HttpDelete httpRequestBase = new HttpDelete(url);
146 HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, header, httpClient);
147 String res = HttpsUtils.extractResponseEntity(httpResponse);
149 PowerMock.verifyAll();
151 assertThat(res, equalTo("Test"));
155 public void testHttpsUtil_post_excepiton() throws Exception {
156 PowerMock.resetAll();
157 thrown.expect(CorrelationException.class);
158 thrown.expectMessage("Failed to connect to server");
160 Map<String, String> header = new HashMap<>();
161 header.put("accept", "application/json");
162 Map<String, String> para = new HashMap<>();
163 para.put("tset", "1111");
164 CloseableHttpClient httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
165 HttpPost httpPost = new HttpPost(url);
166 HttpResponse httpResponse = HttpsUtils.post(httpPost, header, para, null, httpClient);
167 String response = HttpsUtils.extractResponseEntity(httpResponse);
168 assertThat(response, equalTo(""));
172 public void testHttpsUtil_post_normal() throws Exception {
173 PowerMock.resetAll();
174 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
175 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
176 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
177 StatusLine sl = PowerMock.createMock(StatusLine.class);
178 EasyMock.expect(response.getStatusLine()).andReturn(sl);
179 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
180 HttpEntity responseEntity = new StringEntity("Test");
181 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
183 PowerMock.replayAll();
186 String url = "localhost";
187 Map<String, String> header = new HashMap<>();
188 header.put("accept", "application/json");
189 Map<String, String> para = new HashMap<>();
190 para.put("tset", "1111");
192 HttpEntity entity = new StringEntity("Test");
193 HttpPost httpPost = new HttpPost(url);
194 HttpResponse httpResponse = HttpsUtils.post(httpPost, header, para, entity, httpClient);
195 String res = HttpsUtils.extractResponseEntity(httpResponse);
197 PowerMock.verifyAll();
199 assertThat(res, equalTo("Test"));
203 public void testHttpsUtil_put_excepiton() throws Exception {
204 thrown.expect(CorrelationException.class);
205 thrown.expectMessage("Failed to connect to server");
207 Map<String, String> header = new HashMap<>();
208 header.put("accept", "application/json");
209 Map<String, String> para = new HashMap<>();
210 para.put("tset", "1111");
211 CloseableHttpClient httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
212 HttpPut httpPut = new HttpPut(url);
213 HttpResponse httpResponse = HttpsUtils.put(httpPut, header, para, null, httpClient);
214 String response = HttpsUtils.extractResponseEntity(httpResponse);
215 assertThat(response, equalTo(""));
219 public void testHttpsUtil_put_normal() throws Exception {
220 PowerMock.resetAll();
221 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
222 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
223 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
224 StatusLine sl = PowerMock.createMock(StatusLine.class);
225 EasyMock.expect(response.getStatusLine()).andReturn(sl);
226 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
227 HttpEntity responseEntity = new StringEntity("Test");
228 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
230 PowerMock.replayAll();
233 String url = "localhost";
234 Map<String, String> header = new HashMap<>();
235 header.put("accept", "application/json");
236 Map<String, String> para = new HashMap<>();
237 para.put("tset", "1111");
239 HttpEntity entity = new StringEntity("Test");
240 HttpPut httpPut = new HttpPut(url);
241 HttpResponse httpResponse = HttpsUtils.put(httpPut, header, para, entity, httpClient);
242 String res = HttpsUtils.extractResponseEntity(httpResponse);
244 PowerMock.verifyAll();
246 assertThat(res, equalTo("Test"));
250 public void testHttpsUtil_getResponseEntity_input_null() throws Exception {
251 PowerMock.resetAll();
252 httpsUtils = PowerMock.createMock(HttpsUtils.class);
253 PowerMock.replayAll();
254 String actual = Whitebox.invokeMethod(httpsUtils, "extractResponseEntity", null);
255 PowerMock.verifyAll();
256 assertThat(actual, equalTo(""));
261 public void testHttpsUtil_getHttpClient_exception() throws Exception {
262 PowerMock.resetAll();
263 thrown.expect(Exception.class);
264 Whitebox.invokeMethod(HttpsUtils.class, "getConditionalHttpsClient");
265 PowerMock.verifyAll();
269 public void testHttpsUtil_getHttpClient_ok() throws Exception {
270 PowerMock.resetAll();
271 HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
272 PowerMock.verifyAll();