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