2 * Copyright 2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.resmanagement.common.util.restclient;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
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;
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;
57 import mockit.integration.junit4.JMockit;
67 @RunWith(JMockit.class)
68 public class TestRestHttpContentExchange {
71 HttpDestination mockedDest;
74 public ExpectedException thrown = ExpectedException.none();
79 * @throws java.lang.Exception
83 public static void setUpBeforeClass() throws Exception {
89 * @throws java.lang.Exception
93 public static void tearDownAfterClass() throws Exception {
99 * @throws java.lang.Exception
103 public void setUp() throws Exception {
109 * @throws java.lang.Exception
113 public void tearDown() throws Exception {
114 LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.ERROR);
120 * @throws IOException
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();
131 LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG);
132 exchange.onRequestCommitted();
138 * @throws IOException
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();
149 LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG);
150 exchange.onRequestComplete();
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();
167 LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG);
168 exchange.onResponseComplete();
170 final AtomicInteger isCallback = new AtomicInteger(0);
171 final AtomicInteger isException = new AtomicInteger(0);
172 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
175 public void callback(final RestfulResponse response) {
180 public void handleExcepion(final Throwable e) {
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());
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");
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.";
216 final String actual = exchange.decompressGzipToStr(binaryData);
218 assertEquals(actual, expected);
220 new MockUp<ByteArrayInputStream>() {
223 public int read() throws Exception {
224 throw new IOException();
228 public int read(final byte abyte0[], final int i, final int j) {
235 thrown.expect(IOException.class);
236 exchange.decompressGzipToStr(binaryData);
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");
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.";
258 new MockUp<GZIPInputStream>() {
261 public void close() throws IOException {
262 throw new IOException();
267 new MockUp<InputStreamReader>() {
270 public void close() throws IOException {
271 throw new IOException();
276 new MockUp<ByteArrayInputStream>() {
279 public void close() throws IOException {
280 throw new IOException();
285 final String actual = exchange.decompressGzipToStr(binaryData);
287 System.out.println("actual: '" + actual + "'");
288 System.out.println("expected: '" + expected + "'");
289 assertEquals(actual, expected);
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);
307 System.out.println("actual: '" + actual + "'");
308 System.out.println("expected: '" + expected + "'");
309 assertEquals(actual, expected);
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");
325 final Buffer name = new ByteArrayBuffer("key");
326 final Buffer value = new ByteArrayBuffer("value");
327 exchange.onResponseHeader(name, value);
329 new MockUp<HttpHeaders>() {
332 public int getOrdinal(final Buffer buffer) {
333 return HttpHeaders.CONTENT_ENCODING_ORDINAL;
337 exchange.onResponseHeader(name, value);
339 new MockUp<StringUtil>() {
342 public String asciiToLowerCase(final String s) {
347 exchange.onResponseHeader(name, value);
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());
371 public void testOnExceptionThrowableWithCallback() {
372 final AtomicInteger isCallback = new AtomicInteger(0);
373 final AtomicInteger isException = new AtomicInteger(0);
374 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
377 public void callback(final RestfulResponse response) {
382 public void handleExcepion(final Throwable e) {
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());
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());
416 public void testOnConnectionFailedThrowableException() {
417 final AtomicInteger isCallback = new AtomicInteger(0);
418 final AtomicInteger isException = new AtomicInteger(0);
419 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
422 public void callback(final RestfulResponse response) {
427 public void handleExcepion(final Throwable e) {
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());
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);
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() {
469 public void callback(final RestfulResponse response) {
474 public void handleExcepion(final Throwable e) {
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());
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");
505 final Buffer name = new ByteArrayBuffer("key");
506 final Buffer value = new ByteArrayBuffer("value");
508 new MockUp<HttpHeaders>() {
511 public int getOrdinal(final Buffer buffer) {
512 return HttpHeaders.CONTENT_ENCODING_ORDINAL;
516 exchange.onResponseHeader(name, value);
517 assertFalse(exchange.isGzip());
519 new MockUp<StringUtil>() {
522 public String asciiToLowerCase(final String s) {
527 exchange.onResponseHeader(name, value);
528 assertTrue(exchange.isGzip());
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");
544 final Field statusField = HttpExchange.class.getDeclaredField("_status");
545 statusField.setAccessible(true);
546 statusField.set(exchange, new AtomicInteger(200));
548 RestfulResponse response = exchange.getResponse();
549 assertEquals(0, response.getStatus());
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());
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>() {
578 public boolean isGzip() {
582 final Field statusField = HttpExchange.class.getDeclaredField("_status");
583 statusField.setAccessible(true);
584 statusField.set(exchange, new AtomicInteger(200));
586 final RestfulResponse response = exchange.getResponse();
587 assertEquals(0, response.getStatus());