cf28a5c80f8ba6c9db01f15a8e1c2bd6c8523522
[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.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).atLevel(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).atLevel(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).atLevel(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).atLevel(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         assertEquals(actual, expected);
287     }
288
289     /**
290      * <br/>
291      * 
292      * @throws Exception
293      * @since
294      */
295     @Test
296     public void testDecompressGzipToStrNull() throws Exception {
297         final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
298         final Address address = new Address("localhost", 9999);
299         exchange.setAddress(address);
300         exchange.setRequestURI("/the/request/uri");
301         final String expected = "";
302         final String actual = exchange.decompressGzipToStr(null);
303
304         assertEquals(actual, expected);
305     }
306
307     /**
308      * <br/>
309      * 
310      * @throws Exception
311      * @since
312      */
313     @Test
314     public void testOnResponseHeaderBufferBuffer() throws Exception {
315         final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
316         final Address address = new Address("localhost", 9999);
317         exchange.setAddress(address);
318         exchange.setRequestURI("/the/request/uri");
319
320         final Buffer name = new ByteArrayBuffer("key");
321         final Buffer value = new ByteArrayBuffer("value");
322         exchange.onResponseHeader(name, value);
323
324         new MockUp<HttpHeaders>() {
325
326             @Mock
327             public int getOrdinal(final Buffer buffer) {
328                 return HttpHeaders.CONTENT_ENCODING_ORDINAL;
329             }
330
331         };
332         exchange.onResponseHeader(name, value);
333
334         new MockUp<StringUtil>() {
335
336             @Mock
337             public String asciiToLowerCase(final String s) {
338                 return "gzip";
339             }
340
341         };
342         exchange.onResponseHeader(name, value);
343
344     }
345
346     /**
347      * <br/>
348      * 
349      * @since
350      */
351     @Test
352     public void testOnExceptionThrowable() {
353         final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
354         final Address address = new Address("localhost", 9999);
355         exchange.setAddress(address);
356         exchange.setRequestURI("/the/request/uri");
357         exchange.onException(new Exception());
358     }
359
360     /**
361      * <br/>
362      * 
363      * @since
364      */
365     @Test
366     public void testOnExceptionThrowableWithCallback() {
367         final AtomicInteger isCallback = new AtomicInteger(0);
368         final AtomicInteger isException = new AtomicInteger(0);
369         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
370
371             @Override
372             public void callback(final RestfulResponse response) {
373                 isCallback.set(1);
374             }
375
376             @Override
377             public void handleExcepion(final Throwable e) {
378                 isException.set(1);
379             }
380
381         };
382         final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback);
383         final Address address = new Address("localhost", 9999);
384         exchange.setAddress(address);
385         exchange.setRequestURI("/the/request/uri");
386         exchange.onException(new Exception());
387         assertEquals(0, isCallback.get());
388         assertEquals(1, isException.get());
389     }
390
391     /**
392      * <br/>
393      * 
394      * @since
395      */
396     @Test
397     public void testOnConnectionFailedThrowable() {
398         final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
399         final Address address = new Address("localhost", 9999);
400         exchange.setAddress(address);
401         exchange.setRequestURI("/the/request/uri");
402         exchange.onConnectionFailed(new Exception());
403     }
404
405     /**
406      * <br/>
407      * 
408      * @since
409      */
410     @Test
411     public void testOnConnectionFailedThrowableException() {
412         final AtomicInteger isCallback = new AtomicInteger(0);
413         final AtomicInteger isException = new AtomicInteger(0);
414         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
415
416             @Override
417             public void callback(final RestfulResponse response) {
418                 isCallback.set(1);
419             }
420
421             @Override
422             public void handleExcepion(final Throwable e) {
423                 isException.set(1);
424             }
425
426         };
427         final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback);
428         final Address address = new Address("localhost", 9999);
429         exchange.setAddress(address);
430         exchange.setRequestURI("/the/request/uri");
431         exchange.onConnectionFailed(new Exception());
432         assertEquals(0, isCallback.get());
433         assertEquals(1, isException.get());
434     }
435
436     /**
437      * <br/>
438      * 
439      * @since
440      */
441     @Test
442     public void testExpireHttpDestination() {
443         final RestHttpContentExchange exchange = new RestHttpContentExchange(true, null);
444         final Address address = new Address("localhost", 9999);
445         exchange.setAddress(address);
446         exchange.setRequestURI("/the/request/uri");
447         exchange.expire(mockedDest);
448     }
449
450     /**
451      * <br/>
452      * 
453      * @throws Exception
454      * @since
455      */
456     @Test
457     public void testExpireHttpDestinationException() throws Exception {
458         final AtomicInteger isCallback = new AtomicInteger(0);
459         final AtomicInteger isException = new AtomicInteger(0);
460         final List<Throwable> thrSet = new ArrayList<Throwable>();
461         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
462
463             @Override
464             public void callback(final RestfulResponse response) {
465                 isCallback.set(1);
466             }
467
468             @Override
469             public void handleExcepion(final Throwable e) {
470                 isException.set(1);
471                 thrSet.add(e);
472             }
473
474         };
475         final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback);
476         final Address address = new Address("localhost", 9999);
477         exchange.setAddress(address);
478         exchange.setRequestURI("/the/request/uri");
479         exchange.expire(mockedDest);
480         assertEquals(0, isCallback.get());
481         assertEquals(1, isException.get());
482         assertEquals(1, thrSet.size());
483         final Throwable t = thrSet.get(0);
484         assertEquals(ServiceException.class, t.getClass());
485     }
486
487     /**
488      * <br/>
489      * 
490      * @throws Exception
491      * @since
492      */
493     @Test
494     public void testIsGzip() throws Exception {
495         final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
496         final Address address = new Address("localhost", 9999);
497         exchange.setAddress(address);
498         exchange.setRequestURI("/the/request/uri");
499
500         final Buffer name = new ByteArrayBuffer("key");
501         final Buffer value = new ByteArrayBuffer("value");
502
503         new MockUp<HttpHeaders>() {
504
505             @Mock
506             public int getOrdinal(final Buffer buffer) {
507                 return HttpHeaders.CONTENT_ENCODING_ORDINAL;
508             }
509
510         };
511         exchange.onResponseHeader(name, value);
512         assertFalse(exchange.isGzip());
513
514         new MockUp<StringUtil>() {
515
516             @Mock
517             public String asciiToLowerCase(final String s) {
518                 return "gzip";
519             }
520
521         };
522         exchange.onResponseHeader(name, value);
523         assertTrue(exchange.isGzip());
524     }
525
526     /**
527      * <br/>
528      * 
529      * @throws Exception
530      * @since
531      */
532     @Test
533     public void testGetResponse() throws Exception {
534         final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
535         final Address address = new Address("localhost", 9999);
536         exchange.setAddress(address);
537         exchange.setRequestURI("/the/request/uri");
538
539         final Field statusField = HttpExchange.class.getDeclaredField("_status");
540         statusField.setAccessible(true);
541         statusField.set(exchange, new AtomicInteger(200));
542
543         RestfulResponse response = exchange.getResponse();
544         assertEquals(0, response.getStatus());
545
546         final HttpFields fields = new HttpFields();
547         final Field headerFields = CachedExchange.class.getDeclaredField("_responseFields");
548         headerFields.setAccessible(true);
549         headerFields.set(exchange, fields);
550         response = exchange.getResponse();
551         assertEquals(0, response.getStatus());
552         fields.add("Content-Type", "application/json");
553         fields.add("Content-Encode", "UTF-8");
554         response = exchange.getResponse();
555         assertEquals(0, response.getStatus());
556     }
557
558     /**
559      * <br/>
560      * 
561      * @throws Exception
562      * @since
563      */
564     @Test
565     public void testGetResponseGzip() throws Exception {
566         final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
567         final Address address = new Address("localhost", 9999);
568         exchange.setAddress(address);
569         exchange.setRequestURI("/the/request/uri");
570         new MockUp<RestHttpContentExchange>() {
571
572             @Mock
573             public boolean isGzip() {
574                 return true;
575             }
576         };
577         final Field statusField = HttpExchange.class.getDeclaredField("_status");
578         statusField.setAccessible(true);
579         statusField.set(exchange, new AtomicInteger(200));
580
581         final RestfulResponse response = exchange.getResponse();
582         assertEquals(0, response.getStatus());
583     }
584 }