Add Unit Tests
[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.HttpClient;
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 use get method query data from server");
69         String url = "host";
70         Map<String, String> header = new HashMap<>();
71         header.put("accept", "application/json");
72         String response = HttpsUtils.get(url, header);
73         assertThat(response, equalTo(""));
74     }
75
76     @Test
77     public void testHttpsUtil_get_normal() throws Exception {
78         HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
79         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
80         PowerMock.mockStatic(HttpClients.class);
81         EasyMock.expect(HttpClients.custom()).andReturn(hcb);
82         EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
83         EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
84         EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
85         EasyMock.expect(hcb.build()).andReturn(httpClient);
86
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         httpClient.close();
96         EasyMock.expectLastCall();
97
98         PowerMock.replayAll();
99
100
101         String url = "localhost";
102         Map<String, String> header = new HashMap<>();
103         header.put("accept", "application/json");
104
105         HttpEntity entity = new StringEntity("Test");
106         String res = HttpsUtils.get(url, header);
107
108         PowerMock.verifyAll();
109
110         assertThat(res, equalTo("Test"));
111     }
112
113     @Test
114     public void testHttpsUtil_post_excepiton() throws Exception {
115         thrown.expect(CorrelationException.class);
116         thrown.expectMessage("Failed to use post method query data from server");
117         String url = "host";
118         Map<String, String> header = new HashMap<>();
119         header.put("accept", "application/json");
120         Map<String, String> para = new HashMap<>();
121         para.put("tset", "1111");
122         String response = HttpsUtils.post(url, header, para, null);
123         assertThat(response, equalTo(""));
124     }
125
126     @Test
127     public void testHttpsUtil_post_normal() throws Exception {
128         HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
129         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
130         PowerMock.mockStatic(HttpClients.class);
131         EasyMock.expect(HttpClients.custom()).andReturn(hcb);
132         EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
133         EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
134         EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
135         EasyMock.expect(hcb.build()).andReturn(httpClient);
136
137         CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
138         EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
139         StatusLine sl = PowerMock.createMock(StatusLine.class);
140         EasyMock.expect(response.getStatusLine()).andReturn(sl);
141         EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
142         HttpEntity responseEntity = new StringEntity("Test");
143         EasyMock.expect(response.getEntity()).andReturn(responseEntity);
144
145         PowerMock.replayAll();
146
147
148         String url = "localhost";
149         Map<String, String> header = new HashMap<>();
150         header.put("accept", "application/json");
151         Map<String, String> para = new HashMap<>();
152         para.put("tset", "1111");
153
154         HttpEntity entity = new StringEntity("Test");
155         String res = HttpsUtils.post(url, header, para, entity);
156
157         PowerMock.verifyAll();
158
159         assertThat(res, equalTo("Test"));
160     }
161
162     @Test
163     public void testHttpsUtil_getResponseEntity_input_null() throws Exception {
164         PowerMock.resetAll();
165         httpsUtils = PowerMock.createMock(HttpsUtils.class);
166         PowerMock.replayAll();
167         String actual = Whitebox.invokeMethod(httpsUtils, "getResponseEntity", null);
168         PowerMock.verifyAll();
169         assertThat(actual, equalTo(""));
170     }
171
172
173     @Test
174     public void testHttpsUtil_getHttpClient_exception() throws Exception {
175         PowerMock.resetAll();
176         thrown.expect(Exception.class);
177         Whitebox.invokeMethod(HttpsUtils.class, "getHttpClient");
178         PowerMock.verifyAll();
179     }
180
181 }