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.vnfm.svnfm.vnfmadapter.common.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);
286 assertEquals(actual, expected);
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);
304 assertEquals(actual, expected);
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");
320 final Buffer name = new ByteArrayBuffer("key");
321 final Buffer value = new ByteArrayBuffer("value");
322 exchange.onResponseHeader(name, value);
324 new MockUp<HttpHeaders>() {
327 public int getOrdinal(final Buffer buffer) {
328 return HttpHeaders.CONTENT_ENCODING_ORDINAL;
332 exchange.onResponseHeader(name, value);
334 new MockUp<StringUtil>() {
337 public String asciiToLowerCase(final String s) {
342 exchange.onResponseHeader(name, value);
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());
366 public void testOnExceptionThrowableWithCallback() {
367 final AtomicInteger isCallback = new AtomicInteger(0);
368 final AtomicInteger isException = new AtomicInteger(0);
369 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
372 public void callback(final RestfulResponse response) {
377 public void handleExcepion(final Throwable e) {
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());
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());
411 public void testOnConnectionFailedThrowableException() {
412 final AtomicInteger isCallback = new AtomicInteger(0);
413 final AtomicInteger isException = new AtomicInteger(0);
414 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
417 public void callback(final RestfulResponse response) {
422 public void handleExcepion(final Throwable e) {
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());
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);
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() {
464 public void callback(final RestfulResponse response) {
469 public void handleExcepion(final Throwable e) {
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());
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");
500 final Buffer name = new ByteArrayBuffer("key");
501 final Buffer value = new ByteArrayBuffer("value");
503 new MockUp<HttpHeaders>() {
506 public int getOrdinal(final Buffer buffer) {
507 return HttpHeaders.CONTENT_ENCODING_ORDINAL;
511 exchange.onResponseHeader(name, value);
512 assertFalse(exchange.isGzip());
514 new MockUp<StringUtil>() {
517 public String asciiToLowerCase(final String s) {
522 exchange.onResponseHeader(name, value);
523 assertTrue(exchange.isGzip());
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");
539 final Field statusField = HttpExchange.class.getDeclaredField("_status");
540 statusField.setAccessible(true);
541 statusField.set(exchange, new AtomicInteger(200));
543 RestfulResponse response = exchange.getResponse();
544 assertEquals(0, response.getStatus());
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());
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>() {
573 public boolean isGzip() {
577 final Field statusField = HttpExchange.class.getDeclaredField("_status");
578 statusField.setAccessible(true);
579 statusField.set(exchange, new AtomicInteger(200));
581 final RestfulResponse response = exchange.getResponse();
582 assertEquals(0, response.getStatus());