84de1a2576daf40a22c61aa12babc3a28b2b1b01
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / test / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / common / restclient / TestHttpRest.java
1 /*
2  * Copyright 2017 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.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.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.lang.reflect.Field;
24 import java.util.List;
25
26 import org.eclipse.jetty.client.HttpClient;
27 import org.eclipse.jetty.client.HttpContentResponse;
28 import org.eclipse.jetty.client.HttpExchange;
29 import org.eclipse.jetty.client.HttpRequest;
30 import org.eclipse.jetty.client.api.ContentResponse;
31 import org.eclipse.jetty.client.api.Request;
32 import org.eclipse.jetty.http.HttpFields;
33 import org.eclipse.jetty.http.HttpVersion;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.junit.runner.RunWith;
43
44 import mockit.Mock;
45 import mockit.MockUp;
46 import mockit.integration.junit4.JMockit;
47
48 /**
49  * <br/>
50  * <p>
51  * </p>
52  *
53  * @author
54  */
55 @RunWith(JMockit.class)
56 public class TestHttpRest {
57
58     @Rule
59     public ExpectedException thrown = ExpectedException.none();
60
61     /**
62      * <br/>
63      *
64      * @throws java.lang.Exception
65      * @since
66      */
67     static HttpClient httpClient;
68     static RestfulOptions options;
69
70     @BeforeClass
71     public static void setUpBeforeClass() throws Exception {
72         options = new RestfulOptions();
73         options.setOption("thread", new Integer(5));
74         options.setOption("maxConnectionPerAddr", new Integer(200));
75         options.setOption("ConnectTimeout", new Integer(500));
76         options.setHost("localhost");
77
78         httpClient = new HttpClient();
79         httpClient.start();
80         Request request = httpClient.newRequest("http://reqres.in/api/users/2");
81         ContentResponse contentResponse = request.send();
82         new MockUp<HttpBaseRest>() {
83             @Mock
84             public ContentResponse getResponse() {
85                 return contentResponse;
86             }
87         };
88     }
89
90     /**
91      * <br/>
92      *
93      * @throws java.lang.Exception
94      * @since
95      */
96     @AfterClass
97     public static void tearDownAfterClass() throws Exception {
98         httpClient.stop();
99     }
100
101     /**
102      * <br/>
103      *
104      * @throws java.lang.Exception
105      * @since
106      */
107     @Before
108     public void setUp() throws Exception {
109     }
110
111     /**
112      * <br/>
113      *
114      * @throws java.lang.Exception
115      * @since
116      */
117     @After
118     public void tearDown() throws Exception {
119     }
120
121     /**
122      * <br/>
123      *
124      * @throws Exception
125      * @since
126      */
127     @Test
128     public void testInitHttpRest() throws Exception {
129         final RestfulOptions options = new RestfulOptions();
130         new MockUp<HttpClient>() {
131
132             @Mock
133             public void doStart() {
134             }
135         };
136         final HttpRest httpRest = new HttpRest();
137         httpRest.initHttpRest(options);
138         final Field httpClient = HttpBaseRest.class.getDeclaredField("client");
139         httpClient.setAccessible(true);
140         Assert.assertNotNull(httpClient.get(httpRest));
141     }
142 //
143 //    /**
144 //     * <br/>
145 //     *
146 //     * @throws NoSuchFieldException
147 //     * @throws Exception
148 //     * @since
149 //     */
150 //    @Test
151 //    public void testCreateRestHttpContentExchange() throws NoSuchFieldException, Exception {
152 //        final HttpBaseRest httpRest = new HttpRest();
153 //        final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
154 //
155 //            @Override
156 //            public void callback(final RestfulResponse response) {
157 //
158 //            }
159 //
160 //            @Override
161 //            public void handleExcepion(final Throwable e) {
162 //
163 //            }
164 //
165 //        };
166 //         final RestHttpContentExchange exchange = httpRest.createRestHttpContentExchange(callback);
167 //          assertNotNull(exchange);
168 //        final Field callbackField = RestHttpContentExchange.class.getDeclaredField("callback");
169 //        assertNotNull(callbackField);
170 //    }
171
172     /**
173      * <br/>
174      *
175      * @throws Exception
176      * @since
177      */
178     @Test
179     public void testGetStringRestfulParametes() throws Exception {
180         final HttpRest httpRest = getHttpRest(options);
181         final RestfulParametes parametes = new RestfulParametes();
182         parametes.put("id", "1234");
183         parametes.put("name", "some-name");
184         parametes.put("address", null);
185         parametes.putHttpContextHeader("Content-Type", "application/json");
186         parametes.putHttpContextHeader("Accept-Encoding", "*/*");
187         final RestfulResponse response = httpRest.get("path/to/service", parametes);
188         assertEquals(200, response.getStatus());
189
190     }
191
192
193     /**
194      * <br/>
195      *
196      * @throws Exception
197      * @since
198      */
199     @Test
200     public void testGetStringRestfulParametesRestfulOptions() throws Exception {
201         final HttpRest httpRest = getHttpRest(options);
202         final RestfulResponse response = httpRest.get("path/to/service", new RestfulParametes(), options);
203         assertEquals(200, response.getStatus());
204     }
205
206
207     /**
208      * <br/>
209      *
210      * @throws Exception
211      * @since
212      */
213     @Test
214     public void testHeadStringRestfulParametes() throws Exception {
215
216         final HttpRest httpRest = getHttpRest(options);
217         final RestfulParametes parametes = new RestfulParametes();
218         parametes.put("id", "1234");
219         parametes.put("name", "some-name");
220         parametes.put("address", null);
221         parametes.putHttpContextHeader("Content-Type", "");
222         parametes.putHttpContextHeader("Accept-Encoding", "");
223         final RestfulResponse response = httpRest.head("path/to/service", parametes);
224         assertEquals(200, response.getStatus());
225     }
226
227     /**
228      * <br/>
229      *
230      * @throws Exception
231      * @since
232      */
233     @Test
234     public void testHeadStringRestfulParametesRestfulOptions() throws Exception {
235
236         final HttpRest httpRest = getHttpRest(options);
237         final RestfulParametes parametes = new RestfulParametes();
238         parametes.put("id", "1234");
239         parametes.put("name", "some-name");
240         parametes.put("address", null);
241         parametes.putHttpContextHeader("Content-Type", "");
242         parametes.putHttpContextHeader("Accept-Encoding", "");
243         final RestfulResponse response = httpRest.head("path/to/service", parametes, options);
244         assertEquals(200, response.getStatus());
245     }
246
247     /**
248      * <br/>
249      *
250      * @param options
251      * @return
252      * @throws ServiceException
253      * @since
254      */
255     private HttpRest getHttpRest(final RestfulOptions options) throws ServiceException {
256         final HttpRest httpRest = new HttpRest();
257         {
258 //            new MockUp<HttpClient>() {
259 //
260 //                @Mock
261 //                public void doStart() {
262 //                }
263 //
264 //                @Mock
265 //                public void send(final HttpExchange exchange) throws IOException {
266 //                }
267 //            };
268             httpRest.initHttpRest(options);
269
270         }
271         return httpRest;
272     }
273
274     /**
275      * <br/>
276      *
277      * @throws Exception
278      * @since
279      */
280     @Test
281     public void testAsyncGetStringRestfulParametesRestfulAsyncCallback() throws Exception {
282
283
284         final HttpRest httpRest = getHttpRest(options);
285         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
286
287             @Override
288             public void callback(final RestfulResponse response) {
289
290             }
291
292             @Override
293             public void handleExcepion(final Throwable e) {
294
295             }
296
297         };
298         httpRest.asyncGet("path/to/service", new RestfulParametes(), callback);
299         httpRest.asyncGet("path/to/service", new RestfulParametes(), null);
300     }
301
302     /**
303      * <br/>
304      *
305      * @throws ServiceException
306      * @since
307      */
308     @Test
309     public void testAsyncGetStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
310
311
312         final HttpRest httpRest = getHttpRest(options);
313         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
314
315             @Override
316             public void callback(final RestfulResponse response) {
317
318             }
319
320             @Override
321             public void handleExcepion(final Throwable e) {
322
323             }
324
325         };
326         httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), callback);
327         httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), null);
328     }
329
330     /**
331      * <br/>
332      *
333      * @throws ServiceException
334      * @since
335      */
336     @Test
337     public void testPutStringRestfulParametes() throws ServiceException {
338
339         final HttpRest httpRest = getHttpRest(options);
340         final RestfulParametes parametes = new RestfulParametes();
341         parametes.put("id", "1234");
342         parametes.put("name", "some-name");
343         parametes.put("address", null);
344         parametes.putHttpContextHeader("Content-Type", "");
345         parametes.putHttpContextHeader("Accept-Encoding", "");
346         final RestfulResponse response = httpRest.put("path/to/service", parametes);
347         assertEquals(200, response.getStatus());
348     }
349
350     /**
351      * <br/>
352      *
353      * @throws ServiceException
354      * @since
355      */
356     @Test
357     public void testPutStringRestfulParametesRestfulOptions() throws ServiceException {
358
359         final HttpRest httpRest = getHttpRest(options);
360         final RestfulParametes parametes = new RestfulParametes();
361         parametes.put("id", "1234");
362         parametes.put("name", "some-name");
363         parametes.put("address", null);
364         parametes.putHttpContextHeader("Content-Type", "");
365         parametes.putHttpContextHeader("Accept-Encoding", "");
366         final RestfulResponse response = httpRest.put("path/to/service", parametes, null);
367         assertEquals(200, response.getStatus());
368     }
369
370     /**
371      * <br/>
372      *
373      * @throws ServiceException
374      * @since
375      */
376     @Test
377     public void testAsyncPutStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
378
379
380         final HttpRest httpRest = getHttpRest(options);
381
382         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
383
384             @Override
385             public void callback(final RestfulResponse response) {
386
387             }
388
389             @Override
390             public void handleExcepion(final Throwable e) {
391
392             }
393
394         };
395         httpRest.asyncPut("path/to/service", new RestfulParametes(), callback);
396         httpRest.asyncPut("path/to/service", new RestfulParametes(), null);
397     }
398
399     /**
400      * <br/>
401      *
402      * @throws Exception
403      * @since
404      */
405     @Test
406     public void testAsyncPutStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws Exception {
407
408         final HttpRest httpRest = getHttpRest(options);
409
410         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
411
412             @Override
413             public void callback(final RestfulResponse response) {
414
415             }
416
417             @Override
418             public void handleExcepion(final Throwable e) {
419
420             }
421
422         };
423         httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), callback);
424         httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), null);
425     }
426
427     /**
428      * <br/>
429      *
430      * @throws Exception
431      * @since
432      */
433     @Test
434     public void testAsyncPostStringRestfulParametesRestfulAsyncCallback() throws Exception {
435
436         options.setRestTimeout(10);
437
438         final HttpBaseRest httpRest = getHttpRest(options);
439         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
440
441             @Override
442             public void callback(final RestfulResponse response) {
443
444             }
445
446             @Override
447             public void handleExcepion(final Throwable e) {
448
449             }
450
451         };
452         httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback);
453         httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null);
454     }
455
456     /**
457      * <br/>
458      *
459      * @throws ServiceException
460      * @since
461      */
462     @Test
463     public void testAsyncPostStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
464
465         options.setRestTimeout(10);
466
467         final HttpBaseRest httpRest = getHttpRest(options);
468
469         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
470
471             @Override
472             public void callback(final RestfulResponse response) {
473
474             }
475
476             @Override
477             public void handleExcepion(final Throwable e) {
478
479             }
480
481         };
482         httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback);
483         httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null);
484     }
485
486     /**
487      * <br/>
488      *
489      * @throws ServiceException
490      * @since
491      */
492     @Test
493     public void testDeleteStringRestfulParametes() throws ServiceException {
494
495         final HttpBaseRest httpRest = getHttpRest(options);
496
497         final RestfulResponse response = httpRest.delete("path/to/service", null);
498         assertEquals(-1, response.getStatus());
499     }
500
501     /**
502      * <br/>
503      *
504      * @throws ServiceException
505      * @since
506      */
507     @Test
508     public void testDeleteStringRestfulParametesRestfulOptions() throws ServiceException {
509
510
511         final HttpBaseRest httpRest = getHttpRest(options);
512         final RestfulParametes parameters = new RestfulParametes();
513         parameters.put("id", "1234");
514         parameters.put("name", "some-name");
515         parameters.put("address", null);
516         parameters.setRawData("{ \"data\"=\"sample JSON data\"");
517         parameters.putHttpContextHeader("Content-Type", "");
518         parameters.putHttpContextHeader("Accept-Encoding", "");
519         final RestfulResponse response = httpRest.delete("path/to/service", parameters, options);
520         assertEquals(200, response.getStatus());
521     }
522
523     /**
524      * <br/>
525      *
526      * @throws ServiceException
527      * @since
528      */
529     @Test
530     public void testAsyncDeleteStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
531
532         options.setRestTimeout(10);
533
534         final HttpBaseRest httpRest = getHttpRest(options);
535         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
536
537             @Override
538             public void callback(final RestfulResponse response) {
539
540             }
541
542             @Override
543             public void handleExcepion(final Throwable e) {
544
545             }
546
547         };
548         httpRest.asyncDelete("path/to/service", new RestfulParametes(), callback);
549         httpRest.asyncDelete("path/to/service", new RestfulParametes(), null);
550     }
551
552     /**
553      * <br/>
554      *
555      * @throws ServiceException
556      * @since
557      */
558     @Test
559     public void testAsyncDeleteStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
560
561         options.setRestTimeout(10);
562
563         final HttpBaseRest httpRest = getHttpRest(options);
564
565         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
566
567             @Override
568             public void callback(final RestfulResponse response) {
569
570             }
571
572             @Override
573             public void handleExcepion(final Throwable e) {
574
575             }
576
577         };
578         httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, callback);
579         httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, null);
580     }
581
582     /**
583      * <br/>
584      *
585      * @throws ServiceException
586      * @since
587      */
588     @Test
589     public void testPatchStringRestfulParametes() throws ServiceException {
590
591         final HttpBaseRest httpRest = getHttpRest(options);
592         final RestfulParametes parameters = new RestfulParametes();
593         parameters.put("id", "1234");
594         parameters.put("name", "some-name");
595         parameters.put("address", null);
596         parameters.setRawData("{ \"data\"=\"sample JSON data\"");
597         parameters.putHttpContextHeader("Content-Type", "");
598         parameters.putHttpContextHeader("Accept-Encoding", "");
599         final RestfulResponse response = httpRest.patch("path/to/service", parameters);
600         assertEquals(200, response.getStatus());
601     }
602
603     /**
604      * <br/>
605      *
606      * @throws ServiceException
607      * @since
608      */
609     @Test
610     public void testPatchStringRestfulParametesRestfulOptions() throws ServiceException {
611
612         final HttpBaseRest httpRest = getHttpRest(options);
613         final RestfulParametes parameters = new RestfulParametes();
614         parameters.put("id", "1234");
615         parameters.put("name", "some-name");
616         parameters.put("address", null);
617         parameters.setRawData("{ \"data\"=\"sample JSON data\"");
618         parameters.putHttpContextHeader("Content-Type", "");
619         parameters.putHttpContextHeader("Accept-Encoding", "");
620         final RestfulResponse response = httpRest.patch("path/to/service", parameters, options);
621         assertEquals(200, response.getStatus());
622     }
623
624     /**
625      * <br/>
626      *
627      * @throws ServiceException
628      * @since
629      */
630     @Test
631     public void testAsyncPatchStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
632
633         options.setRestTimeout(10);
634
635         final HttpBaseRest httpRest = getHttpRest(options);
636
637         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
638
639             @Override
640             public void callback(final RestfulResponse response) {
641
642             }
643
644             @Override
645             public void handleExcepion(final Throwable e) {
646
647             }
648
649         };
650         httpRest.asyncPatch("path/to/service", new RestfulParametes(), callback);
651         httpRest.asyncPatch("path/to/service", new RestfulParametes(), null);
652     }
653
654     /**
655      * <br/>
656      *
657      * @throws ServiceException
658      * @since
659      */
660     @Test
661     public void testAsyncPatchStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
662
663         options.setRestTimeout(10);
664
665         final HttpBaseRest httpRest = getHttpRest(options);
666
667         final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
668
669             @Override
670             public void callback(final RestfulResponse response) {
671
672             }
673
674             @Override
675             public void handleExcepion(final Throwable e) {
676
677             }
678
679         };
680         httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, callback);
681         httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, null);
682     }
683
684 }