Modify license information.
[vfc/nfvo/wfengine.git] / rest-client / src / test / java / org / openo / baseservice / roa / util / restclient / TestHttpRest.java
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openo.baseservice.roa.util.restclient;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21
22 import java.io.IOException;
23 import java.io.UnsupportedEncodingException;
24 import java.lang.reflect.Field;
25 import java.net.URLEncoder;
26
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;
39
40 import mockit.Mock;
41 import mockit.MockUp;
42 import mockit.Mocked;
43 import mockit.NonStrictExpectations;
44 import mockit.integration.junit4.JMockit;
45
46 /**
47  * <br/>
48  * <p>
49  * </p>
50  * 
51  * @author
52  * @version   13-Jun-2016
53  */
54 @RunWith(JMockit.class)
55 public class TestHttpRest {
56
57     @Rule
58     public ExpectedException thrown = ExpectedException.none();
59
60     /**
61      * <br/>
62      * 
63      * @throws java.lang.Exception
64      * @since  
65      */
66     @BeforeClass
67     public static void setUpBeforeClass() throws Exception {
68     }
69
70     /**
71      * <br/>
72      * 
73      * @throws java.lang.Exception
74      * @since  
75      */
76     @AfterClass
77     public static void tearDownAfterClass() throws Exception {
78     }
79
80     /**
81      * <br/>
82      * 
83      * @throws java.lang.Exception
84      * @since  
85      */
86     @Before
87     public void setUp() throws Exception {
88     }
89
90     /**
91      * <br/>
92      * 
93      * @throws java.lang.Exception
94      * @since  
95      */
96     @After
97     public void tearDown() throws Exception {
98     }
99
100     /**
101      * <br/>
102      * 
103      * @throws Exception
104      * @since  
105      */
106     @Test
107     public void testInitHttpRest() throws Exception {
108         final RestfulOptions options = new RestfulOptions();
109         new MockUp<HttpClient>() {
110
111             @Mock
112             public void doStart() {
113                 System.out.println("started");
114             }
115         };
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));
121     }
122
123     /**
124      * <br/>
125      * 
126      * @throws Exception
127      * @since  
128      */
129     @Test
130     public void testInitHttpRestExcpetion() throws Exception {
131         final RestfulOptions options = new RestfulOptions();
132         new MockUp<HttpClient>() {
133
134             @Mock
135             public void doStart() throws Exception {
136                 throw new Exception();
137             }
138         };
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");
147     }
148
149     /**
150      * <br/>
151      * 
152      * @throws Exception
153      * @since  
154      */
155     @Test
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);
161     }
162
163     /**
164      * <br/>
165      * 
166      * @throws NoSuchFieldException
167      * @throws Exception
168      * @since  
169      */
170     @Test
171     public void testCreateRestHttpContentExchange() throws NoSuchFieldException, Exception {
172         final HttpBaseRest httpRest = new HttpRest();
173         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
174
175             @Override
176             public void callback(final RestfulResponse response) {
177
178             }
179
180             @Override
181             public void handleExcepion(final Throwable e) {
182
183             }
184
185         };
186         final RestHttpContentExchange exchange = httpRest.createRestHttpContentExchange(callback);
187         assertNotNull(exchange);
188         final Field callbackField = RestHttpContentExchange.class.getDeclaredField("callback");
189         assertNotNull(callbackField);
190     }
191
192     /**
193      * <br/>
194      * 
195      * @throws Exception
196      * @since  
197      */
198     @Test
199     public void testGetStringRestfulParametes() throws Exception {
200         final RestfulOptions options = new RestfulOptions();
201
202         final HttpRest httpRest = getHttpRest(options);
203         new MockUp<RestHttpContentExchange>() {
204
205             @Mock
206             public int waitForDone() {
207                 System.out.println("waitForDone");
208                 return HttpExchange.STATUS_COMPLETED;
209             }
210
211             @Mock
212             public RestfulResponse getResponse() throws IOException {
213                 final RestfulResponse response = new RestfulResponse();
214                 response.setStatus(HttpExchange.STATUS_COMPLETED);
215                 return response;
216             }
217
218         };
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());
227
228     }
229
230     /**
231      * <br/>
232      * 
233      * @throws Exception
234      * @since  
235      */
236     @Test
237     public void testGetStringRestfulParametesRestfulOptions() throws Exception {
238         final RestfulOptions options = new RestfulOptions();
239
240         final HttpRest httpRest = getHttpRest(options);
241         new MockUp<RestHttpContentExchange>() {
242
243             @Mock
244             public int waitForDone() {
245                 System.out.println("waitForDone");
246                 return HttpExchange.STATUS_COMPLETED;
247             }
248
249             @Mock
250             public RestfulResponse getResponse() throws IOException {
251                 final RestfulResponse response = new RestfulResponse();
252                 response.setStatus(HttpExchange.STATUS_COMPLETED);
253                 return response;
254             }
255
256         };
257         final RestfulResponse response = httpRest.get("path/to/service", new RestfulParametes(), options);
258         assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
259     }
260
261     /**
262      * <br/>
263      * 
264      * @throws Exception
265      * @since  
266      */
267     @Test
268     public void testGetStringRestfulParametesEncodeError() throws Exception {
269         final RestfulOptions options = new RestfulOptions();
270
271         final HttpRest httpRest = getHttpRest(options);
272         new MockUp<RestHttpContentExchange>() {
273
274             @Mock
275             public int waitForDone() {
276                 System.out.println("waitForDone");
277                 return HttpExchange.STATUS_COMPLETED;
278             }
279
280             @Mock
281             public RestfulResponse getResponse() throws IOException {
282                 final RestfulResponse response = new RestfulResponse();
283                 response.setStatus(HttpExchange.STATUS_COMPLETED);
284                 return response;
285             }
286
287         };
288
289         new NonStrictExpectations() {
290
291             @Mocked
292             URLEncoder encoder;
293
294             {
295                 URLEncoder.encode(anyString, RestfulClientConst.ENCODING);
296                 result = new UnsupportedEncodingException();
297             }
298
299         };
300
301         thrown.expect(ServiceException.class);
302         thrown.expectMessage("Broken VM does not support");
303
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());
312
313     }
314
315     /**
316      * <br/>
317      * 
318      * @throws Exception
319      * @since  
320      */
321     @Test
322     public void testHeadStringRestfulParametes() throws Exception {
323         final RestfulOptions options = new RestfulOptions();
324
325         final HttpRest httpRest = getHttpRest(options);
326         new MockUp<RestHttpContentExchange>() {
327
328             @Mock
329             public int waitForDone() {
330                 System.out.println("waitForDone");
331                 return HttpExchange.STATUS_COMPLETED;
332             }
333
334             @Mock
335             public RestfulResponse getResponse() throws IOException {
336                 final RestfulResponse response = new RestfulResponse();
337                 response.setStatus(HttpExchange.STATUS_COMPLETED);
338                 return response;
339             }
340
341         };
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());
350     }
351
352     /**
353      * <br/>
354      * 
355      * @throws Exception
356      * @since  
357      */
358     @Test
359     public void testHeadStringRestfulParametesRestfulOptions() throws Exception {
360         final RestfulOptions options = new RestfulOptions();
361
362         final HttpRest httpRest = getHttpRest(options);
363         new MockUp<RestHttpContentExchange>() {
364
365             @Mock
366             public int waitForDone() {
367                 System.out.println("waitForDone");
368                 return HttpExchange.STATUS_COMPLETED;
369             }
370
371             @Mock
372             public RestfulResponse getResponse() throws IOException {
373                 final RestfulResponse response = new RestfulResponse();
374                 response.setStatus(HttpExchange.STATUS_COMPLETED);
375                 return response;
376             }
377
378         };
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());
387     }
388
389     /**
390      * <br/>
391      * 
392      * @param options
393      * @return
394      * @throws ServiceException
395      * @since  
396      */
397     private HttpRest getHttpRest(final RestfulOptions options) throws ServiceException {
398         final HttpRest httpRest = new HttpRest();
399         {
400             new MockUp<HttpClient>() {
401
402                 @Mock
403                 public void doStart() {
404                     System.out.println("started");
405                 }
406
407                 @Mock
408                 public void send(final HttpExchange exchange) throws IOException {
409                     System.out.println("send");
410                 }
411             };
412             httpRest.initHttpRest(options);
413
414         }
415         return httpRest;
416     }
417
418     /**
419      * <br/>
420      * 
421      * @throws Exception
422      * @since  
423      */
424     @Test
425     public void testAsyncGetStringRestfulParametesRestfulAsyncCallback() throws Exception {
426         final RestfulOptions options = new RestfulOptions();
427
428         final HttpRest httpRest = getHttpRest(options);
429         new MockUp<RestHttpContentExchange>() {
430
431             @Mock
432             public int waitForDone() {
433                 System.out.println("waitForDone");
434                 return HttpExchange.STATUS_COMPLETED;
435             }
436
437             @Mock
438             public RestfulResponse getResponse() throws IOException {
439                 final RestfulResponse response = new RestfulResponse();
440                 response.setStatus(HttpExchange.STATUS_COMPLETED);
441                 return response;
442             }
443
444         };
445
446         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
447
448             @Override
449             public void callback(final RestfulResponse response) {
450                 System.out.println("callback called.");
451
452             }
453
454             @Override
455             public void handleExcepion(final Throwable e) {
456
457                 System.out.println("handleExcepion called.");
458             }
459
460         };
461         httpRest.asyncGet("path/to/service", new RestfulParametes(), callback);
462         httpRest.asyncGet("path/to/service", new RestfulParametes(), null);
463     }
464
465     /**
466      * <br/>
467      * 
468      * @throws ServiceException
469      * @since  
470      */
471     @Test
472     public void testAsyncGetStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
473         final RestfulOptions options = new RestfulOptions();
474
475         final HttpRest httpRest = getHttpRest(options);
476         new MockUp<RestHttpContentExchange>() {
477
478             @Mock
479             public int waitForDone() {
480                 System.out.println("waitForDone");
481                 return HttpExchange.STATUS_COMPLETED;
482             }
483
484             @Mock
485             public RestfulResponse getResponse() throws IOException {
486                 final RestfulResponse response = new RestfulResponse();
487                 response.setStatus(HttpExchange.STATUS_COMPLETED);
488                 return response;
489             }
490
491         };
492
493         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
494
495             @Override
496             public void callback(final RestfulResponse response) {
497                 System.out.println("callback called.");
498
499             }
500
501             @Override
502             public void handleExcepion(final Throwable e) {
503
504                 System.out.println("handleExcepion called.");
505             }
506
507         };
508         httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), callback);
509         httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), null);
510     }
511
512     /**
513      * <br/>
514      * 
515      * @throws ServiceException
516      * @since  
517      */
518     @Test
519     public void testPutStringRestfulParametes() throws ServiceException {
520         final RestfulOptions options = new RestfulOptions();
521
522         final HttpRest httpRest = getHttpRest(options);
523         new MockUp<RestHttpContentExchange>() {
524
525             @Mock
526             public int waitForDone() {
527                 System.out.println("waitForDone");
528                 return HttpExchange.STATUS_COMPLETED;
529             }
530
531             @Mock
532             public RestfulResponse getResponse() throws IOException {
533                 final RestfulResponse response = new RestfulResponse();
534                 response.setStatus(HttpExchange.STATUS_COMPLETED);
535                 return response;
536             }
537
538         };
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());
547     }
548
549     /**
550      * <br/>
551      * 
552      * @throws ServiceException
553      * @since  
554      */
555     @Test
556     public void testPutStringRestfulParametesRestfulOptions() throws ServiceException {
557
558         final RestfulOptions options = new RestfulOptions();
559
560         final HttpRest httpRest = getHttpRest(options);
561         new MockUp<RestHttpContentExchange>() {
562
563             @Mock
564             public int waitForDone() {
565                 System.out.println("waitForDone");
566                 return HttpExchange.STATUS_COMPLETED;
567             }
568
569             @Mock
570             public RestfulResponse getResponse() throws IOException {
571                 final RestfulResponse response = new RestfulResponse();
572                 response.setStatus(HttpExchange.STATUS_COMPLETED);
573                 return response;
574             }
575
576         };
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());
585     }
586
587     /**
588      * <br/>
589      * 
590      * @throws ServiceException
591      * @since  
592      */
593     @Test
594     public void testAsyncPutStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
595         final RestfulOptions options = new RestfulOptions();
596
597         final HttpRest httpRest = getHttpRest(options);
598         new MockUp<RestHttpContentExchange>() {
599
600             @Mock
601             public int waitForDone() {
602                 System.out.println("waitForDone");
603                 return HttpExchange.STATUS_COMPLETED;
604             }
605
606             @Mock
607             public RestfulResponse getResponse() throws IOException {
608                 final RestfulResponse response = new RestfulResponse();
609                 response.setStatus(HttpExchange.STATUS_COMPLETED);
610                 return response;
611             }
612
613         };
614
615         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
616
617             @Override
618             public void callback(final RestfulResponse response) {
619                 System.out.println("callback called.");
620
621             }
622
623             @Override
624             public void handleExcepion(final Throwable e) {
625
626                 System.out.println("handleExcepion called.");
627             }
628
629         };
630         httpRest.asyncPut("path/to/service", new RestfulParametes(), callback);
631         httpRest.asyncPut("path/to/service", new RestfulParametes(), null);
632     }
633
634     /**
635      * <br/>
636      * 
637      * @throws Exception
638      * @since  
639      */
640     @Test
641     public void testAsyncPutStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws Exception {
642         final RestfulOptions options = new RestfulOptions();
643
644         final HttpRest httpRest = getHttpRest(options);
645         new MockUp<RestHttpContentExchange>() {
646
647             @Mock
648             public int waitForDone() {
649                 System.out.println("waitForDone");
650                 return HttpExchange.STATUS_COMPLETED;
651             }
652
653             @Mock
654             public RestfulResponse getResponse() throws IOException {
655                 final RestfulResponse response = new RestfulResponse();
656                 response.setStatus(HttpExchange.STATUS_COMPLETED);
657                 return response;
658             }
659
660         };
661
662         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
663
664             @Override
665             public void callback(final RestfulResponse response) {
666                 System.out.println("callback called.");
667
668             }
669
670             @Override
671             public void handleExcepion(final Throwable e) {
672
673                 System.out.println("handleExcepion called.");
674             }
675
676         };
677         httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), callback);
678         httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), null);
679     }
680
681     /**
682      * <br/>
683      * 
684      * @throws Exception
685      * @since  
686      */
687     @Test
688     public void testPostStringRestfulParametes() throws Exception {
689         final RestfulOptions options = new RestfulOptions();
690
691         final HttpBaseRest httpRest = getHttpRest(options);
692         new MockUp<RestHttpContentExchange>() {
693
694             @Mock
695             public int waitForDone() {
696                 System.out.println("waitForDone");
697                 return HttpExchange.STATUS_EXPIRED;
698             }
699
700             @Mock
701             public RestfulResponse getResponse() throws IOException {
702                 final RestfulResponse response = new RestfulResponse();
703                 response.setStatus(HttpExchange.STATUS_EXPIRED);
704                 return response;
705             }
706
707         };
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", "");
714
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());
719     }
720
721     /**
722      * <br/>
723      * 
724      * @throws ServiceException
725      * @since  
726      */
727     @Test
728     public void testPostStringRestfulParametesRestfulOptions() throws ServiceException {
729         final RestfulOptions options = new RestfulOptions();
730
731         final HttpBaseRest httpRest = getHttpRest(options);
732         new MockUp<RestHttpContentExchange>() {
733
734             @Mock
735             public int waitForDone() {
736                 System.out.println("waitForDone" + HttpExchange.STATUS_EXCEPTED);
737                 return HttpExchange.STATUS_EXCEPTED;
738             }
739
740             @Mock
741             public RestfulResponse getResponse() throws IOException {
742                 final RestfulResponse response = new RestfulResponse();
743                 response.setStatus(HttpExchange.STATUS_COMPLETED);
744                 return response;
745             }
746
747         };
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());
759     }
760
761     /**
762      * <br/>
763      * 
764      * @throws Exception
765      * @since  
766      */
767     @Test
768     public void testAsyncPostStringRestfulParametesRestfulAsyncCallback() throws Exception {
769         final RestfulOptions options = new RestfulOptions();
770         options.setRestTimeout(10);
771
772         final HttpBaseRest httpRest = getHttpRest(options);
773         new MockUp<RestHttpContentExchange>() {
774
775             @Mock
776             public int waitForDone() {
777                 System.out.println("waitForDone:" + HttpExchange.STATUS_EXCEPTED);
778                 return 99;
779             }
780
781             @Mock
782             public RestfulResponse getResponse() throws IOException {
783                 final RestfulResponse response = new RestfulResponse();
784                 response.setStatus(HttpExchange.STATUS_EXCEPTED);
785                 return response;
786             }
787
788         };
789
790         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
791
792             @Override
793             public void callback(final RestfulResponse response) {
794                 System.out.println("callback called.");
795
796             }
797
798             @Override
799             public void handleExcepion(final Throwable e) {
800
801                 System.out.println("handleExcepion called.");
802             }
803
804         };
805         httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback);
806         httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null);
807     }
808
809     /**
810      * <br/>
811      * 
812      * @throws ServiceException
813      * @since  
814      */
815     @Test
816     public void testAsyncPostStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
817         final RestfulOptions options = new RestfulOptions();
818         options.setRestTimeout(10);
819
820         final HttpBaseRest httpRest = getHttpRest(options);
821         new MockUp<RestHttpContentExchange>() {
822
823             @Mock
824             public int waitForDone() {
825                 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
826                 return HttpExchange.STATUS_COMPLETED;
827             }
828
829             @Mock
830             public RestfulResponse getResponse() throws IOException {
831                 final RestfulResponse response = new RestfulResponse();
832                 response.setStatus(HttpExchange.STATUS_COMPLETED);
833                 return response;
834             }
835
836         };
837
838         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
839
840             @Override
841             public void callback(final RestfulResponse response) {
842                 System.out.println("callback called.");
843
844             }
845
846             @Override
847             public void handleExcepion(final Throwable e) {
848
849                 System.out.println("handleExcepion called.");
850             }
851
852         };
853         httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback);
854         httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null);
855     }
856
857     /**
858      * <br/>
859      * 
860      * @throws ServiceException
861      * @since  
862      */
863     @Test
864     public void testDeleteStringRestfulParametes() throws ServiceException {
865         final RestfulOptions options = new RestfulOptions();
866
867         final HttpBaseRest httpRest = getHttpRest(options);
868
869         final RestfulResponse response = httpRest.delete("path/to/service", null);
870         assertEquals(-1, response.getStatus());
871     }
872
873     /**
874      * <br/>
875      * 
876      * @throws ServiceException
877      * @since  
878      */
879     @Test
880     public void testDeleteStringRestfulParametesRestfulOptions() throws ServiceException {
881         final RestfulOptions options = new RestfulOptions();
882
883         final HttpBaseRest httpRest = getHttpRest(options);
884         new MockUp<RestHttpContentExchange>() {
885
886             @Mock
887             public int waitForDone() {
888                 System.out.println("waitForDone" + HttpExchange.STATUS_COMPLETED);
889                 return HttpExchange.STATUS_COMPLETED;
890             }
891
892             @Mock
893             public RestfulResponse getResponse() throws IOException {
894                 final RestfulResponse response = new RestfulResponse();
895                 response.setStatus(HttpExchange.STATUS_COMPLETED);
896                 return response;
897             }
898
899         };
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());
909     }
910
911     /**
912      * <br/>
913      * 
914      * @throws ServiceException
915      * @since  
916      */
917     @Test
918     public void testAsyncDeleteStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
919         final RestfulOptions options = new RestfulOptions();
920         options.setRestTimeout(10);
921
922         final HttpBaseRest httpRest = getHttpRest(options);
923         new MockUp<RestHttpContentExchange>() {
924
925             @Mock
926             public int waitForDone() {
927                 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
928                 return HttpExchange.STATUS_COMPLETED;
929             }
930
931             @Mock
932             public RestfulResponse getResponse() throws IOException {
933                 final RestfulResponse response = new RestfulResponse();
934                 response.setStatus(HttpExchange.STATUS_COMPLETED);
935                 return response;
936             }
937
938         };
939
940         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
941
942             @Override
943             public void callback(final RestfulResponse response) {
944                 System.out.println("callback called.");
945
946             }
947
948             @Override
949             public void handleExcepion(final Throwable e) {
950
951                 System.out.println("handleExcepion called.");
952             }
953
954         };
955         httpRest.asyncDelete("path/to/service", new RestfulParametes(), callback);
956         httpRest.asyncDelete("path/to/service", new RestfulParametes(), null);
957     }
958
959     /**
960      * <br/>
961      * 
962      * @throws ServiceException
963      * @since  
964      */
965     @Test
966     public void testAsyncDeleteStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
967         final RestfulOptions options = new RestfulOptions();
968         options.setRestTimeout(10);
969
970         final HttpBaseRest httpRest = getHttpRest(options);
971         new MockUp<RestHttpContentExchange>() {
972
973             @Mock
974             public int waitForDone() {
975                 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
976                 return HttpExchange.STATUS_COMPLETED;
977             }
978
979             @Mock
980             public RestfulResponse getResponse() throws IOException {
981                 final RestfulResponse response = new RestfulResponse();
982                 response.setStatus(HttpExchange.STATUS_COMPLETED);
983                 return response;
984             }
985
986         };
987
988         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
989
990             @Override
991             public void callback(final RestfulResponse response) {
992                 System.out.println("callback called.");
993
994             }
995
996             @Override
997             public void handleExcepion(final Throwable e) {
998
999                 System.out.println("handleExcepion called.");
1000             }
1001
1002         };
1003         httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, callback);
1004         httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, null);
1005     }
1006
1007     /**
1008      * <br/>
1009      * 
1010      * @throws ServiceException
1011      * @since  
1012      */
1013     @Test
1014     public void testPatchStringRestfulParametes() throws ServiceException {
1015         final RestfulOptions options = new RestfulOptions();
1016
1017         final HttpBaseRest httpRest = getHttpRest(options);
1018         new MockUp<RestHttpContentExchange>() {
1019
1020             @Mock
1021             public int waitForDone() {
1022                 System.out.println("waitForDone" + HttpExchange.STATUS_COMPLETED);
1023                 return HttpExchange.STATUS_COMPLETED;
1024             }
1025
1026             @Mock
1027             public RestfulResponse getResponse() throws IOException {
1028                 final RestfulResponse response = new RestfulResponse();
1029                 response.setStatus(HttpExchange.STATUS_COMPLETED);
1030                 return response;
1031             }
1032
1033         };
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());
1043     }
1044
1045     /**
1046      * <br/>
1047      * 
1048      * @throws ServiceException
1049      * @since  
1050      */
1051     @Test
1052     public void testPatchStringRestfulParametesRestfulOptions() throws ServiceException {
1053         final RestfulOptions options = new RestfulOptions();
1054
1055         final HttpBaseRest httpRest = getHttpRest(options);
1056         new MockUp<RestHttpContentExchange>() {
1057
1058             @Mock
1059             public int waitForDone() {
1060                 System.out.println("waitForDone" + HttpExchange.STATUS_COMPLETED);
1061                 return HttpExchange.STATUS_COMPLETED;
1062             }
1063
1064             @Mock
1065             public RestfulResponse getResponse() throws IOException {
1066                 final RestfulResponse response = new RestfulResponse();
1067                 response.setStatus(HttpExchange.STATUS_COMPLETED);
1068                 return response;
1069             }
1070
1071         };
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());
1081     }
1082
1083     /**
1084      * <br/>
1085      * 
1086      * @throws ServiceException
1087      * @since  
1088      */
1089     @Test
1090     public void testAsyncPatchStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
1091         final RestfulOptions options = new RestfulOptions();
1092         options.setRestTimeout(10);
1093
1094         final HttpBaseRest httpRest = getHttpRest(options);
1095         new MockUp<RestHttpContentExchange>() {
1096
1097             @Mock
1098             public int waitForDone() {
1099                 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
1100                 return HttpExchange.STATUS_COMPLETED;
1101             }
1102
1103             @Mock
1104             public RestfulResponse getResponse() throws IOException {
1105                 final RestfulResponse response = new RestfulResponse();
1106                 response.setStatus(HttpExchange.STATUS_COMPLETED);
1107                 return response;
1108             }
1109
1110         };
1111
1112         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
1113
1114             @Override
1115             public void callback(final RestfulResponse response) {
1116                 System.out.println("callback called.");
1117
1118             }
1119
1120             @Override
1121             public void handleExcepion(final Throwable e) {
1122
1123                 System.out.println("handleExcepion called.");
1124             }
1125
1126         };
1127         httpRest.asyncPatch("path/to/service", new RestfulParametes(), callback);
1128         httpRest.asyncPatch("path/to/service", new RestfulParametes(), null);
1129     }
1130
1131     /**
1132      * <br/>
1133      * 
1134      * @throws ServiceException
1135      * @since  
1136      */
1137     @Test
1138     public void testAsyncPatchStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
1139         final RestfulOptions options = new RestfulOptions();
1140         options.setRestTimeout(10);
1141
1142         final HttpBaseRest httpRest = getHttpRest(options);
1143         new MockUp<RestHttpContentExchange>() {
1144
1145             @Mock
1146             public int waitForDone() {
1147                 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
1148                 return HttpExchange.STATUS_COMPLETED;
1149             }
1150
1151             @Mock
1152             public RestfulResponse getResponse() throws IOException {
1153                 final RestfulResponse response = new RestfulResponse();
1154                 response.setStatus(HttpExchange.STATUS_COMPLETED);
1155                 return response;
1156             }
1157
1158         };
1159
1160         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
1161
1162             @Override
1163             public void callback(final RestfulResponse response) {
1164                 System.out.println("callback called.");
1165
1166             }
1167
1168             @Override
1169             public void handleExcepion(final Throwable e) {
1170
1171                 System.out.println("handleExcepion called.");
1172             }
1173
1174         };
1175         httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, callback);
1176         httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, null);
1177     }
1178
1179 }