Change HTTP Requests into HTTPS Ones
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / utils / HttpsUtilsTest.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.holmes.common.utils;
18
19 import static org.hamcrest.CoreMatchers.equalTo;
20 import static org.hamcrest.MatcherAssert.assertThat;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
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;
49
50 @PrepareForTest({CloseableHttpClient.class, HttpClientBuilder.class, HttpClients.class, CloseableHttpResponse.class,
51         StatusLine.class})
52 @RunWith(PowerMockRunner.class)
53 public class HttpsUtilsTest {
54
55     @Rule
56     public ExpectedException thrown = ExpectedException.none();
57     private HttpsUtils httpsUtils;
58
59     @Before
60     public void setUp() {
61         httpsUtils = new HttpsUtils();
62     }
63
64
65     @Test
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!");
69         String url = "host";
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(""));
75     }
76
77     @Test
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);
88
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);
96
97         httpClient.close();
98         EasyMock.expectLastCall();
99
100         PowerMock.replayAll();
101
102
103         String url = "localhost";
104         Map<String, String> header = new HashMap<>();
105         header.put("accept", "application/json");
106
107         HttpEntity entity = new StringEntity("Test");
108         HttpResponse httpResponse = HttpsUtils.get(url, header);
109         String res = HttpsUtils.extractResponseEntity(httpResponse);
110
111         PowerMock.verifyAll();
112
113         assertThat(res, equalTo("Test"));
114     }
115
116     @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!");
120         String url = "host";
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(""));
126     }
127
128     @Test
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);
139
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);
147
148         httpClient.close();
149         EasyMock.expectLastCall();
150
151         PowerMock.replayAll();
152
153
154         String url = "localhost";
155         Map<String, String> header = new HashMap<>();
156         header.put("accept", "application/json");
157
158         HttpEntity entity = new StringEntity("Test");
159         HttpResponse httpResponse = HttpsUtils.delete(url, header);
160         String res = HttpsUtils.extractResponseEntity(httpResponse);
161
162         PowerMock.verifyAll();
163
164         assertThat(res, equalTo("Test"));
165     }
166
167     @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!");
171         String url = "host";
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");
176
177         HttpResponse httpResponse = HttpsUtils.post(url, header, para, null);
178         String response = HttpsUtils.extractResponseEntity(httpResponse);
179         assertThat(response, equalTo(""));
180     }
181
182     @Test
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);
193
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);
201
202         httpClient.close();
203         EasyMock.expectLastCall();
204
205         PowerMock.replayAll();
206
207
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");
213
214         HttpEntity entity = new StringEntity("Test");
215         HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity);
216         String res = HttpsUtils.extractResponseEntity(httpResponse);
217
218         PowerMock.verifyAll();
219
220         assertThat(res, equalTo("Test"));
221     }
222
223     @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!");
227         String url = "host";
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");
232
233         HttpResponse httpResponse = HttpsUtils.put(url, header, para, null);
234         String response = HttpsUtils.extractResponseEntity(httpResponse);
235         assertThat(response, equalTo(""));
236     }
237
238     @Test
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);
249
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);
257
258         httpClient.close();
259         EasyMock.expectLastCall();
260
261         PowerMock.replayAll();
262
263
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");
269
270         HttpEntity entity = new StringEntity("Test");
271         HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity);
272         String res = HttpsUtils.extractResponseEntity(httpResponse);
273
274         PowerMock.verifyAll();
275
276         assertThat(res, equalTo("Test"));
277     }
278
279     @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(""));
287     }
288
289
290     @Test
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();
296     }
297
298 }