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