adjust the code
[vfc/nfvo/wfengine.git] / rest-client / src / test / java / org / openo / baseservice / roa / util / restclient / TestHttpRest.java
1 /*
2  * Copyright (c) 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 org.openo.baseservice.remoteservice.exception.ServiceException;
23 import org.openo.baseservice.roa.util.ServiceUtil;
24
25 import org.eclipse.jetty.client.HttpClient;
26 import org.eclipse.jetty.client.HttpExchange;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Ignore;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.ExpectedException;
36 import org.junit.runner.RunWith;
37
38 import java.io.IOException;
39 import java.io.UnsupportedEncodingException;
40 import java.lang.reflect.Field;
41 import java.net.URLEncoder;
42
43 import mockit.Expectations;
44 import mockit.Mock;
45 import mockit.MockUp;
46 import mockit.Mocked;
47 import mockit.NonStrictExpectations;
48 import mockit.integration.junit4.JMockit;
49
50 /**
51  * <br/>
52  * <p>
53  * </p>
54  * 
55  * @author
56  * @version SDNO 0.5 13-Jun-2016
57  */
58 @RunWith(JMockit.class)
59 public class TestHttpRest {
60
61     @Rule
62     public ExpectedException thrown = ExpectedException.none();
63
64     /**
65      * <br/>
66      * 
67      * @throws java.lang.Exception
68      * @since SDNO 0.5
69      */
70     @BeforeClass
71     public static void setUpBeforeClass() throws Exception {
72     }
73
74     /**
75      * <br/>
76      * 
77      * @throws java.lang.Exception
78      * @since SDNO 0.5
79      */
80     @AfterClass
81     public static void tearDownAfterClass() throws Exception {
82     }
83
84     /**
85      * <br/>
86      * 
87      * @throws java.lang.Exception
88      * @since SDNO 0.5
89      */
90     @Before
91     public void setUp() throws Exception {
92     }
93
94     /**
95      * <br/>
96      * 
97      * @throws java.lang.Exception
98      * @since SDNO 0.5
99      */
100     @After
101     public void tearDown() throws Exception {
102     }
103
104     /**
105      * <br/>
106      * 
107      * @throws Exception
108      * @since SDNO 0.5
109      */
110     @Test
111     public void testInitHttpRest() throws Exception {
112         final RestfulOptions options = new RestfulOptions();
113         new MockUp<HttpClient>() {
114
115             @Mock
116             public void doStart() {
117                 System.out.println("started");
118             }
119         };
120         final HttpRest httpRest = new HttpRest();
121         httpRest.initHttpRest(options);
122         final Field httpClient = HttpBaseRest.class.getDeclaredField("client");
123         httpClient.setAccessible(true);
124         Assert.assertNotNull(httpClient.get(httpRest));
125     }
126
127     /**
128      * <br/>
129      * 
130      * @throws Exception
131      * @since SDNO 0.5
132      */
133     @Test
134     public void testInitHttpRestExcpetion() throws Exception {
135         final RestfulOptions options = new RestfulOptions();
136         new MockUp<HttpClient>() {
137
138             @Mock
139             public void doStart() throws Exception {
140                 throw new Exception();
141             }
142         };
143         final HttpRest httpRest = new HttpRest();
144         thrown.expect(ServiceException.class);
145         thrown.expectMessage("http client init failed.");
146         httpRest.initHttpRest(options);
147         final Field httpClient = HttpRest.class.getDeclaredField("client");
148         httpClient.setAccessible(true);
149         Assert.assertNull(httpClient.get(httpRest));
150         System.out.println("finished");
151     }
152
153     /**
154      * <br/>
155      * 
156      * @throws Exception
157      * @since SDNO 0.5
158      */
159     @Test
160     public void testInitHttpRestNull() throws Exception {
161         final HttpRest httpRest = new HttpRest();
162         thrown.expect(ServiceException.class);
163         thrown.expectMessage("option is null.");
164         httpRest.initHttpRest(null);
165     }
166
167     /**
168      * <br/>
169      * 
170      * @throws NoSuchFieldException
171      * @throws Exception
172      * @since SDNO 0.5
173      */
174     @Test
175     public void testCreateRestHttpContentExchange() throws NoSuchFieldException, Exception {
176         final HttpBaseRest httpRest = new HttpRest();
177         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
178
179             @Override
180             public void callback(final RestfulResponse response) {
181
182             }
183
184             @Override
185             public void handleExcepion(final Throwable e) {
186
187             }
188
189         };
190         final RestHttpContentExchange exchange = httpRest.createRestHttpContentExchange(callback);
191         assertNotNull(exchange);
192         final Field callbackField = RestHttpContentExchange.class.getDeclaredField("callback");
193         assertNotNull(callbackField);
194     }
195
196     /**
197      * <br/>
198      * 
199      * @throws Exception
200      * @since SDNO 0.5
201      */
202     @Test
203     public void testGetStringRestfulParametes() throws Exception {
204         final RestfulOptions options = new RestfulOptions();
205
206         final HttpRest httpRest = getHttpRest(options);
207         new MockUp<RestHttpContentExchange>() {
208
209             @Mock
210             public int waitForDone() {
211                 System.out.println("waitForDone");
212                 return HttpExchange.STATUS_COMPLETED;
213             }
214
215             @Mock
216             public RestfulResponse getResponse() throws IOException {
217                 final RestfulResponse response = new RestfulResponse();
218                 response.setStatus(HttpExchange.STATUS_COMPLETED);
219                 return response;
220             }
221
222         };
223         final RestfulParametes parametes = new RestfulParametes();
224         parametes.put("id", "1234");
225         parametes.put("name", "some-name");
226         parametes.put("address", null);
227         parametes.putHttpContextHeader("Content-Type", "application/json");
228         parametes.putHttpContextHeader("Accept-Encoding", "*/*");
229         final RestfulResponse response = httpRest.get("path/to/service", parametes);
230         assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
231
232     }
233
234     /**
235      * <br/>
236      * 
237      * @throws Exception
238      * @since SDNO 0.5
239      */
240     @Test
241     public void testGetStringRestfulParametesRestfulOptions() throws Exception {
242         final RestfulOptions options = new RestfulOptions();
243
244         final HttpRest httpRest = getHttpRest(options);
245         new MockUp<RestHttpContentExchange>() {
246
247             @Mock
248             public int waitForDone() {
249                 System.out.println("waitForDone");
250                 return HttpExchange.STATUS_COMPLETED;
251             }
252
253             @Mock
254             public RestfulResponse getResponse() throws IOException {
255                 final RestfulResponse response = new RestfulResponse();
256                 response.setStatus(HttpExchange.STATUS_COMPLETED);
257                 return response;
258             }
259
260         };
261         final RestfulResponse response = httpRest.get("path/to/service", new RestfulParametes(), options);
262         assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
263     }
264
265     /**
266      * <br/>
267      * 
268      * @throws Exception
269      * @since SDNO 0.5
270      */
271     @Ignore
272     @Test
273     public void testGetStringRestfulParametesEncodeError() throws Exception {
274         final RestfulOptions options = new RestfulOptions();
275
276         final HttpRest httpRest = getHttpRest(options);
277         new MockUp<RestHttpContentExchange>() {
278
279             @Mock
280             public int waitForDone() {
281                 System.out.println("waitForDone");
282                 return HttpExchange.STATUS_COMPLETED;
283             }
284
285             @Mock
286             public RestfulResponse getResponse() throws IOException {
287                 final RestfulResponse response = new RestfulResponse();
288                 response.setStatus(HttpExchange.STATUS_COMPLETED);
289                 return response;
290             }
291
292         };
293
294         new NonStrictExpectations() {
295
296             @Mocked
297             URLEncoder encoder;
298
299             {
300                 URLEncoder.encode(anyString, RestfulClientConst.ENCODING);
301                 result = new UnsupportedEncodingException();
302             }
303
304         };
305
306         thrown.expect(ServiceException.class);
307         thrown.expectMessage("Broken VM does not support");
308
309         final RestfulParametes parametes = new RestfulParametes();
310         parametes.put("id", "1234");
311         parametes.put("name", "some-name");
312         parametes.put("address", null);
313         parametes.putHttpContextHeader("Content-Type", "application/json");
314         parametes.putHttpContextHeader("Accept-Encoding", "*/*");
315         final RestfulResponse response = httpRest.get("path/to/service", parametes);
316         assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
317
318     }
319
320     /**
321      * <br/>
322      * 
323      * @throws Exception
324      * @since SDNO 0.5
325      */
326     @Test
327     public void testHeadStringRestfulParametes() throws Exception {
328         final RestfulOptions options = new RestfulOptions();
329
330         final HttpRest httpRest = getHttpRest(options);
331         new MockUp<RestHttpContentExchange>() {
332
333             @Mock
334             public int waitForDone() {
335                 System.out.println("waitForDone");
336                 return HttpExchange.STATUS_COMPLETED;
337             }
338
339             @Mock
340             public RestfulResponse getResponse() throws IOException {
341                 final RestfulResponse response = new RestfulResponse();
342                 response.setStatus(HttpExchange.STATUS_COMPLETED);
343                 return response;
344             }
345
346         };
347         final RestfulParametes parametes = new RestfulParametes();
348         parametes.put("id", "1234");
349         parametes.put("name", "some-name");
350         parametes.put("address", null);
351         parametes.putHttpContextHeader("Content-Type", "");
352         parametes.putHttpContextHeader("Accept-Encoding", "");
353         final RestfulResponse response = httpRest.head("path/to/service", parametes);
354         assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
355     }
356
357     /**
358      * <br/>
359      * 
360      * @throws Exception
361      * @since SDNO 0.5
362      */
363     @Ignore
364     @Test
365     public void testHeadStringRestfulParametesRestfulOptions() throws Exception {
366         final RestfulOptions options = new RestfulOptions();
367
368         final HttpRest httpRest = getHttpRest(options);
369         new MockUp<RestHttpContentExchange>() {
370
371             @Mock
372             public int waitForDone() {
373                 System.out.println("waitForDone");
374                 return HttpExchange.STATUS_COMPLETED;
375             }
376
377             @Mock
378             public RestfulResponse getResponse() throws IOException {
379                 final RestfulResponse response = new RestfulResponse();
380                 response.setStatus(HttpExchange.STATUS_COMPLETED);
381                 return response;
382             }
383
384         };
385         final RestfulParametes parametes = new RestfulParametes();
386         parametes.put("id", "1234");
387         parametes.put("name", "some-name");
388         parametes.put("address", null);
389         parametes.putHttpContextHeader("Content-Type", "");
390         parametes.putHttpContextHeader("Accept-Encoding", "");
391         new Expectations() {
392
393             ServiceUtil serviceUtil;
394
395             {
396                 serviceUtil = new ServiceUtil(anyString, anyString);
397                 serviceUtil.getServiceHost();
398                 returns("127.0.0.1");
399
400                 serviceUtil.getServicePort();
401                 returns(10);
402             }
403
404         };
405         final RestfulResponse response = httpRest.head("path/to/service", parametes, options);
406         assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
407     }
408
409     /**
410      * <br/>
411      * 
412      * @param options
413      * @return
414      * @throws ServiceException
415      * @since SDNO 0.5
416      */
417     private HttpRest getHttpRest(final RestfulOptions options) throws ServiceException {
418         final HttpRest httpRest = new HttpRest();
419         {
420             new MockUp<HttpClient>() {
421
422                 @Mock
423                 public void doStart() {
424                     System.out.println("started");
425                 }
426
427                 @Mock
428                 public void send(final HttpExchange exchange) throws IOException {
429                     System.out.println("send");
430                 }
431             };
432             httpRest.initHttpRest(options);
433
434         }
435         return httpRest;
436     }
437
438     /**
439      * <br/>
440      * 
441      * @throws Exception
442      * @since SDNO 0.5
443      */
444     @Test
445     public void testAsyncGetStringRestfulParametesRestfulAsyncCallback() throws Exception {
446         final RestfulOptions options = new RestfulOptions();
447
448         final HttpRest httpRest = getHttpRest(options);
449         new MockUp<RestHttpContentExchange>() {
450
451             @Mock
452             public int waitForDone() {
453                 System.out.println("waitForDone");
454                 return HttpExchange.STATUS_COMPLETED;
455             }
456
457             @Mock
458             public RestfulResponse getResponse() throws IOException {
459                 final RestfulResponse response = new RestfulResponse();
460                 response.setStatus(HttpExchange.STATUS_COMPLETED);
461                 return response;
462             }
463
464         };
465
466         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
467
468             @Override
469             public void callback(final RestfulResponse response) {
470                 System.out.println("callback called.");
471
472             }
473
474             @Override
475             public void handleExcepion(final Throwable e) {
476
477                 System.out.println("handleExcepion called.");
478             }
479
480         };
481         httpRest.asyncGet("path/to/service", new RestfulParametes(), callback);
482         httpRest.asyncGet("path/to/service", new RestfulParametes(), null);
483     }
484
485     /**
486      * <br/>
487      * 
488      * @throws ServiceException
489      * @since SDNO 0.5
490      */
491     @Test
492     public void testAsyncGetStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
493         final RestfulOptions options = new RestfulOptions();
494
495         final HttpRest httpRest = getHttpRest(options);
496         new MockUp<RestHttpContentExchange>() {
497
498             @Mock
499             public int waitForDone() {
500                 System.out.println("waitForDone");
501                 return HttpExchange.STATUS_COMPLETED;
502             }
503
504             @Mock
505             public RestfulResponse getResponse() throws IOException {
506                 final RestfulResponse response = new RestfulResponse();
507                 response.setStatus(HttpExchange.STATUS_COMPLETED);
508                 return response;
509             }
510
511         };
512
513         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
514
515             @Override
516             public void callback(final RestfulResponse response) {
517                 System.out.println("callback called.");
518
519             }
520
521             @Override
522             public void handleExcepion(final Throwable e) {
523
524                 System.out.println("handleExcepion called.");
525             }
526
527         };
528         httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), callback);
529         httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), null);
530     }
531
532     /**
533      * <br/>
534      * 
535      * @throws ServiceException
536      * @since SDNO 0.5
537      */
538     @Ignore
539     @Test
540     public void testPutStringRestfulParametes() throws ServiceException {
541         final RestfulOptions options = new RestfulOptions();
542
543         final HttpRest httpRest = getHttpRest(options);
544         new MockUp<RestHttpContentExchange>() {
545
546             @Mock
547             public int waitForDone() {
548                 System.out.println("waitForDone");
549                 return HttpExchange.STATUS_COMPLETED;
550             }
551
552             @Mock
553             public RestfulResponse getResponse() throws IOException {
554                 final RestfulResponse response = new RestfulResponse();
555                 response.setStatus(HttpExchange.STATUS_COMPLETED);
556                 return response;
557             }
558
559         };
560         final RestfulParametes parametes = new RestfulParametes();
561         parametes.put("id", "1234");
562         parametes.put("name", "some-name");
563         parametes.put("address", null);
564         parametes.putHttpContextHeader("Content-Type", "");
565         parametes.putHttpContextHeader("Accept-Encoding", "");
566         new Expectations() {
567
568             ServiceUtil serviceUtil;
569
570             {
571                 serviceUtil = new ServiceUtil(anyString, anyString);
572                 serviceUtil.getServiceHost();
573                 returns("127.0.0.1");
574
575                 serviceUtil.getServicePort();
576                 returns(-1);
577             }
578
579         };
580         final RestfulResponse response = httpRest.put("path/to/service", parametes);
581         assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
582     }
583
584     /**
585      * <br/>
586      * 
587      * @throws ServiceException
588      * @since SDNO 0.5
589      */
590     @Ignore
591     @Test
592     public void testPutStringRestfulParametesRestfulOptions() throws ServiceException {
593
594         final RestfulOptions options = new RestfulOptions();
595
596         final HttpRest httpRest = getHttpRest(options);
597         new MockUp<RestHttpContentExchange>() {
598
599             @Mock
600             public int waitForDone() {
601                 System.out.println("waitForDone");
602                 return HttpExchange.STATUS_COMPLETED;
603             }
604
605             @Mock
606             public RestfulResponse getResponse() throws IOException {
607                 final RestfulResponse response = new RestfulResponse();
608                 response.setStatus(HttpExchange.STATUS_COMPLETED);
609                 return response;
610             }
611
612         };
613         final RestfulParametes parametes = new RestfulParametes();
614         parametes.put("id", "1234");
615         parametes.put("name", "some-name");
616         parametes.put("address", null);
617         parametes.putHttpContextHeader("Content-Type", "");
618         parametes.putHttpContextHeader("Accept-Encoding", "");
619         new Expectations() {
620
621             ServiceUtil serviceUtil;
622
623             {
624                 serviceUtil = new ServiceUtil(anyString, anyString);
625                 serviceUtil.getServiceHost();
626                 returns("127.0.0.1");
627
628                 serviceUtil.getServicePort();
629                 returns(10);
630             }
631
632         };
633         final RestfulResponse response = httpRest.put("path/to/service", parametes, null);
634         assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
635     }
636
637     /**
638      * <br/>
639      * 
640      * @throws ServiceException
641      * @since SDNO 0.5
642      */
643     @Test
644     public void testAsyncPutStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
645         final RestfulOptions options = new RestfulOptions();
646
647         final HttpRest httpRest = getHttpRest(options);
648         new MockUp<RestHttpContentExchange>() {
649
650             @Mock
651             public int waitForDone() {
652                 System.out.println("waitForDone");
653                 return HttpExchange.STATUS_COMPLETED;
654             }
655
656             @Mock
657             public RestfulResponse getResponse() throws IOException {
658                 final RestfulResponse response = new RestfulResponse();
659                 response.setStatus(HttpExchange.STATUS_COMPLETED);
660                 return response;
661             }
662
663         };
664
665         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
666
667             @Override
668             public void callback(final RestfulResponse response) {
669                 System.out.println("callback called.");
670
671             }
672
673             @Override
674             public void handleExcepion(final Throwable e) {
675
676                 System.out.println("handleExcepion called.");
677             }
678
679         };
680         httpRest.asyncPut("path/to/service", new RestfulParametes(), callback);
681         httpRest.asyncPut("path/to/service", new RestfulParametes(), null);
682     }
683
684     /**
685      * <br/>
686      * 
687      * @throws Exception
688      * @since SDNO 0.5
689      */
690     @Test
691     public void testAsyncPutStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws Exception {
692         final RestfulOptions options = new RestfulOptions();
693
694         final HttpRest httpRest = getHttpRest(options);
695         new MockUp<RestHttpContentExchange>() {
696
697             @Mock
698             public int waitForDone() {
699                 System.out.println("waitForDone");
700                 return HttpExchange.STATUS_COMPLETED;
701             }
702
703             @Mock
704             public RestfulResponse getResponse() throws IOException {
705                 final RestfulResponse response = new RestfulResponse();
706                 response.setStatus(HttpExchange.STATUS_COMPLETED);
707                 return response;
708             }
709
710         };
711
712         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
713
714             @Override
715             public void callback(final RestfulResponse response) {
716                 System.out.println("callback called.");
717
718             }
719
720             @Override
721             public void handleExcepion(final Throwable e) {
722
723                 System.out.println("handleExcepion called.");
724             }
725
726         };
727         httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), callback);
728         httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), null);
729     }
730
731     /**
732      * <br/>
733      * 
734      * @throws Exception
735      * @since SDNO 0.5
736      */
737     @Test
738     public void testPostStringRestfulParametes() throws Exception {
739         final RestfulOptions options = new RestfulOptions();
740
741         final HttpBaseRest httpRest = getHttpRest(options);
742         new MockUp<RestHttpContentExchange>() {
743
744             @Mock
745             public int waitForDone() {
746                 System.out.println("waitForDone");
747                 return HttpExchange.STATUS_EXPIRED;
748             }
749
750             @Mock
751             public RestfulResponse getResponse() throws IOException {
752                 final RestfulResponse response = new RestfulResponse();
753                 response.setStatus(HttpExchange.STATUS_EXPIRED);
754                 return response;
755             }
756
757         };
758         final RestfulParametes parameters = new RestfulParametes();
759         parameters.put("id", "1234");
760         parameters.put("name", "some-name");
761         parameters.put("address", null);
762         parameters.putHttpContextHeader("Content-Type", "");
763         parameters.putHttpContextHeader("Accept-Encoding", "");
764
765         thrown.expect(ServiceException.class);
766         thrown.expectMessage("request is expierd");
767         final RestfulResponse response = httpRest.post("http://localhost:80/path/to/service", parameters);
768         assertEquals(HttpExchange.STATUS_EXPIRED, response.getStatus());
769     }
770
771     /**
772      * <br/>
773      * 
774      * @throws ServiceException
775      * @since SDNO 0.5
776      */
777     @Ignore
778     @Test
779     public void testPostStringRestfulParametesRestfulOptions() throws ServiceException {
780         final RestfulOptions options = new RestfulOptions();
781
782         final HttpBaseRest httpRest = getHttpRest(options);
783         new MockUp<RestHttpContentExchange>() {
784
785             @Mock
786             public int waitForDone() {
787                 System.out.println("waitForDone" + HttpExchange.STATUS_EXCEPTED);
788                 return HttpExchange.STATUS_EXCEPTED;
789             }
790
791             @Mock
792             public RestfulResponse getResponse() throws IOException {
793                 final RestfulResponse response = new RestfulResponse();
794                 response.setStatus(HttpExchange.STATUS_COMPLETED);
795                 return response;
796             }
797
798         };
799         final RestfulParametes parameters = new RestfulParametes();
800         parameters.put("id", "1234");
801         parameters.put("name", "some-name");
802         parameters.put("address", null);
803         parameters.setRawData("{ \"data\"=\"sample JSON data\"");
804         parameters.putHttpContextHeader("Content-Type", "");
805         parameters.putHttpContextHeader("Accept-Encoding", "");
806         new Expectations() {
807
808             ServiceUtil serviceUtil;
809
810             {
811                 serviceUtil = new ServiceUtil(anyString, anyString);
812                 serviceUtil.getServiceHost();
813                 returns("127.0.0.1");
814
815                 serviceUtil.getServicePort();
816                 returns(10);
817             }
818
819         };
820         thrown.expect(ServiceException.class);
821         thrown.expectMessage("request is exception");
822         final RestfulResponse response = httpRest.post("path/to/service", parameters, null);
823         assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
824     }
825
826     /**
827      * <br/>
828      * 
829      * @throws Exception
830      * @since SDNO 0.5
831      */
832     @Test
833     public void testAsyncPostStringRestfulParametesRestfulAsyncCallback() throws Exception {
834         final RestfulOptions options = new RestfulOptions();
835         options.setRestTimeout(10);
836
837         final HttpBaseRest httpRest = getHttpRest(options);
838         new MockUp<RestHttpContentExchange>() {
839
840             @Mock
841             public int waitForDone() {
842                 System.out.println("waitForDone:" + HttpExchange.STATUS_EXCEPTED);
843                 return 99;
844             }
845
846             @Mock
847             public RestfulResponse getResponse() throws IOException {
848                 final RestfulResponse response = new RestfulResponse();
849                 response.setStatus(HttpExchange.STATUS_EXCEPTED);
850                 return response;
851             }
852
853         };
854
855         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
856
857             @Override
858             public void callback(final RestfulResponse response) {
859                 System.out.println("callback called.");
860
861             }
862
863             @Override
864             public void handleExcepion(final Throwable e) {
865
866                 System.out.println("handleExcepion called.");
867             }
868
869         };
870         httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback);
871         httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null);
872     }
873
874     /**
875      * <br/>
876      * 
877      * @throws ServiceException
878      * @since SDNO 0.5
879      */
880     @Test
881     public void testAsyncPostStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
882         final RestfulOptions options = new RestfulOptions();
883         options.setRestTimeout(10);
884
885         final HttpBaseRest httpRest = getHttpRest(options);
886         new MockUp<RestHttpContentExchange>() {
887
888             @Mock
889             public int waitForDone() {
890                 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
891                 return HttpExchange.STATUS_COMPLETED;
892             }
893
894             @Mock
895             public RestfulResponse getResponse() throws IOException {
896                 final RestfulResponse response = new RestfulResponse();
897                 response.setStatus(HttpExchange.STATUS_COMPLETED);
898                 return response;
899             }
900
901         };
902
903         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
904
905             @Override
906             public void callback(final RestfulResponse response) {
907                 System.out.println("callback called.");
908
909             }
910
911             @Override
912             public void handleExcepion(final Throwable e) {
913
914                 System.out.println("handleExcepion called.");
915             }
916
917         };
918         httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback);
919         httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null);
920     }
921
922     /**
923      * <br/>
924      * 
925      * @throws ServiceException
926      * @since SDNO 0.5
927      */
928     @Test
929     public void testDeleteStringRestfulParametes() throws ServiceException {
930         final RestfulOptions options = new RestfulOptions();
931
932         final HttpBaseRest httpRest = getHttpRest(options);
933
934         final RestfulResponse response = httpRest.delete("path/to/service", null);
935         assertEquals(-1, response.getStatus());
936     }
937
938     /**
939      * <br/>
940      * 
941      * @throws ServiceException
942      * @since SDNO 0.5
943      */
944     @Ignore
945     @Test
946     public void testDeleteStringRestfulParametesRestfulOptions() throws ServiceException {
947         final RestfulOptions options = new RestfulOptions();
948
949         final HttpBaseRest httpRest = getHttpRest(options);
950         new MockUp<RestHttpContentExchange>() {
951
952             @Mock
953             public int waitForDone() {
954                 System.out.println("waitForDone" + HttpExchange.STATUS_COMPLETED);
955                 return HttpExchange.STATUS_COMPLETED;
956             }
957
958             @Mock
959             public RestfulResponse getResponse() throws IOException {
960                 final RestfulResponse response = new RestfulResponse();
961                 response.setStatus(HttpExchange.STATUS_COMPLETED);
962                 return response;
963             }
964
965         };
966         final RestfulParametes parameters = new RestfulParametes();
967         parameters.put("id", "1234");
968         parameters.put("name", "some-name");
969         parameters.put("address", null);
970         parameters.setRawData("{ \"data\"=\"sample JSON data\"");
971         parameters.putHttpContextHeader("Content-Type", "");
972         parameters.putHttpContextHeader("Accept-Encoding", "");
973         new Expectations() {
974
975             ServiceUtil serviceUtil;
976
977             {
978                 serviceUtil = new ServiceUtil(anyString, anyString);
979                 serviceUtil.getServiceHost();
980                 returns("127.0.0.1");
981
982                 serviceUtil.getServicePort();
983                 returns(10);
984             }
985
986         };
987         final RestfulResponse response = httpRest.delete("path/to/service", parameters, options);
988         assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
989     }
990
991     /**
992      * <br/>
993      * 
994      * @throws ServiceException
995      * @since SDNO 0.5
996      */
997     @Test
998     public void testAsyncDeleteStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
999         final RestfulOptions options = new RestfulOptions();
1000         options.setRestTimeout(10);
1001
1002         final HttpBaseRest httpRest = getHttpRest(options);
1003         new MockUp<RestHttpContentExchange>() {
1004
1005             @Mock
1006             public int waitForDone() {
1007                 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
1008                 return HttpExchange.STATUS_COMPLETED;
1009             }
1010
1011             @Mock
1012             public RestfulResponse getResponse() throws IOException {
1013                 final RestfulResponse response = new RestfulResponse();
1014                 response.setStatus(HttpExchange.STATUS_COMPLETED);
1015                 return response;
1016             }
1017
1018         };
1019
1020         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
1021
1022             @Override
1023             public void callback(final RestfulResponse response) {
1024                 System.out.println("callback called.");
1025
1026             }
1027
1028             @Override
1029             public void handleExcepion(final Throwable e) {
1030
1031                 System.out.println("handleExcepion called.");
1032             }
1033
1034         };
1035         httpRest.asyncDelete("path/to/service", new RestfulParametes(), callback);
1036         httpRest.asyncDelete("path/to/service", new RestfulParametes(), null);
1037     }
1038
1039     /**
1040      * <br/>
1041      * 
1042      * @throws ServiceException
1043      * @since SDNO 0.5
1044      */
1045     @Test
1046     public void testAsyncDeleteStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
1047         final RestfulOptions options = new RestfulOptions();
1048         options.setRestTimeout(10);
1049
1050         final HttpBaseRest httpRest = getHttpRest(options);
1051         new MockUp<RestHttpContentExchange>() {
1052
1053             @Mock
1054             public int waitForDone() {
1055                 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
1056                 return HttpExchange.STATUS_COMPLETED;
1057             }
1058
1059             @Mock
1060             public RestfulResponse getResponse() throws IOException {
1061                 final RestfulResponse response = new RestfulResponse();
1062                 response.setStatus(HttpExchange.STATUS_COMPLETED);
1063                 return response;
1064             }
1065
1066         };
1067
1068         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
1069
1070             @Override
1071             public void callback(final RestfulResponse response) {
1072                 System.out.println("callback called.");
1073
1074             }
1075
1076             @Override
1077             public void handleExcepion(final Throwable e) {
1078
1079                 System.out.println("handleExcepion called.");
1080             }
1081
1082         };
1083         httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, callback);
1084         httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, null);
1085     }
1086
1087     /**
1088      * <br/>
1089      * 
1090      * @throws ServiceException
1091      * @since SDNO 0.5
1092      */
1093     @Ignore
1094     @Test
1095     public void testPatchStringRestfulParametes() throws ServiceException {
1096         final RestfulOptions options = new RestfulOptions();
1097
1098         final HttpBaseRest httpRest = getHttpRest(options);
1099         new MockUp<RestHttpContentExchange>() {
1100
1101             @Mock
1102             public int waitForDone() {
1103                 System.out.println("waitForDone" + HttpExchange.STATUS_COMPLETED);
1104                 return HttpExchange.STATUS_COMPLETED;
1105             }
1106
1107             @Mock
1108             public RestfulResponse getResponse() throws IOException {
1109                 final RestfulResponse response = new RestfulResponse();
1110                 response.setStatus(HttpExchange.STATUS_COMPLETED);
1111                 return response;
1112             }
1113
1114         };
1115         final RestfulParametes parameters = new RestfulParametes();
1116         parameters.put("id", "1234");
1117         parameters.put("name", "some-name");
1118         parameters.put("address", null);
1119         parameters.setRawData("{ \"data\"=\"sample JSON data\"");
1120         parameters.putHttpContextHeader("Content-Type", "");
1121         parameters.putHttpContextHeader("Accept-Encoding", "");
1122         new Expectations() {
1123
1124             ServiceUtil serviceUtil;
1125
1126             {
1127                 serviceUtil = new ServiceUtil(anyString, anyString);
1128                 serviceUtil.getServiceHost();
1129                 returns("127.0.0.1");
1130
1131                 serviceUtil.getServicePort();
1132                 returns(10);
1133             }
1134
1135         };
1136         final RestfulResponse response = httpRest.patch("path/to/service", parameters);
1137         assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
1138     }
1139
1140     /**
1141      * <br/>
1142      * 
1143      * @throws ServiceException
1144      * @since SDNO 0.5
1145      */
1146     @Ignore
1147     @Test
1148     public void testPatchStringRestfulParametesRestfulOptions() throws ServiceException {
1149         final RestfulOptions options = new RestfulOptions();
1150
1151         final HttpBaseRest httpRest = getHttpRest(options);
1152         new MockUp<RestHttpContentExchange>() {
1153
1154             @Mock
1155             public int waitForDone() {
1156                 System.out.println("waitForDone" + HttpExchange.STATUS_COMPLETED);
1157                 return HttpExchange.STATUS_COMPLETED;
1158             }
1159
1160             @Mock
1161             public RestfulResponse getResponse() throws IOException {
1162                 final RestfulResponse response = new RestfulResponse();
1163                 response.setStatus(HttpExchange.STATUS_COMPLETED);
1164                 return response;
1165             }
1166
1167         };
1168         final RestfulParametes parameters = new RestfulParametes();
1169         parameters.put("id", "1234");
1170         parameters.put("name", "some-name");
1171         parameters.put("address", null);
1172         parameters.setRawData("{ \"data\"=\"sample JSON data\"");
1173         parameters.putHttpContextHeader("Content-Type", "");
1174         parameters.putHttpContextHeader("Accept-Encoding", "");
1175         new Expectations() {
1176
1177             ServiceUtil serviceUtil;
1178
1179             {
1180                 serviceUtil = new ServiceUtil(anyString, anyString);
1181                 serviceUtil.getServiceHost();
1182                 returns("127.0.0.1");
1183
1184                 serviceUtil.getServicePort();
1185                 returns(10);
1186             }
1187
1188         };
1189         final RestfulResponse response = httpRest.patch("path/to/service", parameters, options);
1190         assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
1191     }
1192
1193     /**
1194      * <br/>
1195      * 
1196      * @throws ServiceException
1197      * @since SDNO 0.5
1198      */
1199     @Test
1200     public void testAsyncPatchStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
1201         final RestfulOptions options = new RestfulOptions();
1202         options.setRestTimeout(10);
1203
1204         final HttpBaseRest httpRest = getHttpRest(options);
1205         new MockUp<RestHttpContentExchange>() {
1206
1207             @Mock
1208             public int waitForDone() {
1209                 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
1210                 return HttpExchange.STATUS_COMPLETED;
1211             }
1212
1213             @Mock
1214             public RestfulResponse getResponse() throws IOException {
1215                 final RestfulResponse response = new RestfulResponse();
1216                 response.setStatus(HttpExchange.STATUS_COMPLETED);
1217                 return response;
1218             }
1219
1220         };
1221
1222         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
1223
1224             @Override
1225             public void callback(final RestfulResponse response) {
1226                 System.out.println("callback called.");
1227
1228             }
1229
1230             @Override
1231             public void handleExcepion(final Throwable e) {
1232
1233                 System.out.println("handleExcepion called.");
1234             }
1235
1236         };
1237         httpRest.asyncPatch("path/to/service", new RestfulParametes(), callback);
1238         httpRest.asyncPatch("path/to/service", new RestfulParametes(), null);
1239     }
1240
1241     /**
1242      * <br/>
1243      * 
1244      * @throws ServiceException
1245      * @since SDNO 0.5
1246      */
1247     @Test
1248     public void testAsyncPatchStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
1249         final RestfulOptions options = new RestfulOptions();
1250         options.setRestTimeout(10);
1251
1252         final HttpBaseRest httpRest = getHttpRest(options);
1253         new MockUp<RestHttpContentExchange>() {
1254
1255             @Mock
1256             public int waitForDone() {
1257                 System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
1258                 return HttpExchange.STATUS_COMPLETED;
1259             }
1260
1261             @Mock
1262             public RestfulResponse getResponse() throws IOException {
1263                 final RestfulResponse response = new RestfulResponse();
1264                 response.setStatus(HttpExchange.STATUS_COMPLETED);
1265                 return response;
1266             }
1267
1268         };
1269
1270         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
1271
1272             @Override
1273             public void callback(final RestfulResponse response) {
1274                 System.out.println("callback called.");
1275
1276             }
1277
1278             @Override
1279             public void handleExcepion(final Throwable e) {
1280
1281                 System.out.println("handleExcepion called.");
1282             }
1283
1284         };
1285         httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, callback);
1286         httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, null);
1287     }
1288
1289 }