3ff5ddac67a6c43b977193819aa239f40ca4f40d
[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.HttpDelete;
32 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
33 import org.apache.http.client.methods.HttpGet;
34 import org.apache.http.client.methods.HttpPost;
35 import org.apache.http.client.methods.HttpPut;
36 import org.apache.http.client.methods.HttpRequestBase;
37 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
38 import org.apache.http.entity.StringEntity;
39 import org.apache.http.impl.client.CloseableHttpClient;
40 import org.apache.http.impl.client.HttpClientBuilder;
41 import org.apache.http.impl.client.HttpClients;
42 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
43 import org.easymock.EasyMock;
44 import org.junit.Before;
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.junit.rules.ExpectedException;
48 import org.junit.runner.RunWith;
49 import org.onap.holmes.common.exception.CorrelationException;
50 import org.powermock.api.easymock.PowerMock;
51 import org.powermock.core.classloader.annotations.PowerMockIgnore;
52 import org.powermock.core.classloader.annotations.PrepareForTest;
53 import org.powermock.modules.junit4.PowerMockRunner;
54 import org.powermock.reflect.Whitebox;
55
56 @PrepareForTest({CloseableHttpClient.class, HttpClientBuilder.class, HttpClients.class, CloseableHttpResponse.class,
57         StatusLine.class})
58 @RunWith(PowerMockRunner.class)
59 @PowerMockIgnore("javax.net.ssl.*")
60 public class HttpsUtilsTest {
61
62     @Rule
63     public ExpectedException thrown = ExpectedException.none();
64     private HttpsUtils httpsUtils;
65
66     @Before
67     public void setUp() {
68         httpsUtils = new HttpsUtils();
69     }
70
71
72     @Test
73     public void testHttpsUtil_get_excepiton() throws Exception {
74         PowerMock.resetAll();
75         thrown.expect(CorrelationException.class);
76         thrown.expectMessage("Failed to connect to server");
77         String url = "host";
78         Map<String, String> header = new HashMap<>();
79         header.put("accept", "application/json");
80         CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
81         HttpGet httpRequestBase = new HttpGet(url);
82         HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient);
83         String response = HttpsUtils.extractResponseEntity(httpResponse);
84         assertThat(response, equalTo(""));
85     }
86
87     @Test
88     public void testHttpsUtil_get_normal() throws Exception {
89         PowerMock.resetAll();
90         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
91         CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
92         EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
93         StatusLine sl = PowerMock.createMock(StatusLine.class);
94         EasyMock.expect(response.getStatusLine()).andReturn(sl);
95         EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
96         HttpEntity responseEntity = new StringEntity("Test");
97         EasyMock.expect(response.getEntity()).andReturn(responseEntity);
98
99         PowerMock.replayAll();
100
101
102         String url = "localhost";
103         Map<String, String> header = new HashMap<>();
104         header.put("accept", "application/json");
105
106         HttpGet httpRequestBase = new HttpGet(url);
107         HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient);
108         String res = HttpsUtils.extractResponseEntity(httpResponse);
109
110         PowerMock.verifyAll();
111
112         assertThat(res, equalTo("Test"));
113     }
114
115     @Test
116     public void testHttpsUtil_delete_excepiton() throws Exception {
117         PowerMock.resetAll();
118         thrown.expect(CorrelationException.class);
119         thrown.expectMessage("Failed to connect to server");
120         String url = "host";
121         Map<String, String> header = new HashMap<>();
122         header.put("accept", "application/json");
123         HttpDelete httpRequestBase = new HttpDelete(url);
124         CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
125         HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, header, httpClient);
126         String response = HttpsUtils.extractResponseEntity(httpResponse);
127         assertThat(response, equalTo(""));
128     }
129
130     @Test
131     public void testHttpsUtil_delete_normal() throws Exception {
132         PowerMock.resetAll();
133         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
134         CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
135         EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
136         StatusLine sl = PowerMock.createMock(StatusLine.class);
137         EasyMock.expect(response.getStatusLine()).andReturn(sl);
138         EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
139         HttpEntity responseEntity = new StringEntity("Test");
140         EasyMock.expect(response.getEntity()).andReturn(responseEntity);
141
142         PowerMock.replayAll();
143
144
145         String url = "localhost";
146         Map<String, String> header = new HashMap<>();
147         header.put("accept", "application/json");
148
149         HttpDelete httpRequestBase = new HttpDelete(url);
150         HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, header, httpClient);
151         String res = HttpsUtils.extractResponseEntity(httpResponse);
152
153         PowerMock.verifyAll();
154
155         assertThat(res, equalTo("Test"));
156     }
157
158     @Test
159     public void testHttpsUtil_post_excepiton() throws Exception {
160         PowerMock.resetAll();
161         thrown.expect(CorrelationException.class);
162         thrown.expectMessage("Failed to connect to server");
163         String url = "host";
164         Map<String, String> header = new HashMap<>();
165         header.put("accept", "application/json");
166         Map<String, String> para = new HashMap<>();
167         para.put("tset", "1111");
168         CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
169         HttpPost httpPost = new HttpPost(url);
170         HttpResponse httpResponse = HttpsUtils.post(httpPost, header, para, null, httpClient);
171         String response = HttpsUtils.extractResponseEntity(httpResponse);
172         assertThat(response, equalTo(""));
173     }
174
175     @Test
176     public void testHttpsUtil_post_normal() throws Exception {
177         PowerMock.resetAll();
178         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
179         CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
180         EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
181         StatusLine sl = PowerMock.createMock(StatusLine.class);
182         EasyMock.expect(response.getStatusLine()).andReturn(sl);
183         EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
184         HttpEntity responseEntity = new StringEntity("Test");
185         EasyMock.expect(response.getEntity()).andReturn(responseEntity);
186
187         PowerMock.replayAll();
188
189
190         String url = "localhost";
191         Map<String, String> header = new HashMap<>();
192         header.put("accept", "application/json");
193         Map<String, String> para = new HashMap<>();
194         para.put("tset", "1111");
195
196         HttpEntity entity = new StringEntity("Test");
197         HttpPost httpPost = new HttpPost(url);
198         HttpResponse httpResponse = HttpsUtils.post(httpPost, 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 connect to server");
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         HttpPut httpPut = new HttpPut(url);
217         HttpResponse httpResponse = HttpsUtils.put(httpPut, header, para, null, httpClient);
218         String response = HttpsUtils.extractResponseEntity(httpResponse);
219         assertThat(response, equalTo(""));
220     }
221
222     @Test
223     public void testHttpsUtil_put_normal() throws Exception {
224         PowerMock.resetAll();
225         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
226         CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
227         EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
228         StatusLine sl = PowerMock.createMock(StatusLine.class);
229         EasyMock.expect(response.getStatusLine()).andReturn(sl);
230         EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
231         HttpEntity responseEntity = new StringEntity("Test");
232         EasyMock.expect(response.getEntity()).andReturn(responseEntity);
233
234         PowerMock.replayAll();
235
236
237         String url = "localhost";
238         Map<String, String> header = new HashMap<>();
239         header.put("accept", "application/json");
240         Map<String, String> para = new HashMap<>();
241         para.put("tset", "1111");
242
243         HttpEntity entity = new StringEntity("Test");
244         HttpPut httpPut = new HttpPut(url);
245         HttpResponse httpResponse = HttpsUtils.put(httpPut, header, para, entity, httpClient);
246         String res = HttpsUtils.extractResponseEntity(httpResponse);
247
248         PowerMock.verifyAll();
249
250         assertThat(res, equalTo("Test"));
251     }
252
253     @Test
254     public void testHttpsUtil_getResponseEntity_input_null() throws Exception {
255         PowerMock.resetAll();
256         httpsUtils = PowerMock.createMock(HttpsUtils.class);
257         PowerMock.replayAll();
258         String actual = Whitebox.invokeMethod(httpsUtils, "extractResponseEntity", null);
259         PowerMock.verifyAll();
260         assertThat(actual, equalTo(""));
261     }
262
263
264     @Test
265     public void testHttpsUtil_getHttpClient_exception() throws Exception {
266         PowerMock.resetAll();
267         thrown.expect(Exception.class);
268         Whitebox.invokeMethod(HttpsUtils.class, "getHttpClient");
269         PowerMock.verifyAll();
270     }
271
272     @Test
273     public void testHttpsUtil_getHttpClient_ok() throws Exception {
274         PowerMock.resetAll();
275         HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
276         PowerMock.verifyAll();
277     }
278
279 }