2 * Copyright 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.assertNotNull;
22 import java.io.IOException;
23 import java.io.UnsupportedEncodingException;
24 import java.lang.reflect.Field;
25 import java.net.URLEncoder;
27 import org.eclipse.jetty.client.HttpClient;
28 import org.eclipse.jetty.client.HttpExchange;
29 import org.junit.After;
30 import org.junit.AfterClass;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.junit.runner.RunWith;
38 import org.openo.baseservice.remoteservice.exception.ServiceException;
43 import mockit.NonStrictExpectations;
44 import mockit.integration.junit4.JMockit;
52 * @version 13-Jun-2016
54 @RunWith(JMockit.class)
55 public class TestHttpRest {
58 public ExpectedException thrown = ExpectedException.none();
63 * @throws java.lang.Exception
67 public static void setUpBeforeClass() throws Exception {
73 * @throws java.lang.Exception
77 public static void tearDownAfterClass() throws Exception {
83 * @throws java.lang.Exception
87 public void setUp() throws Exception {
93 * @throws java.lang.Exception
97 public void tearDown() throws Exception {
107 public void testInitHttpRest() throws Exception {
108 final RestfulOptions options = new RestfulOptions();
109 new MockUp<HttpClient>() {
112 public void doStart() {
113 System.out.println("started");
116 final HttpRest httpRest = new HttpRest();
117 httpRest.initHttpRest(options);
118 final Field httpClient = HttpBaseRest.class.getDeclaredField("client");
119 httpClient.setAccessible(true);
120 Assert.assertNotNull(httpClient.get(httpRest));
130 public void testInitHttpRestExcpetion() throws Exception {
131 final RestfulOptions options = new RestfulOptions();
132 new MockUp<HttpClient>() {
135 public void doStart() throws Exception {
136 throw new Exception();
139 final HttpRest httpRest = new HttpRest();
140 thrown.expect(ServiceException.class);
141 thrown.expectMessage("http client init failed.");
142 httpRest.initHttpRest(options);
143 final Field httpClient = HttpRest.class.getDeclaredField("client");
144 httpClient.setAccessible(true);
145 Assert.assertNull(httpClient.get(httpRest));
146 System.out.println("finished");
156 public void testInitHttpRestNull() throws Exception {
157 final HttpRest httpRest = new HttpRest();
158 thrown.expect(ServiceException.class);
159 thrown.expectMessage("option is null.");
160 httpRest.initHttpRest(null);
166 * @throws NoSuchFieldException
171 public void testCreateRestHttpContentExchange() throws NoSuchFieldException, Exception {
172 final HttpBaseRest httpRest = new HttpRest();
173 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
176 public void callback(final RestfulResponse response) {
181 public void handleExcepion(final Throwable e) {
186 final RestHttpContentExchange exchange = httpRest.createRestHttpContentExchange(callback);
187 assertNotNull(exchange);
188 final Field callbackField = RestHttpContentExchange.class.getDeclaredField("callback");
189 assertNotNull(callbackField);
199 public void testGetStringRestfulParametes() throws Exception {
200 final RestfulOptions options = new RestfulOptions();
202 final HttpRest httpRest = getHttpRest(options);
203 new MockUp<RestHttpContentExchange>() {
206 public int waitForDone() {
207 System.out.println("waitForDone");
208 return HttpExchange.STATUS_COMPLETED;
212 public RestfulResponse getResponse() throws IOException {
213 final RestfulResponse response = new RestfulResponse();
214 response.setStatus(HttpExchange.STATUS_COMPLETED);
219 final RestfulParametes parametes = new RestfulParametes();
220 parametes.put("id", "1234");
221 parametes.put("name", "some-name");
222 parametes.put("address", null);
223 parametes.putHttpContextHeader("Content-Type", "application/json");
224 parametes.putHttpContextHeader("Accept-Encoding", "*/*");
225 final RestfulResponse response = httpRest.get("path/to/service", parametes);
226 assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
237 public void testGetStringRestfulParametesRestfulOptions() throws Exception {
238 final RestfulOptions options = new RestfulOptions();
240 final HttpRest httpRest = getHttpRest(options);
241 new MockUp<RestHttpContentExchange>() {
244 public int waitForDone() {
245 System.out.println("waitForDone");
246 return HttpExchange.STATUS_COMPLETED;
250 public RestfulResponse getResponse() throws IOException {
251 final RestfulResponse response = new RestfulResponse();
252 response.setStatus(HttpExchange.STATUS_COMPLETED);
257 final RestfulResponse response = httpRest.get("path/to/service", new RestfulParametes(), options);
258 assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
268 public void testGetStringRestfulParametesEncodeError() throws Exception {
269 final RestfulOptions options = new RestfulOptions();
271 final HttpRest httpRest = getHttpRest(options);
272 new MockUp<RestHttpContentExchange>() {
275 public int waitForDone() {
276 System.out.println("waitForDone");
277 return HttpExchange.STATUS_COMPLETED;
281 public RestfulResponse getResponse() throws IOException {
282 final RestfulResponse response = new RestfulResponse();
283 response.setStatus(HttpExchange.STATUS_COMPLETED);
289 new NonStrictExpectations() {
295 URLEncoder.encode(anyString, RestfulClientConst.ENCODING);
296 result = new UnsupportedEncodingException();
301 thrown.expect(ServiceException.class);
302 thrown.expectMessage("Broken VM does not support");
304 final RestfulParametes parametes = new RestfulParametes();
305 parametes.put("id", "1234");
306 parametes.put("name", "some-name");
307 parametes.put("address", null);
308 parametes.putHttpContextHeader("Content-Type", "application/json");
309 parametes.putHttpContextHeader("Accept-Encoding", "*/*");
310 final RestfulResponse response = httpRest.get("path/to/service", parametes);
311 assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
322 public void testHeadStringRestfulParametes() throws Exception {
323 final RestfulOptions options = new RestfulOptions();
325 final HttpRest httpRest = getHttpRest(options);
326 new MockUp<RestHttpContentExchange>() {
329 public int waitForDone() {
330 System.out.println("waitForDone");
331 return HttpExchange.STATUS_COMPLETED;
335 public RestfulResponse getResponse() throws IOException {
336 final RestfulResponse response = new RestfulResponse();
337 response.setStatus(HttpExchange.STATUS_COMPLETED);
342 final RestfulParametes parametes = new RestfulParametes();
343 parametes.put("id", "1234");
344 parametes.put("name", "some-name");
345 parametes.put("address", null);
346 parametes.putHttpContextHeader("Content-Type", "");
347 parametes.putHttpContextHeader("Accept-Encoding", "");
348 final RestfulResponse response = httpRest.head("path/to/service", parametes);
349 assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
359 public void testHeadStringRestfulParametesRestfulOptions() throws Exception {
360 final RestfulOptions options = new RestfulOptions();
362 final HttpRest httpRest = getHttpRest(options);
363 new MockUp<RestHttpContentExchange>() {
366 public int waitForDone() {
367 System.out.println("waitForDone");
368 return HttpExchange.STATUS_COMPLETED;
372 public RestfulResponse getResponse() throws IOException {
373 final RestfulResponse response = new RestfulResponse();
374 response.setStatus(HttpExchange.STATUS_COMPLETED);
379 final RestfulParametes parametes = new RestfulParametes();
380 parametes.put("id", "1234");
381 parametes.put("name", "some-name");
382 parametes.put("address", null);
383 parametes.putHttpContextHeader("Content-Type", "");
384 parametes.putHttpContextHeader("Accept-Encoding", "");
385 final RestfulResponse response = httpRest.head("path/to/service", parametes, options);
386 assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
394 * @throws ServiceException
397 private HttpRest getHttpRest(final RestfulOptions options) throws ServiceException {
398 final HttpRest httpRest = new HttpRest();
400 new MockUp<HttpClient>() {
403 public void doStart() {
404 System.out.println("started");
408 public void send(final HttpExchange exchange) throws IOException {
409 System.out.println("send");
412 httpRest.initHttpRest(options);
425 public void testAsyncGetStringRestfulParametesRestfulAsyncCallback() throws Exception {
426 final RestfulOptions options = new RestfulOptions();
428 final HttpRest httpRest = getHttpRest(options);
429 new MockUp<RestHttpContentExchange>() {
432 public int waitForDone() {
433 System.out.println("waitForDone");
434 return HttpExchange.STATUS_COMPLETED;
438 public RestfulResponse getResponse() throws IOException {
439 final RestfulResponse response = new RestfulResponse();
440 response.setStatus(HttpExchange.STATUS_COMPLETED);
446 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
449 public void callback(final RestfulResponse response) {
450 System.out.println("callback called.");
455 public void handleExcepion(final Throwable e) {
457 System.out.println("handleExcepion called.");
461 httpRest.asyncGet("path/to/service", new RestfulParametes(), callback);
462 httpRest.asyncGet("path/to/service", new RestfulParametes(), null);
468 * @throws ServiceException
472 public void testAsyncGetStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
473 final RestfulOptions options = new RestfulOptions();
475 final HttpRest httpRest = getHttpRest(options);
476 new MockUp<RestHttpContentExchange>() {
479 public int waitForDone() {
480 System.out.println("waitForDone");
481 return HttpExchange.STATUS_COMPLETED;
485 public RestfulResponse getResponse() throws IOException {
486 final RestfulResponse response = new RestfulResponse();
487 response.setStatus(HttpExchange.STATUS_COMPLETED);
493 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
496 public void callback(final RestfulResponse response) {
497 System.out.println("callback called.");
502 public void handleExcepion(final Throwable e) {
504 System.out.println("handleExcepion called.");
508 httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), callback);
509 httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), null);
515 * @throws ServiceException
519 public void testPutStringRestfulParametes() throws ServiceException {
520 final RestfulOptions options = new RestfulOptions();
522 final HttpRest httpRest = getHttpRest(options);
523 new MockUp<RestHttpContentExchange>() {
526 public int waitForDone() {
527 System.out.println("waitForDone");
528 return HttpExchange.STATUS_COMPLETED;
532 public RestfulResponse getResponse() throws IOException {
533 final RestfulResponse response = new RestfulResponse();
534 response.setStatus(HttpExchange.STATUS_COMPLETED);
539 final RestfulParametes parametes = new RestfulParametes();
540 parametes.put("id", "1234");
541 parametes.put("name", "some-name");
542 parametes.put("address", null);
543 parametes.putHttpContextHeader("Content-Type", "");
544 parametes.putHttpContextHeader("Accept-Encoding", "");
545 final RestfulResponse response = httpRest.put("path/to/service", parametes);
546 assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
552 * @throws ServiceException
556 public void testPutStringRestfulParametesRestfulOptions() throws ServiceException {
558 final RestfulOptions options = new RestfulOptions();
560 final HttpRest httpRest = getHttpRest(options);
561 new MockUp<RestHttpContentExchange>() {
564 public int waitForDone() {
565 System.out.println("waitForDone");
566 return HttpExchange.STATUS_COMPLETED;
570 public RestfulResponse getResponse() throws IOException {
571 final RestfulResponse response = new RestfulResponse();
572 response.setStatus(HttpExchange.STATUS_COMPLETED);
577 final RestfulParametes parametes = new RestfulParametes();
578 parametes.put("id", "1234");
579 parametes.put("name", "some-name");
580 parametes.put("address", null);
581 parametes.putHttpContextHeader("Content-Type", "");
582 parametes.putHttpContextHeader("Accept-Encoding", "");
583 final RestfulResponse response = httpRest.put("path/to/service", parametes, null);
584 assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
590 * @throws ServiceException
594 public void testAsyncPutStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
595 final RestfulOptions options = new RestfulOptions();
597 final HttpRest httpRest = getHttpRest(options);
598 new MockUp<RestHttpContentExchange>() {
601 public int waitForDone() {
602 System.out.println("waitForDone");
603 return HttpExchange.STATUS_COMPLETED;
607 public RestfulResponse getResponse() throws IOException {
608 final RestfulResponse response = new RestfulResponse();
609 response.setStatus(HttpExchange.STATUS_COMPLETED);
615 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
618 public void callback(final RestfulResponse response) {
619 System.out.println("callback called.");
624 public void handleExcepion(final Throwable e) {
626 System.out.println("handleExcepion called.");
630 httpRest.asyncPut("path/to/service", new RestfulParametes(), callback);
631 httpRest.asyncPut("path/to/service", new RestfulParametes(), null);
641 public void testAsyncPutStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws Exception {
642 final RestfulOptions options = new RestfulOptions();
644 final HttpRest httpRest = getHttpRest(options);
645 new MockUp<RestHttpContentExchange>() {
648 public int waitForDone() {
649 System.out.println("waitForDone");
650 return HttpExchange.STATUS_COMPLETED;
654 public RestfulResponse getResponse() throws IOException {
655 final RestfulResponse response = new RestfulResponse();
656 response.setStatus(HttpExchange.STATUS_COMPLETED);
662 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
665 public void callback(final RestfulResponse response) {
666 System.out.println("callback called.");
671 public void handleExcepion(final Throwable e) {
673 System.out.println("handleExcepion called.");
677 httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), callback);
678 httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), null);
688 public void testPostStringRestfulParametes() throws Exception {
689 final RestfulOptions options = new RestfulOptions();
691 final HttpBaseRest httpRest = getHttpRest(options);
692 new MockUp<RestHttpContentExchange>() {
695 public int waitForDone() {
696 System.out.println("waitForDone");
697 return HttpExchange.STATUS_EXPIRED;
701 public RestfulResponse getResponse() throws IOException {
702 final RestfulResponse response = new RestfulResponse();
703 response.setStatus(HttpExchange.STATUS_EXPIRED);
708 final RestfulParametes parameters = new RestfulParametes();
709 parameters.put("id", "1234");
710 parameters.put("name", "some-name");
711 parameters.put("address", null);
712 parameters.putHttpContextHeader("Content-Type", "");
713 parameters.putHttpContextHeader("Accept-Encoding", "");
715 thrown.expect(ServiceException.class);
716 thrown.expectMessage("request is expierd");
717 final RestfulResponse response = httpRest.post("http://localhost:80/path/to/service", parameters);
718 assertEquals(HttpExchange.STATUS_EXPIRED, response.getStatus());
724 * @throws ServiceException
728 public void testPostStringRestfulParametesRestfulOptions() throws ServiceException {
729 final RestfulOptions options = new RestfulOptions();
731 final HttpBaseRest httpRest = getHttpRest(options);
732 new MockUp<RestHttpContentExchange>() {
735 public int waitForDone() {
736 System.out.println("waitForDone" + HttpExchange.STATUS_EXCEPTED);
737 return HttpExchange.STATUS_EXCEPTED;
741 public RestfulResponse getResponse() throws IOException {
742 final RestfulResponse response = new RestfulResponse();
743 response.setStatus(HttpExchange.STATUS_COMPLETED);
748 final RestfulParametes parameters = new RestfulParametes();
749 parameters.put("id", "1234");
750 parameters.put("name", "some-name");
751 parameters.put("address", null);
752 parameters.setRawData("{ \"data\"=\"sample JSON data\"");
753 parameters.putHttpContextHeader("Content-Type", "");
754 parameters.putHttpContextHeader("Accept-Encoding", "");
755 thrown.expect(ServiceException.class);
756 thrown.expectMessage("request is exception");
757 final RestfulResponse response = httpRest.post("path/to/service", parameters, null);
758 assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
768 public void testAsyncPostStringRestfulParametesRestfulAsyncCallback() throws Exception {
769 final RestfulOptions options = new RestfulOptions();
770 options.setRestTimeout(10);
772 final HttpBaseRest httpRest = getHttpRest(options);
773 new MockUp<RestHttpContentExchange>() {
776 public int waitForDone() {
777 System.out.println("waitForDone:" + HttpExchange.STATUS_EXCEPTED);
782 public RestfulResponse getResponse() throws IOException {
783 final RestfulResponse response = new RestfulResponse();
784 response.setStatus(HttpExchange.STATUS_EXCEPTED);
790 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
793 public void callback(final RestfulResponse response) {
794 System.out.println("callback called.");
799 public void handleExcepion(final Throwable e) {
801 System.out.println("handleExcepion called.");
805 httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback);
806 httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null);
812 * @throws ServiceException
816 public void testAsyncPostStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
817 final RestfulOptions options = new RestfulOptions();
818 options.setRestTimeout(10);
820 final HttpBaseRest httpRest = getHttpRest(options);
821 new MockUp<RestHttpContentExchange>() {
824 public int waitForDone() {
825 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
826 return HttpExchange.STATUS_COMPLETED;
830 public RestfulResponse getResponse() throws IOException {
831 final RestfulResponse response = new RestfulResponse();
832 response.setStatus(HttpExchange.STATUS_COMPLETED);
838 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
841 public void callback(final RestfulResponse response) {
842 System.out.println("callback called.");
847 public void handleExcepion(final Throwable e) {
849 System.out.println("handleExcepion called.");
853 httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback);
854 httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null);
860 * @throws ServiceException
864 public void testDeleteStringRestfulParametes() throws ServiceException {
865 final RestfulOptions options = new RestfulOptions();
867 final HttpBaseRest httpRest = getHttpRest(options);
869 final RestfulResponse response = httpRest.delete("path/to/service", null);
870 assertEquals(-1, response.getStatus());
876 * @throws ServiceException
880 public void testDeleteStringRestfulParametesRestfulOptions() throws ServiceException {
881 final RestfulOptions options = new RestfulOptions();
883 final HttpBaseRest httpRest = getHttpRest(options);
884 new MockUp<RestHttpContentExchange>() {
887 public int waitForDone() {
888 System.out.println("waitForDone" + HttpExchange.STATUS_COMPLETED);
889 return HttpExchange.STATUS_COMPLETED;
893 public RestfulResponse getResponse() throws IOException {
894 final RestfulResponse response = new RestfulResponse();
895 response.setStatus(HttpExchange.STATUS_COMPLETED);
900 final RestfulParametes parameters = new RestfulParametes();
901 parameters.put("id", "1234");
902 parameters.put("name", "some-name");
903 parameters.put("address", null);
904 parameters.setRawData("{ \"data\"=\"sample JSON data\"");
905 parameters.putHttpContextHeader("Content-Type", "");
906 parameters.putHttpContextHeader("Accept-Encoding", "");
907 final RestfulResponse response = httpRest.delete("path/to/service", parameters, options);
908 assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
914 * @throws ServiceException
918 public void testAsyncDeleteStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
919 final RestfulOptions options = new RestfulOptions();
920 options.setRestTimeout(10);
922 final HttpBaseRest httpRest = getHttpRest(options);
923 new MockUp<RestHttpContentExchange>() {
926 public int waitForDone() {
927 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
928 return HttpExchange.STATUS_COMPLETED;
932 public RestfulResponse getResponse() throws IOException {
933 final RestfulResponse response = new RestfulResponse();
934 response.setStatus(HttpExchange.STATUS_COMPLETED);
940 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
943 public void callback(final RestfulResponse response) {
944 System.out.println("callback called.");
949 public void handleExcepion(final Throwable e) {
951 System.out.println("handleExcepion called.");
955 httpRest.asyncDelete("path/to/service", new RestfulParametes(), callback);
956 httpRest.asyncDelete("path/to/service", new RestfulParametes(), null);
962 * @throws ServiceException
966 public void testAsyncDeleteStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
967 final RestfulOptions options = new RestfulOptions();
968 options.setRestTimeout(10);
970 final HttpBaseRest httpRest = getHttpRest(options);
971 new MockUp<RestHttpContentExchange>() {
974 public int waitForDone() {
975 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
976 return HttpExchange.STATUS_COMPLETED;
980 public RestfulResponse getResponse() throws IOException {
981 final RestfulResponse response = new RestfulResponse();
982 response.setStatus(HttpExchange.STATUS_COMPLETED);
988 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
991 public void callback(final RestfulResponse response) {
992 System.out.println("callback called.");
997 public void handleExcepion(final Throwable e) {
999 System.out.println("handleExcepion called.");
1003 httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, callback);
1004 httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, null);
1010 * @throws ServiceException
1014 public void testPatchStringRestfulParametes() throws ServiceException {
1015 final RestfulOptions options = new RestfulOptions();
1017 final HttpBaseRest httpRest = getHttpRest(options);
1018 new MockUp<RestHttpContentExchange>() {
1021 public int waitForDone() {
1022 System.out.println("waitForDone" + HttpExchange.STATUS_COMPLETED);
1023 return HttpExchange.STATUS_COMPLETED;
1027 public RestfulResponse getResponse() throws IOException {
1028 final RestfulResponse response = new RestfulResponse();
1029 response.setStatus(HttpExchange.STATUS_COMPLETED);
1034 final RestfulParametes parameters = new RestfulParametes();
1035 parameters.put("id", "1234");
1036 parameters.put("name", "some-name");
1037 parameters.put("address", null);
1038 parameters.setRawData("{ \"data\"=\"sample JSON data\"");
1039 parameters.putHttpContextHeader("Content-Type", "");
1040 parameters.putHttpContextHeader("Accept-Encoding", "");
1041 final RestfulResponse response = httpRest.patch("path/to/service", parameters);
1042 assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
1048 * @throws ServiceException
1052 public void testPatchStringRestfulParametesRestfulOptions() throws ServiceException {
1053 final RestfulOptions options = new RestfulOptions();
1055 final HttpBaseRest httpRest = getHttpRest(options);
1056 new MockUp<RestHttpContentExchange>() {
1059 public int waitForDone() {
1060 System.out.println("waitForDone" + HttpExchange.STATUS_COMPLETED);
1061 return HttpExchange.STATUS_COMPLETED;
1065 public RestfulResponse getResponse() throws IOException {
1066 final RestfulResponse response = new RestfulResponse();
1067 response.setStatus(HttpExchange.STATUS_COMPLETED);
1072 final RestfulParametes parameters = new RestfulParametes();
1073 parameters.put("id", "1234");
1074 parameters.put("name", "some-name");
1075 parameters.put("address", null);
1076 parameters.setRawData("{ \"data\"=\"sample JSON data\"");
1077 parameters.putHttpContextHeader("Content-Type", "");
1078 parameters.putHttpContextHeader("Accept-Encoding", "");
1079 final RestfulResponse response = httpRest.patch("path/to/service", parameters, options);
1080 assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
1086 * @throws ServiceException
1090 public void testAsyncPatchStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
1091 final RestfulOptions options = new RestfulOptions();
1092 options.setRestTimeout(10);
1094 final HttpBaseRest httpRest = getHttpRest(options);
1095 new MockUp<RestHttpContentExchange>() {
1098 public int waitForDone() {
1099 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
1100 return HttpExchange.STATUS_COMPLETED;
1104 public RestfulResponse getResponse() throws IOException {
1105 final RestfulResponse response = new RestfulResponse();
1106 response.setStatus(HttpExchange.STATUS_COMPLETED);
1112 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
1115 public void callback(final RestfulResponse response) {
1116 System.out.println("callback called.");
1121 public void handleExcepion(final Throwable e) {
1123 System.out.println("handleExcepion called.");
1127 httpRest.asyncPatch("path/to/service", new RestfulParametes(), callback);
1128 httpRest.asyncPatch("path/to/service", new RestfulParametes(), null);
1134 * @throws ServiceException
1138 public void testAsyncPatchStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
1139 final RestfulOptions options = new RestfulOptions();
1140 options.setRestTimeout(10);
1142 final HttpBaseRest httpRest = getHttpRest(options);
1143 new MockUp<RestHttpContentExchange>() {
1146 public int waitForDone() {
1147 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
1148 return HttpExchange.STATUS_COMPLETED;
1152 public RestfulResponse getResponse() throws IOException {
1153 final RestfulResponse response = new RestfulResponse();
1154 response.setStatus(HttpExchange.STATUS_COMPLETED);
1160 final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
1163 public void callback(final RestfulResponse response) {
1164 System.out.println("callback called.");
1169 public void handleExcepion(final Throwable e) {
1171 System.out.println("handleExcepion called.");
1175 httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, callback);
1176 httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, null);