Fix SVNFM jetty-all vulnerability fix
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / test / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / common / restclient / TestRestHttpContentExchange.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.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.lang.reflect.Field;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.concurrent.atomic.AtomicInteger;
31 import java.util.zip.GZIPInputStream;
32
33 import org.apache.logging.log4j.Level;
34 import org.apache.logging.log4j.LogManager;
35 //import org.eclipse.jetty.client.HttpRequestListeners;
36 //import org.eclipse.jetty.client.CachedExchange;
37 import org.eclipse.jetty.client.HttpDestination;
38 //import org.eclipse.jetty.client.HttpExchange;
39 //import org.eclipse.jetty.http.HttpFields;
40 //import org.eclipse.jetty.http.HttpHeaders;
41 //import org.eclipse.jetty.io.Buffer;
42 //import org.eclipse.jetty.io.ByteArrayBuffer;
43 import org.eclipse.jetty.util.StringUtil;
44 import org.junit.After;
45 import org.junit.AfterClass;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Ignore;
49 import org.junit.Rule;
50 import org.junit.Test;
51 import org.junit.rules.ExpectedException;
52 import org.junit.runner.RunWith;
53
54 import mockit.Mock;
55 import mockit.MockUp;
56 import mockit.Mocked;
57 import mockit.integration.junit4.JMockit;
58
59 /**
60  * <br/>
61  * <p>
62  * </p>
63  * 
64  * @author
65  * @version
66  */
67 @RunWith(JMockit.class)
68 public class TestRestHttpContentExchange {
69
70     @Mocked
71     HttpDestination mockedDest;
72
73     @Rule
74     public ExpectedException thrown = ExpectedException.none();
75
76     /**
77      * <br/>
78      *
79      * @throws java.lang.Exception
80      * @since
81      */
82     @BeforeClass
83     public static void setUpBeforeClass() throws Exception {
84     }
85
86     /**
87      * <br/>
88      *
89      * @throws java.lang.Exception
90      * @since
91      */
92     @AfterClass
93     public static void tearDownAfterClass() throws Exception {
94     }
95
96     /**
97      * <br/>
98      *
99      * @throws java.lang.Exception
100      * @since
101      */
102     @Before
103     public void setUp() throws Exception {
104     }
105
106     /**
107      * <br/>
108      *
109      * @throws java.lang.Exception
110      * @since
111      */
112     @After
113     public void tearDown() throws Exception {
114         LogManager.getLogger(RestHttpContentExchange.class).atLevel(Level.ERROR);
115     }
116
117     /**
118      * <br/>
119      *
120      * @throws IOException
121      * @since
122      */
123     @Test
124     public void testGetResponse() throws IOException {
125        RestHttpContentExchange exchange = new RestHttpContentExchange();
126       int statusCode= exchange.getResponse().getStatus();
127       assertEquals(0,statusCode);
128
129     }
130 //    @Test
131 //    public void testOnRequestCommitted() throws IOException {
132 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
133 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
134 //        exchange.setAddress(address);
135 //        exchange.setRequestURI("/the/request/uri");
136 //        exchange.onRequestCommitted();
137 //
138 //        LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG);
139 //        exchange.onRequestCommitted();
140 //    }
141 //
142 //    /**
143 //     * <br/>
144 //     *
145 //     * @throws IOException
146 //     * @since
147 //     */
148 //    @Test
149 //    public void testOnRequestComplete() throws IOException {
150 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
151 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
152 //        exchange.setAddress(address);
153 //        exchange.setRequestURI("/the/request/uri");
154 //        exchange.onRequestComplete();
155 //
156 //        LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG);
157 //        exchange.onRequestComplete();
158 //    }
159 //
160 //    /**
161 //     * <br/>
162 //     *
163 //     * @throws Exception
164 //     * @since
165 //     */
166 //    @Test
167 //    public void testOnResponseComplete() throws Exception {
168 //        RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
169 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
170 //        exchange.setAddress(address);
171 //        exchange.setRequestURI("/the/request/uri");
172 //        exchange.onResponseComplete();
173 //
174 //        LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG);
175 //        exchange.onResponseComplete();
176 //
177 //        final AtomicInteger isCallback = new AtomicInteger(0);
178 //        final AtomicInteger isException = new AtomicInteger(0);
179 //        final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
180 //
181 //            @Override
182 //            public void callback(final RestfulResponse response) {
183 //                isCallback.set(1);
184 //            }
185 //
186 //            @Override
187 //            public void handleExcepion(final Throwable e) {
188 //                isException.set(1);
189 //            }
190 //
191 //        };
192 //
193 //        final Field statusField = HttpExchange.class.getDeclaredField("_status");
194 //        statusField.setAccessible(true);
195 //        exchange = new RestHttpContentExchange(false, callback);
196 //        statusField.set(exchange, new AtomicInteger(200));
197 //        exchange.setAddress(new HttpRequestListeners("localhost", 9999));
198 //        exchange.setRequestURI("/the/request/uri");
199 //        exchange.onResponseComplete();
200 //        assertEquals(1, isCallback.get());
201 //        assertEquals(0, isException.get());
202 //    }
203 //
204 //    /**
205 //     * <br/>
206 //     *
207 //     * @throws Exception
208 //     * @since
209 //     */
210 //    @Test
211 //    @Ignore
212 //    public void testDecompressGzipToStr() throws Exception {
213 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
214 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
215 //        exchange.setAddress(address);
216 //        exchange.setRequestURI("/the/request/uri");
217 //
218 //        final InputStream stream = ClassLoader.getSystemResourceAsStream("sample.txt.gz");
219 //        final byte[] binaryData = new byte[1024];
220 //        stream.read(binaryData);
221 //        final String expected = "sample data.";
222 //
223 //        final String actual = exchange.decompressGzipToStr(binaryData);
224 //
225 //        assertEquals(actual, expected);
226 //
227 //        new MockUp<ByteArrayInputStream>() {
228 //
229 //            @Mock
230 //            public int read() throws Exception {
231 //                throw new IOException();
232 //            }
233 //
234 //            @Mock
235 //            public int read(final byte abyte0[], final int i, final int j) {
236 //
237 //                return -1;
238 //            }
239 //
240 //        };
241 //
242 //        thrown.expect(IOException.class);
243 //        exchange.decompressGzipToStr(binaryData);
244 //    }
245 //
246 //    /**
247 //     * <br/>
248 //     *
249 //     * @throws Exception
250 //     * @since
251 //     */
252 //    @Test
253 //    @Ignore
254 //    public void testDecompressGzipToStrException() throws Exception {
255 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
256 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
257 //        exchange.setAddress(address);
258 //        exchange.setRequestURI("/the/request/uri");
259 //
260 //        final InputStream stream = ClassLoader.getSystemResourceAsStream("sample.txt.gz");
261 //        final byte[] binaryData = new byte[1024];
262 //        stream.read(binaryData);
263 //        final String expected = "sample data.";
264 //
265 //        new MockUp<GZIPInputStream>() {
266 //
267 //            @Mock
268 //            public void close() throws IOException {
269 //                throw new IOException();
270 //            }
271 //
272 //        };
273 //
274 //        new MockUp<InputStreamReader>() {
275 //
276 //            @Mock
277 //            public void close() throws IOException {
278 //                throw new IOException();
279 //            }
280 //
281 //        };
282 //
283 //        new MockUp<ByteArrayInputStream>() {
284 //
285 //            @Mock
286 //            public void close() throws IOException {
287 //                throw new IOException();
288 //            }
289 //
290 //        };
291 //
292 //        final String actual = exchange.decompressGzipToStr(binaryData);
293 //        assertEquals(actual, expected);
294 //    }
295 //
296 //    /**
297 //     * <br/>
298 //     *
299 //     * @throws Exception
300 //     * @since
301 //     */
302 //    @Test
303 //    public void testDecompressGzipToStrNull() throws Exception {
304 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
305 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
306 //        exchange.setAddress(address);
307 //        exchange.setRequestURI("/the/request/uri");
308 //        final String expected = "";
309 //        final String actual = exchange.decompressGzipToStr(null);
310 //
311 //        assertEquals(actual, expected);
312 //    }
313 //
314 //    /**
315 //     * <br/>
316 //     *
317 //     * @throws Exception
318 //     * @since
319 //     */
320 //    @Test
321 //    public void testOnResponseHeaderBufferBuffer() throws Exception {
322 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
323 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
324 //        exchange.setAddress(address);
325 //        exchange.setRequestURI("/the/request/uri");
326 //
327 //        final Buffer name = new ByteArrayBuffer("key");
328 //        final Buffer value = new ByteArrayBuffer("value");
329 //        exchange.onResponseHeader(name, value);
330 //
331 //        new MockUp<HttpHeaders>() {
332 //
333 //            @Mock
334 //            public int getOrdinal(final Buffer buffer) {
335 //                return HttpHeaders.CONTENT_ENCODING_ORDINAL;
336 //            }
337 //
338 //        };
339 //        exchange.onResponseHeader(name, value);
340 //
341 //        new MockUp<StringUtil>() {
342 //
343 //            @Mock
344 //            public String asciiToLowerCase(final String s) {
345 //                return "gzip";
346 //            }
347 //
348 //        };
349 //        exchange.onResponseHeader(name, value);
350 //
351 //    }
352 //
353 //    /**
354 //     * <br/>
355 //     *
356 //     * @since
357 //     */
358 //    @Test
359 //    public void testOnExceptionThrowable() {
360 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
361 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
362 //        exchange.setAddress(address);
363 //        exchange.setRequestURI("/the/request/uri");
364 //        exchange.onException(new Exception());
365 //    }
366 //
367 //    /**
368 //     * <br/>
369 //     *
370 //     * @since
371 //     */
372 //    @Test
373 //    public void testOnExceptionThrowableWithCallback() {
374 //        final AtomicInteger isCallback = new AtomicInteger(0);
375 //        final AtomicInteger isException = new AtomicInteger(0);
376 //        final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
377 //
378 //            @Override
379 //            public void callback(final RestfulResponse response) {
380 //                isCallback.set(1);
381 //            }
382 //
383 //            @Override
384 //            public void handleExcepion(final Throwable e) {
385 //                isException.set(1);
386 //            }
387 //
388 //        };
389 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback);
390 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
391 //        exchange.setAddress(address);
392 //        exchange.setRequestURI("/the/request/uri");
393 //        exchange.onException(new Exception());
394 //        assertEquals(0, isCallback.get());
395 //        assertEquals(1, isException.get());
396 //    }
397 //
398 //    /**
399 //     * <br/>
400 //     *
401 //     * @since
402 //     */
403 //    @Test
404 //    public void testOnConnectionFailedThrowable() {
405 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
406 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
407 //        exchange.setAddress(address);
408 //        exchange.setRequestURI("/the/request/uri");
409 //        exchange.onConnectionFailed(new Exception());
410 //    }
411 //
412 //    /**
413 //     * <br/>
414 //     *
415 //     * @since
416 //     */
417 //    @Test
418 //    public void testOnConnectionFailedThrowableException() {
419 //        final AtomicInteger isCallback = new AtomicInteger(0);
420 //        final AtomicInteger isException = new AtomicInteger(0);
421 //        final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
422 //
423 //            @Override
424 //            public void callback(final RestfulResponse response) {
425 //                isCallback.set(1);
426 //            }
427 //
428 //            @Override
429 //            public void handleExcepion(final Throwable e) {
430 //                isException.set(1);
431 //            }
432 //
433 //        };
434 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback);
435 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
436 //        exchange.setAddress(address);
437 //        exchange.setRequestURI("/the/request/uri");
438 //        exchange.onConnectionFailed(new Exception());
439 //        assertEquals(0, isCallback.get());
440 //        assertEquals(1, isException.get());
441 //    }
442 //
443 //    /**
444 //     * <br/>
445 //     *
446 //     * @since
447 //     */
448 //    @Test
449 //    public void testExpireHttpDestination() {
450 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(true, null);
451 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
452 //        exchange.setAddress(address);
453 //        exchange.setRequestURI("/the/request/uri");
454 //        exchange.expire(mockedDest);
455 //    }
456 //
457 //    /**
458 //     * <br/>
459 //     *
460 //     * @throws Exception
461 //     * @since
462 //     */
463 //    @Test
464 //    public void testExpireHttpDestinationException() throws Exception {
465 //        final AtomicInteger isCallback = new AtomicInteger(0);
466 //        final AtomicInteger isException = new AtomicInteger(0);
467 //        final List<Throwable> thrSet = new ArrayList<Throwable>();
468 //        final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
469 //
470 //            @Override
471 //            public void callback(final RestfulResponse response) {
472 //                isCallback.set(1);
473 //            }
474 //
475 //            @Override
476 //            public void handleExcepion(final Throwable e) {
477 //                isException.set(1);
478 //                thrSet.add(e);
479 //            }
480 //
481 //        };
482 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback);
483 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
484 //        exchange.setAddress(address);
485 //        exchange.setRequestURI("/the/request/uri");
486 //        exchange.expire(mockedDest);
487 //        assertEquals(0, isCallback.get());
488 //        assertEquals(1, isException.get());
489 //        assertEquals(1, thrSet.size());
490 //        final Throwable t = thrSet.get(0);
491 //        assertEquals(ServiceException.class, t.getClass());
492 //    }
493 //
494 //    /**
495 //     * <br/>
496 //     *
497 //     * @throws Exception
498 //     * @since
499 //     */
500 //    @Test
501 //    public void testIsGzip() throws Exception {
502 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
503 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
504 //        exchange.setAddress(address);
505 //        exchange.setRequestURI("/the/request/uri");
506 //
507 //        final Buffer name = new ByteArrayBuffer("key");
508 //        final Buffer value = new ByteArrayBuffer("value");
509 //
510 //        new MockUp<HttpHeaders>() {
511 //
512 //            @Mock
513 //            public int getOrdinal(final Buffer buffer) {
514 //                return HttpHeaders.CONTENT_ENCODING_ORDINAL;
515 //            }
516 //
517 //        };
518 //        exchange.onResponseHeader(name, value);
519 //        assertFalse(exchange.isGzip());
520 //
521 //        new MockUp<StringUtil>() {
522 //
523 //            @Mock
524 //            public String asciiToLowerCase(final String s) {
525 //                return "gzip";
526 //            }
527 //
528 //        };
529 //        exchange.onResponseHeader(name, value);
530 //        assertTrue(exchange.isGzip());
531 //    }
532 //
533 //    /**
534 //     * <br/>
535 //     *
536 //     * @throws Exception
537 //     * @since
538 //     */
539 //    @Test
540 //    public void testGetResponse() throws Exception {
541 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
542 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
543 //        exchange.setAddress(address);
544 //        exchange.setRequestURI("/the/request/uri");
545 //
546 //        final Field statusField = HttpExchange.class.getDeclaredField("_status");
547 //        statusField.setAccessible(true);
548 //        statusField.set(exchange, new AtomicInteger(200));
549 //
550 //        RestfulResponse response = exchange.getResponse();
551 //        assertEquals(0, response.getStatus());
552 //
553 //        final HttpFields fields = new HttpFields();
554 //        final Field headerFields = CachedExchange.class.getDeclaredField("_responseFields");
555 //        headerFields.setAccessible(true);
556 //        headerFields.set(exchange, fields);
557 //        response = exchange.getResponse();
558 //        assertEquals(0, response.getStatus());
559 //        fields.add("Content-Type", "application/json");
560 //        fields.add("Content-Encode", "UTF-8");
561 //        response = exchange.getResponse();
562 //        assertEquals(0, response.getStatus());
563 //    }
564 //
565 //    /**
566 //     * <br/>
567 //     *
568 //     * @throws Exception
569 //     * @since
570 //     */
571 //    @Test
572 //    public void testGetResponseGzip() throws Exception {
573 //        final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
574 //        final HttpRequestListeners address = new HttpRequestListeners("localhost", 9999);
575 //        exchange.setAddress(address);
576 //        exchange.setRequestURI("/the/request/uri");
577 //        new MockUp<RestHttpContentExchange>() {
578 //
579 //            @Mock
580 //            public boolean isGzip() {
581 //                return true;
582 //            }
583 //        };
584 //        final Field statusField = HttpExchange.class.getDeclaredField("_status");
585 //        statusField.setAccessible(true);
586 //        statusField.set(exchange, new AtomicInteger(200));
587 //
588 //        final RestfulResponse response = exchange.getResponse();
589 //        assertEquals(0, response.getStatus());
590 //    }
591 }