2 * Copyright (c) 2016, 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.openo.baseservice.roa.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 org.openo.baseservice.remoteservice.exception.ServiceException;
25 import org.apache.log4j.Level;
26 import org.apache.log4j.LogManager;
27 import org.eclipse.jetty.client.Address;
28 import org.eclipse.jetty.client.CachedExchange;
29 import org.eclipse.jetty.client.HttpDestination;
30 import org.eclipse.jetty.client.HttpExchange;
31 import org.eclipse.jetty.http.HttpFields;
32 import org.eclipse.jetty.http.HttpHeaders;
33 import org.eclipse.jetty.io.Buffer;
34 import org.eclipse.jetty.io.ByteArrayBuffer;
35 import org.eclipse.jetty.util.StringUtil;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.ExpectedException;
43 import org.junit.runner.RunWith;
45 import java.io.ByteArrayInputStream;
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.io.InputStreamReader;
49 import java.lang.reflect.Field;
50 import java.util.ArrayList;
51 import java.util.List;
52 import java.util.concurrent.atomic.AtomicInteger;
53 import java.util.zip.GZIPInputStream;
58 import mockit.integration.junit4.JMockit;
66 * @version SDNO 0.5 20-Jun-2016
68 @RunWith(JMockit.class)
69 public class TestRestHttpContentExchange {
72 HttpDestination mockedDest;
75 public ExpectedException thrown = ExpectedException.none();
80 * @throws java.lang.Exception
84 public static void setUpBeforeClass() throws Exception {
90 * @throws java.lang.Exception
94 public static void tearDownAfterClass() throws Exception {
100 * @throws java.lang.Exception
104 public void setUp() throws Exception {
110 * @throws java.lang.Exception
114 public void tearDown() throws Exception {
115 LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.ERROR);
121 * @throws IOException
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();
132 LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG);
133 exchange.onRequestCommitted();
139 * @throws IOException
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();
150 LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG);
151 exchange.onRequestComplete();
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();
168 LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG);
169 exchange.onResponseComplete();
171 final AtomicInteger isCallback = new AtomicInteger(0);
172 final AtomicInteger isException = new AtomicInteger(0);
173 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
176 public void callback(final RestfulResponse response) {
181 public void handleExcepion(final Throwable e) {
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());
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 System.out.println("actual: '" + actual + "'");
219 System.out.println("expected: '" + expected + "'");
220 assertEquals(actual, expected);
222 new MockUp<ByteArrayInputStream>() {
225 public int read() throws Exception {
226 throw new IOException();
230 public int read(final byte abyte0[], final int i, final int j) {
237 thrown.expect(IOException.class);
238 exchange.decompressGzipToStr(binaryData);
248 public void testDecompressGzipToStrException() throws Exception {
249 final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
250 final Address address = new Address("localhost", 9999);
251 exchange.setAddress(address);
252 exchange.setRequestURI("/the/request/uri");
254 final InputStream stream = ClassLoader.getSystemResourceAsStream("sample.txt.gz");
255 final byte[] binaryData = new byte[1024];
256 stream.read(binaryData);
257 final String expected = "sample data.";
259 new MockUp<GZIPInputStream>() {
262 public void close() throws IOException {
263 throw new IOException();
268 new MockUp<InputStreamReader>() {
271 public void close() throws IOException {
272 throw new IOException();
277 new MockUp<ByteArrayInputStream>() {
280 public void close() throws IOException {
281 throw new IOException();
286 final String actual = exchange.decompressGzipToStr(binaryData);
288 System.out.println("actual: '" + actual + "'");
289 System.out.println("expected: '" + expected + "'");
290 assertEquals(actual, expected);
300 public void testDecompressGzipToStrNull() throws Exception {
301 final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
302 final Address address = new Address("localhost", 9999);
303 exchange.setAddress(address);
304 exchange.setRequestURI("/the/request/uri");
305 final String expected = "";
306 final String actual = exchange.decompressGzipToStr(null);
308 System.out.println("actual: '" + actual + "'");
309 System.out.println("expected: '" + expected + "'");
310 assertEquals(actual, expected);
320 public void testOnResponseHeaderBufferBuffer() throws Exception {
321 final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
322 final Address address = new Address("localhost", 9999);
323 exchange.setAddress(address);
324 exchange.setRequestURI("/the/request/uri");
326 final Buffer name = new ByteArrayBuffer("key");
327 final Buffer value = new ByteArrayBuffer("value");
328 exchange.onResponseHeader(name, value);
330 new MockUp<HttpHeaders>() {
333 public int getOrdinal(final Buffer buffer) {
334 return HttpHeaders.CONTENT_ENCODING_ORDINAL;
338 exchange.onResponseHeader(name, value);
340 new MockUp<StringUtil>() {
343 public String asciiToLowerCase(final String s) {
348 exchange.onResponseHeader(name, value);
358 public void testOnExceptionThrowable() {
359 final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
360 final Address address = new Address("localhost", 9999);
361 exchange.setAddress(address);
362 exchange.setRequestURI("/the/request/uri");
363 exchange.onException(new Exception());
372 public void testOnExceptionThrowableWithCallback() {
373 final AtomicInteger isCallback = new AtomicInteger(0);
374 final AtomicInteger isException = new AtomicInteger(0);
375 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
378 public void callback(final RestfulResponse response) {
383 public void handleExcepion(final Throwable e) {
388 final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback);
389 final Address address = new Address("localhost", 9999);
390 exchange.setAddress(address);
391 exchange.setRequestURI("/the/request/uri");
392 exchange.onException(new Exception());
393 assertEquals(0, isCallback.get());
394 assertEquals(1, isException.get());
403 public void testOnConnectionFailedThrowable() {
404 final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
405 final Address address = new Address("localhost", 9999);
406 exchange.setAddress(address);
407 exchange.setRequestURI("/the/request/uri");
408 exchange.onConnectionFailed(new Exception());
417 public void testOnConnectionFailedThrowableException() {
418 final AtomicInteger isCallback = new AtomicInteger(0);
419 final AtomicInteger isException = new AtomicInteger(0);
420 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
423 public void callback(final RestfulResponse response) {
428 public void handleExcepion(final Throwable e) {
433 final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback);
434 final Address address = new Address("localhost", 9999);
435 exchange.setAddress(address);
436 exchange.setRequestURI("/the/request/uri");
437 exchange.onConnectionFailed(new Exception());
438 assertEquals(0, isCallback.get());
439 assertEquals(1, isException.get());
448 public void testExpireHttpDestination() {
449 final RestHttpContentExchange exchange = new RestHttpContentExchange(true, null);
450 final Address address = new Address("localhost", 9999);
451 exchange.setAddress(address);
452 exchange.setRequestURI("/the/request/uri");
453 exchange.expire(mockedDest);
463 public void testExpireHttpDestinationException() throws Exception {
464 final AtomicInteger isCallback = new AtomicInteger(0);
465 final AtomicInteger isException = new AtomicInteger(0);
466 final List<Throwable> thrSet = new ArrayList<Throwable>();
467 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
470 public void callback(final RestfulResponse response) {
475 public void handleExcepion(final Throwable e) {
481 final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback);
482 final Address address = new Address("localhost", 9999);
483 exchange.setAddress(address);
484 exchange.setRequestURI("/the/request/uri");
485 exchange.expire(mockedDest);
486 assertEquals(0, isCallback.get());
487 assertEquals(1, isException.get());
488 assertEquals(1, thrSet.size());
489 final Throwable t = thrSet.get(0);
490 assertEquals(ServiceException.class, t.getClass());
500 public void testIsGzip() throws Exception {
501 final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
502 final Address address = new Address("localhost", 9999);
503 exchange.setAddress(address);
504 exchange.setRequestURI("/the/request/uri");
506 final Buffer name = new ByteArrayBuffer("key");
507 final Buffer value = new ByteArrayBuffer("value");
509 new MockUp<HttpHeaders>() {
512 public int getOrdinal(final Buffer buffer) {
513 return HttpHeaders.CONTENT_ENCODING_ORDINAL;
517 exchange.onResponseHeader(name, value);
518 assertFalse(exchange.isGzip());
520 new MockUp<StringUtil>() {
523 public String asciiToLowerCase(final String s) {
528 exchange.onResponseHeader(name, value);
529 assertTrue(exchange.isGzip());
539 public void testGetResponse() throws Exception {
540 final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
541 final Address address = new Address("localhost", 9999);
542 exchange.setAddress(address);
543 exchange.setRequestURI("/the/request/uri");
545 final Field statusField = HttpExchange.class.getDeclaredField("_status");
546 statusField.setAccessible(true);
547 statusField.set(exchange, new AtomicInteger(200));
549 RestfulResponse response = exchange.getResponse();
550 assertEquals(0, response.getStatus());
552 final HttpFields fields = new HttpFields();
553 final Field headerFields = CachedExchange.class.getDeclaredField("_responseFields");
554 headerFields.setAccessible(true);
555 headerFields.set(exchange, fields);
556 response = exchange.getResponse();
557 assertEquals(0, response.getStatus());
558 fields.add("Content-Type", "application/json");
559 fields.add("Content-Encode", "UTF-8");
560 response = exchange.getResponse();
561 assertEquals(0, response.getStatus());
571 public void testGetResponseGzip() throws Exception {
572 final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
573 final Address address = new Address("localhost", 9999);
574 exchange.setAddress(address);
575 exchange.setRequestURI("/the/request/uri");
576 new MockUp<RestHttpContentExchange>() {
579 public boolean isGzip() {
583 final Field statusField = HttpExchange.class.getDeclaredField("_status");
584 statusField.setAccessible(true);
585 statusField.set(exchange, new AtomicInteger(200));
587 final RestfulResponse response = exchange.getResponse();
588 assertEquals(0, response.getStatus());