2 * Copyright 2016-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.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 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;
53 import org.openo.baseservice.remoteservice.exception.ServiceException;
58 import mockit.integration.junit4.JMockit;
66 * @version 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());
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");
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.";
217 final String actual = exchange.decompressGzipToStr(binaryData);
219 System.out.println("actual: '" + actual + "'");
220 System.out.println("expected: '" + expected + "'");
221 assertEquals(actual, expected);
223 new MockUp<ByteArrayInputStream>() {
226 public int read() throws Exception {
227 throw new IOException();
231 public int read(final byte abyte0[], final int i, final int j) {
238 thrown.expect(IOException.class);
239 exchange.decompressGzipToStr(binaryData);
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");
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.";
261 new MockUp<GZIPInputStream>() {
264 public void close() throws IOException {
265 throw new IOException();
270 new MockUp<InputStreamReader>() {
273 public void close() throws IOException {
274 throw new IOException();
279 new MockUp<ByteArrayInputStream>() {
282 public void close() throws IOException {
283 throw new IOException();
288 final String actual = exchange.decompressGzipToStr(binaryData);
290 System.out.println("actual: '" + actual + "'");
291 System.out.println("expected: '" + expected + "'");
292 assertEquals(actual, expected);
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);
310 System.out.println("actual: '" + actual + "'");
311 System.out.println("expected: '" + expected + "'");
312 assertEquals(actual, expected);
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");
328 final Buffer name = new ByteArrayBuffer("key");
329 final Buffer value = new ByteArrayBuffer("value");
330 exchange.onResponseHeader(name, value);
332 new MockUp<HttpHeaders>() {
335 public int getOrdinal(final Buffer buffer) {
336 return HttpHeaders.CONTENT_ENCODING_ORDINAL;
340 exchange.onResponseHeader(name, value);
342 new MockUp<StringUtil>() {
345 public String asciiToLowerCase(final String s) {
350 exchange.onResponseHeader(name, value);
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());
374 public void testOnExceptionThrowableWithCallback() {
375 final AtomicInteger isCallback = new AtomicInteger(0);
376 final AtomicInteger isException = new AtomicInteger(0);
377 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
380 public void callback(final RestfulResponse response) {
385 public void handleExcepion(final Throwable e) {
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());
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());
419 public void testOnConnectionFailedThrowableException() {
420 final AtomicInteger isCallback = new AtomicInteger(0);
421 final AtomicInteger isException = new AtomicInteger(0);
422 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
425 public void callback(final RestfulResponse response) {
430 public void handleExcepion(final Throwable e) {
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());
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);
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() {
472 public void callback(final RestfulResponse response) {
477 public void handleExcepion(final Throwable e) {
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());
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");
508 final Buffer name = new ByteArrayBuffer("key");
509 final Buffer value = new ByteArrayBuffer("value");
511 new MockUp<HttpHeaders>() {
514 public int getOrdinal(final Buffer buffer) {
515 return HttpHeaders.CONTENT_ENCODING_ORDINAL;
519 exchange.onResponseHeader(name, value);
520 assertFalse(exchange.isGzip());
522 new MockUp<StringUtil>() {
525 public String asciiToLowerCase(final String s) {
530 exchange.onResponseHeader(name, value);
531 assertTrue(exchange.isGzip());
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");
547 final Field statusField = HttpExchange.class.getDeclaredField("_status");
548 statusField.setAccessible(true);
549 statusField.set(exchange, new AtomicInteger(200));
551 RestfulResponse response = exchange.getResponse();
552 assertEquals(0, response.getStatus());
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());
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>() {
581 public boolean isGzip() {
585 final Field statusField = HttpExchange.class.getDeclaredField("_status");
586 statusField.setAccessible(true);
587 statusField.set(exchange, new AtomicInteger(200));
589 final RestfulResponse response = exchange.getResponse();
590 assertEquals(0, response.getStatus());