Fixed Sonar Bug and Code Smell Blockers
[cli.git] / profiles / http / src / test / java / org / onap / cli / fw / http / OnapHttpConnectionTest.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
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.cli.fw.http;
18
19 import static org.junit.Assert.assertEquals;
20
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import mockit.Invocation;
26 import mockit.Mock;
27 import mockit.MockUp;
28
29 import org.apache.http.client.ClientProtocolException;
30 import org.apache.http.client.methods.CloseableHttpResponse;
31 import org.apache.http.client.methods.HttpUriRequest;
32 import org.apache.http.impl.client.CloseableHttpClient;
33 import org.apache.http.protocol.HttpContext;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.cli.fw.http.connect.HttpInput;
37 import org.onap.cli.fw.http.connect.HttpResult;
38 import org.onap.cli.fw.http.connect.OnapHttpConnection;
39 import org.onap.cli.fw.http.error.OnapCommandHttpFailure;
40 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
41
42
43 public class OnapHttpConnectionTest {
44     HttpInput inp = null;
45     OnapHttpConnection con = null;
46
47     @Before
48     public void setup() {
49         mockHttpRequest(null);
50         inp = new HttpInput();
51         inp.setMethod("get");
52         inp.setBody("body");
53         Map<String, String> map1 = new HashMap<>();
54         map1.put("header1", "value1");
55         inp.setReqHeaders(map1);
56         Map<String, String> map2 = new HashMap<>();
57         map2.put("query1", "value1");
58         inp.setReqQueries(map2);
59         Map<String, String> map = new HashMap<>();
60         map.put("cookie1", "value1");
61         inp.setReqCookies(map);
62         inp.setUri("http://192.168.99.10:80");
63     }
64
65     @Test(expected = OnapCommandHttpFailure.class)
66     public void httpUnSecuredGetExceptionTest() throws OnapCommandHttpFailure {
67         new MockUp<CloseableHttpClient>() {
68             @Mock
69             public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
70                     throws IOException, ClientProtocolException {
71
72                 throw new IOException("IO Exception");
73             }
74         };
75         inp.setMethod("get");
76         con = new OnapHttpConnection();
77         con.get(inp);
78
79     }
80
81     @Test(expected = OnapCommandHttpFailure.class)
82     public void httpUnSecuredPostExceptionTest() throws OnapCommandHttpFailure {
83         new MockUp<CloseableHttpClient>() {
84             @Mock
85             public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
86                     throws IOException, ClientProtocolException {
87
88                 throw new IOException("IO Exception");
89             }
90         };
91
92         inp.setMethod("post");
93         con = new OnapHttpConnection();
94         con.post(inp);
95     }
96
97
98     @Test(expected = OnapCommandHttpFailure.class)
99     public void httpUnSecuredPostExceptionTest1() throws OnapCommandHttpFailure {
100         new MockUp<CloseableHttpClient>() {
101             @Mock
102             public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
103                     throws IOException, ClientProtocolException {
104
105                 throw new IOException("IO Exception");
106             }
107         };
108
109         inp.setMethod("post");
110         inp.setBinaryData(true);
111         con = new OnapHttpConnection();
112         con.post(inp);
113     }
114
115     @Test(expected = OnapCommandHttpFailure.class)
116     public void httpUnSecuredPutExceptionTest() throws OnapCommandHttpFailure {
117         new MockUp<CloseableHttpClient>() {
118             @Mock
119             public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
120                     throws IOException, ClientProtocolException {
121
122                 throw new IOException("IO Exception");
123             }
124         };
125         inp.setMethod("put");
126         con = new OnapHttpConnection();
127         con.put(inp);
128     }
129
130     @Test(expected = OnapCommandHttpFailure.class)
131     public void httpUnSecuredDeleteExceptionTest() throws OnapCommandHttpFailure {
132         new MockUp<CloseableHttpClient>() {
133             @Mock
134             public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
135                     throws IOException, ClientProtocolException {
136
137                 throw new IOException("IO Exception");
138             }
139         };
140         inp.setMethod("delete");
141         con = new OnapHttpConnection();
142         con.delete(inp);
143     }
144
145     @Test(expected = IllegalArgumentException.class)
146     public void httpUnSecuredOtherExceptionTest() throws OnapCommandHttpFailure {
147         new MockUp<CloseableHttpClient>() {
148             @Mock
149             public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
150                     throws IOException, ClientProtocolException {
151
152                 throw new IOException("IO Exception");
153             }
154         };
155         inp.setMethod("other");
156         con = new OnapHttpConnection();
157         con.request(inp);
158     }
159
160     @Test(expected = OnapCommandHttpFailure.class)
161     public void testGetMultipartEntityWithoutMultipartEntityName() throws OnapCommandHttpFailure {
162         new MockUp<CloseableHttpClient>() {
163             @Mock
164             public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
165                     throws IOException, ClientProtocolException {
166
167                 throw new IOException("IO Exception");
168             }
169         };
170         new MockUp<HttpInput>() {
171
172             @Mock
173             public boolean isBinaryData() {
174                 return true;
175             }
176         };
177         Map<String, String> reqHeaders = new HashMap<>();
178         reqHeaders.put("Content-Disposition","form-data");
179         reqHeaders.put("name","upload");
180         reqHeaders.put("filename","upload.txt");
181         reqHeaders.put("Content-Type","application/octet-stream");
182         reqHeaders.put("Content-Transfer-Encoding","binary");
183         inp.setReqHeaders(reqHeaders);
184         inp.setMethod("post");
185         con = new OnapHttpConnection();
186         con.request(inp);
187     }
188
189     @Test(expected = OnapCommandHttpFailure.class)
190     public void testGetMultipartEntityWithMultipartEntityName() throws OnapCommandHttpFailure {
191         new MockUp<CloseableHttpClient>() {
192             @Mock
193             public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
194                     throws IOException, ClientProtocolException {
195
196                 throw new IOException("IO Exception");
197             }
198         };
199         new MockUp<HttpInput>() {
200
201             @Mock
202             public boolean isBinaryData() {
203                 return true;
204             }
205         };
206         Map<String, String> reqHeaders = new HashMap<>();
207         reqHeaders.put("Content-Disposition","form-data");
208         reqHeaders.put("name","upload");
209         reqHeaders.put("filename","upload.txt");
210         reqHeaders.put("Content-Type","application/octet-stream");
211         reqHeaders.put("Content-Transfer-Encoding","binary");
212         inp.setReqHeaders(reqHeaders);
213         inp.setMethod("post");
214         inp.setMultipartEntityName("test");
215         con = new OnapHttpConnection();
216         con.request(inp);
217     }
218
219     @Test()
220     public void httpUnSecuredCloseExceptionTest() throws IOException {
221         inp.setMethod("other");
222         con = new OnapHttpConnection();
223         assertDoesNotThrow(() -> con.close());
224     }
225
226     @Test
227     public void httpSecuredGetExceptionTest() {
228
229         // ProtocolVersion p = new ProtocolVersion("http",1,0);
230         // HttpResponse hr = DefaultHttpResponseFactory.INSTANCE.newHttpResponse(p, 200 , null) ;
231
232         new MockUp<CloseableHttpClient>() {
233             @Mock
234             public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
235                     throws IOException, ClientProtocolException {
236
237                 throw new IOException("IO Exception");
238             }
239         };
240         try {
241             HttpInput inp = new HttpInput();
242             inp.setMethod("get");
243             inp.setBody("body");
244             inp.setReqHeaders(new HashMap<String, String>());
245             inp.setReqQueries(new HashMap<String, String>());
246             inp.setUri("https://192.168.99.10:80");
247             OnapHttpConnection con = new OnapHttpConnection();
248             con.get(inp);
249         } catch (OnapCommandHttpFailure e) {
250             assertEquals("0x3001::IO Exception", e.getMessage());
251         }
252     }
253
254     private static void mockHttpRequest(HttpResult result) {
255         new MockUp<OnapHttpConnection>() {
256             boolean isMock = false;
257
258             @Mock
259             public HttpResult request(Invocation inv, HttpInput input) throws OnapCommandHttpFailure {
260                 if (isMock) {
261                     return result;
262                 } else {
263                     return inv.proceed(input);
264                 }
265             }
266         };
267     }
268 }