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.HttpClient;
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 use get method query data from server");
70 Map<String, String> header = new HashMap<>();
71 header.put("accept", "application/json");
72 String response = HttpsUtils.get(url, header);
73 assertThat(response, equalTo(""));
77 public void testHttpsUtil_get_normal() throws Exception {
78 HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
79 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
80 PowerMock.mockStatic(HttpClients.class);
81 EasyMock.expect(HttpClients.custom()).andReturn(hcb);
82 EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
83 EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
84 EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
85 EasyMock.expect(hcb.build()).andReturn(httpClient);
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);
96 EasyMock.expectLastCall();
98 PowerMock.replayAll();
101 String url = "localhost";
102 Map<String, String> header = new HashMap<>();
103 header.put("accept", "application/json");
105 HttpEntity entity = new StringEntity("Test");
106 String res = HttpsUtils.get(url, header);
108 PowerMock.verifyAll();
110 assertThat(res, equalTo("Test"));
114 public void testHttpsUtil_post_excepiton() throws Exception {
115 thrown.expect(CorrelationException.class);
116 thrown.expectMessage("Failed to use post method query data from server");
118 Map<String, String> header = new HashMap<>();
119 header.put("accept", "application/json");
120 Map<String, String> para = new HashMap<>();
121 para.put("tset", "1111");
122 String response = HttpsUtils.post(url, header, para, null);
123 assertThat(response, equalTo(""));
127 public void testHttpsUtil_post_normal() throws Exception {
128 HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
129 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
130 PowerMock.mockStatic(HttpClients.class);
131 EasyMock.expect(HttpClients.custom()).andReturn(hcb);
132 EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
133 EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
134 EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
135 EasyMock.expect(hcb.build()).andReturn(httpClient);
137 CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
138 EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
139 StatusLine sl = PowerMock.createMock(StatusLine.class);
140 EasyMock.expect(response.getStatusLine()).andReturn(sl);
141 EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
142 HttpEntity responseEntity = new StringEntity("Test");
143 EasyMock.expect(response.getEntity()).andReturn(responseEntity);
145 PowerMock.replayAll();
148 String url = "localhost";
149 Map<String, String> header = new HashMap<>();
150 header.put("accept", "application/json");
151 Map<String, String> para = new HashMap<>();
152 para.put("tset", "1111");
154 HttpEntity entity = new StringEntity("Test");
155 String res = HttpsUtils.post(url, header, para, entity);
157 PowerMock.verifyAll();
159 assertThat(res, equalTo("Test"));
163 public void testHttpsUtil_getResponseEntity_input_null() throws Exception {
164 PowerMock.resetAll();
165 httpsUtils = PowerMock.createMock(HttpsUtils.class);
166 PowerMock.replayAll();
167 String actual = Whitebox.invokeMethod(httpsUtils, "getResponseEntity", null);
168 PowerMock.verifyAll();
169 assertThat(actual, equalTo(""));
174 public void testHttpsUtil_getHttpClient_exception() throws Exception {
175 PowerMock.resetAll();
176 thrown.expect(Exception.class);
177 Whitebox.invokeMethod(HttpsUtils.class, "getHttpClient");
178 PowerMock.verifyAll();