fix https bug
[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.PowerMockIgnore;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
49 import org.powermock.reflect.Whitebox;
50
51 @PrepareForTest({CloseableHttpClient.class, HttpClientBuilder.class, HttpClients.class, CloseableHttpResponse.class,
52         StatusLine.class})
53 @RunWith(PowerMockRunner.class)
54 @PowerMockIgnore("javax.net.ssl.*")
55 public class HttpsUtilsTest {
56
57     @Rule
58     public ExpectedException thrown = ExpectedException.none();
59     private HttpsUtils httpsUtils;
60
61     @Before
62     public void setUp() {
63         httpsUtils = new HttpsUtils();
64     }
65
66
67     @Test
68     public void testHttpsUtil_get_excepiton() throws Exception {
69         PowerMock.resetAll();
70         thrown.expect(CorrelationException.class);
71         thrown.expectMessage("Failed to query data from server through GET method!");
72         String url = "host";
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(""));
79     }
80
81     @Test
82     public void testHttpsUtil_get_normal() throws Exception {
83         PowerMock.resetAll();
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);
92
93         httpClient.close();
94         EasyMock.expectLastCall();
95
96         PowerMock.replayAll();
97
98
99         String url = "localhost";
100         Map<String, String> header = new HashMap<>();
101         header.put("accept", "application/json");
102
103         HttpEntity entity = new StringEntity("Test");
104         HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient);
105         String res = HttpsUtils.extractResponseEntity(httpResponse);
106
107         PowerMock.verifyAll();
108
109         assertThat(res, equalTo("Test"));
110     }
111
112     @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!");
117         String url = "host";
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(""));
124     }
125
126     @Test
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);
137
138         httpClient.close();
139         EasyMock.expectLastCall();
140
141         PowerMock.replayAll();
142
143
144         String url = "localhost";
145         Map<String, String> header = new HashMap<>();
146         header.put("accept", "application/json");
147
148         HttpEntity entity = new StringEntity("Test");
149         HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
150         String res = HttpsUtils.extractResponseEntity(httpResponse);
151
152         PowerMock.verifyAll();
153
154         assertThat(res, equalTo("Test"));
155     }
156
157     @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!");
162         String url = "host";
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(""));
171     }
172
173     @Test
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);
184
185         httpClient.close();
186         EasyMock.expectLastCall();
187
188         PowerMock.replayAll();
189
190
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");
196
197         HttpEntity entity = new StringEntity("Test");
198         HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity, httpClient);
199         String res = HttpsUtils.extractResponseEntity(httpResponse);
200
201         PowerMock.verifyAll();
202
203         assertThat(res, equalTo("Test"));
204     }
205
206     @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!");
210         String url = "host";
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(""));
219     }
220
221     @Test
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);
232
233         httpClient.close();
234         EasyMock.expectLastCall();
235
236         PowerMock.replayAll();
237
238
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");
244
245         HttpEntity entity = new StringEntity("Test");
246         HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity, httpClient);
247         String res = HttpsUtils.extractResponseEntity(httpResponse);
248
249         PowerMock.verifyAll();
250
251         assertThat(res, equalTo("Test"));
252     }
253
254     @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(""));
262     }
263
264
265     @Test
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();
271     }
272
273     @Test
274     public void testHttpsUtil_getHttpClient_ok() throws Exception {
275         PowerMock.resetAll();
276         HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
277         PowerMock.verifyAll();
278     }
279
280 }