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         PowerMock.replayAll();
94
95
96         String url = "localhost";
97         Map<String, String> header = new HashMap<>();
98         header.put("accept", "application/json");
99
100         HttpEntity entity = new StringEntity("Test");
101         HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient);
102         String res = HttpsUtils.extractResponseEntity(httpResponse);
103
104         PowerMock.verifyAll();
105
106         assertThat(res, equalTo("Test"));
107     }
108
109     @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!");
114         String url = "host";
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(""));
121     }
122
123     @Test
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);
134
135         PowerMock.replayAll();
136
137
138         String url = "localhost";
139         Map<String, String> header = new HashMap<>();
140         header.put("accept", "application/json");
141
142         HttpEntity entity = new StringEntity("Test");
143         HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
144         String res = HttpsUtils.extractResponseEntity(httpResponse);
145
146         PowerMock.verifyAll();
147
148         assertThat(res, equalTo("Test"));
149     }
150
151     @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!");
156         String url = "host";
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(""));
165     }
166
167     @Test
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);
178
179         PowerMock.replayAll();
180
181
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");
187
188         HttpEntity entity = new StringEntity("Test");
189         HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity, httpClient);
190         String res = HttpsUtils.extractResponseEntity(httpResponse);
191
192         PowerMock.verifyAll();
193
194         assertThat(res, equalTo("Test"));
195     }
196
197     @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!");
201         String url = "host";
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(""));
210     }
211
212     @Test
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);
223
224         PowerMock.replayAll();
225
226
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");
232
233         HttpEntity entity = new StringEntity("Test");
234         HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity, httpClient);
235         String res = HttpsUtils.extractResponseEntity(httpResponse);
236
237         PowerMock.verifyAll();
238
239         assertThat(res, equalTo("Test"));
240     }
241
242     @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(""));
250     }
251
252
253     @Test
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();
259     }
260
261     @Test
262     public void testHttpsUtil_getHttpClient_ok() throws Exception {
263         PowerMock.resetAll();
264         HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
265         PowerMock.verifyAll();
266     }
267
268 }