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