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