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.assertNotNull;
22 import java.io.IOException;
23 import java.lang.reflect.Field;
24 import java.util.List;
26 import org.eclipse.jetty.client.HttpClient;
27 import org.eclipse.jetty.client.HttpContentResponse;
28 import org.eclipse.jetty.client.HttpExchange;
29 import org.eclipse.jetty.client.HttpRequest;
30 import org.eclipse.jetty.client.api.ContentResponse;
31 import org.eclipse.jetty.client.api.Request;
32 import org.eclipse.jetty.http.HttpFields;
33 import org.eclipse.jetty.http.HttpVersion;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.junit.runner.RunWith;
46 import mockit.integration.junit4.JMockit;
55 @RunWith(JMockit.class)
56 public class TestHttpRest {
59 public ExpectedException thrown = ExpectedException.none();
64 * @throws java.lang.Exception
67 static HttpClient httpClient;
68 static RestfulOptions options;
71 public static void setUpBeforeClass() throws Exception {
72 options = new RestfulOptions();
73 options.setOption("thread", new Integer(5));
74 options.setOption("maxConnectionPerAddr", new Integer(200));
75 options.setOption("ConnectTimeout", new Integer(500));
76 options.setHost("localhost");
78 httpClient = new HttpClient();
80 Request request = httpClient.newRequest("http://reqres.in/api/users/2");
81 ContentResponse contentResponse = request.send();
82 new MockUp<HttpBaseRest>() {
84 public ContentResponse getResponse() {
85 return contentResponse;
93 * @throws java.lang.Exception
97 public static void tearDownAfterClass() throws Exception {
104 * @throws java.lang.Exception
108 public void setUp() throws Exception {
114 * @throws java.lang.Exception
118 public void tearDown() throws Exception {
128 public void testInitHttpRest() throws Exception {
129 final RestfulOptions options = new RestfulOptions();
130 new MockUp<HttpClient>() {
133 public void doStart() {
136 final HttpRest httpRest = new HttpRest();
137 httpRest.initHttpRest(options);
138 final Field httpClient = HttpBaseRest.class.getDeclaredField("client");
139 httpClient.setAccessible(true);
140 Assert.assertNotNull(httpClient.get(httpRest));
146 // * @throws NoSuchFieldException
147 // * @throws Exception
151 // public void testCreateRestHttpContentExchange() throws NoSuchFieldException, Exception {
152 // final HttpBaseRest httpRest = new HttpRest();
153 // final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
156 // public void callback(final RestfulResponse response) {
161 // public void handleExcepion(final Throwable e) {
166 // final RestHttpContentExchange exchange = httpRest.createRestHttpContentExchange(callback);
167 // assertNotNull(exchange);
168 // final Field callbackField = RestHttpContentExchange.class.getDeclaredField("callback");
169 // assertNotNull(callbackField);
179 public void testGetStringRestfulParametes() throws Exception {
180 final HttpRest httpRest = getHttpRest(options);
181 final RestfulParametes parametes = new RestfulParametes();
182 parametes.put("id", "1234");
183 parametes.put("name", "some-name");
184 parametes.put("address", null);
185 parametes.putHttpContextHeader("Content-Type", "application/json");
186 parametes.putHttpContextHeader("Accept-Encoding", "*/*");
187 final RestfulResponse response = httpRest.get("path/to/service", parametes);
188 assertEquals(200, response.getStatus());
200 public void testGetStringRestfulParametesRestfulOptions() throws Exception {
201 final HttpRest httpRest = getHttpRest(options);
202 final RestfulResponse response = httpRest.get("path/to/service", new RestfulParametes(), options);
203 assertEquals(200, response.getStatus());
214 public void testHeadStringRestfulParametes() throws Exception {
216 final HttpRest httpRest = getHttpRest(options);
217 final RestfulParametes parametes = new RestfulParametes();
218 parametes.put("id", "1234");
219 parametes.put("name", "some-name");
220 parametes.put("address", null);
221 parametes.putHttpContextHeader("Content-Type", "");
222 parametes.putHttpContextHeader("Accept-Encoding", "");
223 final RestfulResponse response = httpRest.head("path/to/service", parametes);
224 assertEquals(200, response.getStatus());
234 public void testHeadStringRestfulParametesRestfulOptions() throws Exception {
236 final HttpRest httpRest = getHttpRest(options);
237 final RestfulParametes parametes = new RestfulParametes();
238 parametes.put("id", "1234");
239 parametes.put("name", "some-name");
240 parametes.put("address", null);
241 parametes.putHttpContextHeader("Content-Type", "");
242 parametes.putHttpContextHeader("Accept-Encoding", "");
243 final RestfulResponse response = httpRest.head("path/to/service", parametes, options);
244 assertEquals(200, response.getStatus());
252 * @throws ServiceException
255 private HttpRest getHttpRest(final RestfulOptions options) throws ServiceException {
256 final HttpRest httpRest = new HttpRest();
258 // new MockUp<HttpClient>() {
261 // public void doStart() {
265 // public void send(final HttpExchange exchange) throws IOException {
268 httpRest.initHttpRest(options);
281 public void testAsyncGetStringRestfulParametesRestfulAsyncCallback() throws Exception {
284 final HttpRest httpRest = getHttpRest(options);
285 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
288 public void callback(final RestfulResponse response) {
293 public void handleExcepion(final Throwable e) {
298 httpRest.asyncGet("path/to/service", new RestfulParametes(), callback);
299 httpRest.asyncGet("path/to/service", new RestfulParametes(), null);
305 * @throws ServiceException
309 public void testAsyncGetStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
312 final HttpRest httpRest = getHttpRest(options);
313 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
316 public void callback(final RestfulResponse response) {
321 public void handleExcepion(final Throwable e) {
326 httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), callback);
327 httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), null);
333 * @throws ServiceException
337 public void testPutStringRestfulParametes() throws ServiceException {
339 final HttpRest httpRest = getHttpRest(options);
340 final RestfulParametes parametes = new RestfulParametes();
341 parametes.put("id", "1234");
342 parametes.put("name", "some-name");
343 parametes.put("address", null);
344 parametes.putHttpContextHeader("Content-Type", "");
345 parametes.putHttpContextHeader("Accept-Encoding", "");
346 final RestfulResponse response = httpRest.put("path/to/service", parametes);
347 assertEquals(200, response.getStatus());
353 * @throws ServiceException
357 public void testPutStringRestfulParametesRestfulOptions() throws ServiceException {
359 final HttpRest httpRest = getHttpRest(options);
360 final RestfulParametes parametes = new RestfulParametes();
361 parametes.put("id", "1234");
362 parametes.put("name", "some-name");
363 parametes.put("address", null);
364 parametes.putHttpContextHeader("Content-Type", "");
365 parametes.putHttpContextHeader("Accept-Encoding", "");
366 final RestfulResponse response = httpRest.put("path/to/service", parametes, null);
367 assertEquals(200, response.getStatus());
373 * @throws ServiceException
377 public void testAsyncPutStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
380 final HttpRest httpRest = getHttpRest(options);
382 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
385 public void callback(final RestfulResponse response) {
390 public void handleExcepion(final Throwable e) {
395 httpRest.asyncPut("path/to/service", new RestfulParametes(), callback);
396 httpRest.asyncPut("path/to/service", new RestfulParametes(), null);
406 public void testAsyncPutStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws Exception {
408 final HttpRest httpRest = getHttpRest(options);
410 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
413 public void callback(final RestfulResponse response) {
418 public void handleExcepion(final Throwable e) {
423 httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), callback);
424 httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), null);
434 public void testAsyncPostStringRestfulParametesRestfulAsyncCallback() throws Exception {
436 options.setRestTimeout(10);
438 final HttpBaseRest httpRest = getHttpRest(options);
439 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
442 public void callback(final RestfulResponse response) {
447 public void handleExcepion(final Throwable e) {
452 httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback);
453 httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null);
459 * @throws ServiceException
463 public void testAsyncPostStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
465 options.setRestTimeout(10);
467 final HttpBaseRest httpRest = getHttpRest(options);
469 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
472 public void callback(final RestfulResponse response) {
477 public void handleExcepion(final Throwable e) {
482 httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback);
483 httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null);
489 * @throws ServiceException
493 public void testDeleteStringRestfulParametes() throws ServiceException {
495 final HttpBaseRest httpRest = getHttpRest(options);
497 final RestfulResponse response = httpRest.delete("path/to/service", null);
498 assertEquals(-1, response.getStatus());
504 * @throws ServiceException
508 public void testDeleteStringRestfulParametesRestfulOptions() throws ServiceException {
511 final HttpBaseRest httpRest = getHttpRest(options);
512 final RestfulParametes parameters = new RestfulParametes();
513 parameters.put("id", "1234");
514 parameters.put("name", "some-name");
515 parameters.put("address", null);
516 parameters.setRawData("{ \"data\"=\"sample JSON data\"");
517 parameters.putHttpContextHeader("Content-Type", "");
518 parameters.putHttpContextHeader("Accept-Encoding", "");
519 final RestfulResponse response = httpRest.delete("path/to/service", parameters, options);
520 assertEquals(200, response.getStatus());
526 * @throws ServiceException
530 public void testAsyncDeleteStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
532 options.setRestTimeout(10);
534 final HttpBaseRest httpRest = getHttpRest(options);
535 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
538 public void callback(final RestfulResponse response) {
543 public void handleExcepion(final Throwable e) {
548 httpRest.asyncDelete("path/to/service", new RestfulParametes(), callback);
549 httpRest.asyncDelete("path/to/service", new RestfulParametes(), null);
555 * @throws ServiceException
559 public void testAsyncDeleteStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
561 options.setRestTimeout(10);
563 final HttpBaseRest httpRest = getHttpRest(options);
565 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
568 public void callback(final RestfulResponse response) {
573 public void handleExcepion(final Throwable e) {
578 httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, callback);
579 httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, null);
585 * @throws ServiceException
589 public void testPatchStringRestfulParametes() throws ServiceException {
591 final HttpBaseRest httpRest = getHttpRest(options);
592 final RestfulParametes parameters = new RestfulParametes();
593 parameters.put("id", "1234");
594 parameters.put("name", "some-name");
595 parameters.put("address", null);
596 parameters.setRawData("{ \"data\"=\"sample JSON data\"");
597 parameters.putHttpContextHeader("Content-Type", "");
598 parameters.putHttpContextHeader("Accept-Encoding", "");
599 final RestfulResponse response = httpRest.patch("path/to/service", parameters);
600 assertEquals(200, response.getStatus());
606 * @throws ServiceException
610 public void testPatchStringRestfulParametesRestfulOptions() throws ServiceException {
612 final HttpBaseRest httpRest = getHttpRest(options);
613 final RestfulParametes parameters = new RestfulParametes();
614 parameters.put("id", "1234");
615 parameters.put("name", "some-name");
616 parameters.put("address", null);
617 parameters.setRawData("{ \"data\"=\"sample JSON data\"");
618 parameters.putHttpContextHeader("Content-Type", "");
619 parameters.putHttpContextHeader("Accept-Encoding", "");
620 final RestfulResponse response = httpRest.patch("path/to/service", parameters, options);
621 assertEquals(200, response.getStatus());
627 * @throws ServiceException
631 public void testAsyncPatchStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
633 options.setRestTimeout(10);
635 final HttpBaseRest httpRest = getHttpRest(options);
637 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
640 public void callback(final RestfulResponse response) {
645 public void handleExcepion(final Throwable e) {
650 httpRest.asyncPatch("path/to/service", new RestfulParametes(), callback);
651 httpRest.asyncPatch("path/to/service", new RestfulParametes(), null);
657 * @throws ServiceException
661 public void testAsyncPatchStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
663 options.setRestTimeout(10);
665 final HttpBaseRest httpRest = getHttpRest(options);
667 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
670 public void callback(final RestfulResponse response) {
675 public void handleExcepion(final Throwable e) {
680 httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, callback);
681 httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, null);