-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
JAXRSClientServerSpringBookTest.java
1078 lines (938 loc) · 44.4 KB
/
JAXRSClientServerSpringBookTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.systest.jaxrs;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.ext.xml.XMLSource;
import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
import org.apache.cxf.jaxrs.model.wadl.WadlGenerator;
import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
import org.apache.cxf.jaxrs.provider.aegis.AegisElementProvider;
import org.apache.cxf.staxutils.StaxUtils;
import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.ws.commons.schema.constants.Constants;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class JAXRSClientServerSpringBookTest extends AbstractBusClientServerTestBase {
public static final String PORT = BookServerSpring.PORT;
@BeforeClass
public static void startServers() throws Exception {
AbstractResourceInfo.clearAllMaps();
assertTrue("server did not launch correctly",
launchServer(BookServerSpring.class, true));
createStaticBus();
}
@Test
public void testGetGenericBook() throws Exception {
String baseAddress = "http://localhost:" + PORT + "/the/thebooks8/books";
WebClient wc = WebClient.create(baseAddress);
Long id = wc.type("application/xml").accept("text/plain").post(new Book("CXF", 1L), Long.class);
assertEquals(Long.valueOf(1), id);
Book book = wc.replaceHeader("Accept", "application/xml").query("id", 1L).get(Book.class);
assertEquals("CXF", book.getName());
}
@Test
public void testGetDocuments() throws Exception {
String baseAddress = "http://localhost:" + PORT + "/the/thedocs/resource/doc";
WebClient wc = WebClient.create(baseAddress);
Response r = wc.accept("application/json").get();
assertEquals("[{\"t\":\"doc\"}]", r.readEntity(String.class));
}
@Test
public void testGetBookWebEx() throws Exception {
final String address = "http://localhost:" + PORT + "/the/thebooks/bookstore/books/webex";
doTestGetBookWebEx(address);
}
@Test
public void testGetBookText() throws Exception {
final String address = "http://localhost:" + PORT + "/the/thebooks/bookstore/books/text";
WebClient wc = WebClient.create(address).accept("text/*");
WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000);
assertEquals(406, wc.get().getStatus());
}
@Test
public void testGetServicesPageNotFound() throws Exception {
final String address = "http://localhost:" + PORT + "/the/services;a=b";
WebClient wc = WebClient.create(address).accept("text/*");
WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000);
assertEquals(404, wc.get().getStatus());
}
@Test
public void testGetServicesPage() throws Exception {
final String address = "http://localhost:" + PORT + "/the/services";
WebClient wc = WebClient.create(address).accept("text/*");
String s = wc.get(String.class);
assertTrue(s.contains("href=\"/the/services/?stylesheet=1\""));
assertTrue(s.contains("<title>CXF - Service list</title>"));
assertTrue(s.contains("<a href=\"http://localhost:" + PORT + "/the/"));
}
@Test
public void testGetServicesPageWithServletPatternMatchOnly() throws Exception {
final String address = "http://localhost:" + PORT + "/the/;a=b";
WebClient wc = WebClient.create(address).accept("text/*");
String s = wc.get(String.class);
assertTrue(s.contains("href=\"/the/?stylesheet=1\""));
assertTrue(s.contains("<title>CXF - Service list</title>"));
assertFalse(s.contains(";a=b"));
assertTrue(s.contains("<a href=\"http://localhost:" + PORT + "/the/"));
}
@Test
public void testGetServicesPageWithServletPatternMatchOnly2() throws Exception {
final String address = "http://localhost:" + PORT + "/services;a=b;/list;a=b/;a=b";
WebClient wc = WebClient.create(address).accept("text/*");
String s = wc.get(String.class);
assertTrue(s.contains("href=\"/services/list/?stylesheet=1\""));
assertTrue(s.contains("<title>CXF - Service list</title>"));
assertFalse(s.contains(";a=b"));
assertTrue(s.contains("<a href=\"http://localhost:" + PORT + "/services/list/"));
}
@Test
public void testStaticResourcesWithRedirectQueryCheck() throws Exception {
final String address = "http://localhost:" + PORT + "/services/?.html";
WebClient wc = WebClient.create(address).accept("text/*");
String s = wc.get(String.class);
// Check we don't have a directory listing
assertFalse(s.contains("META-INF"));
}
@Test
public void testEchoBookForm() throws Exception {
String address = "http://localhost:" + PORT + "/bus/thebooksform/bookform";
doTestEchoBookForm(address);
}
@Test
public void testEchoBookForm2() throws Exception {
String address = "http://localhost:" + PORT + "/bus/thebooksform/bookform2";
doTestEchoBookForm(address);
}
@Test
public void testEchoBookForm3() throws Exception {
String address = "http://localhost:" + PORT + "/bus/thebooksform/bookform3";
doTestEchoBookForm(address);
}
@Test
public void testEchoBookForm4() throws Exception {
String address = "http://localhost:" + PORT + "/bus/thebooksform/bookform4";
doTestEchoBookForm(address);
}
@Test
public void testEchoBookForm5() throws Exception {
String address = "http://localhost:" + PORT + "/bus/thebooksform/bookform5";
doTestEchoBookForm(address);
}
private void doTestEchoBookForm(String address) throws Exception {
WebClient wc = WebClient.create(address);
WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L);
Book b =
wc.form(new Form().param("name", "CXFForm").param("id", "125"))
.readEntity(Book.class);
assertEquals("CXFForm", b.getName());
assertEquals(125L, b.getId());
}
@Test
public void testEchoBookFormXml() throws Exception {
String address = "http://localhost:" + PORT + "/bus/thebooksform/bookform";
WebClient wc = WebClient.create(address);
Book b =
wc.type("application/xml").post(new Book("CXFFormXml", 125L))
.readEntity(Book.class);
assertEquals("CXFFormXml", b.getName());
assertEquals(125L, b.getId());
}
@Test
public void testGetBookWebEx4() throws Exception {
final String address = "http://localhost:" + PORT + "/the/thebooks%203/bookstore/books/webex2";
doTestGetBookWebEx(address);
}
private void doTestGetBookWebEx(String address) throws Exception {
WebClient wc = WebClient.create(address);
WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L);
try {
wc.accept("text/plain", "application/json").get(Book.class);
fail("InternalServerErrorException is expected");
} catch (InternalServerErrorException ex) {
String errorMessage = ex.getResponse().readEntity(String.class);
assertEquals("Book web exception", errorMessage);
}
}
@Test
public void testPostGeneratedBook() throws Exception {
String baseAddress = "http://localhost:" + PORT + "/the/generated";
JAXBElementProvider<?> provider = new JAXBElementProvider<>();
provider.setJaxbElementClassMap(Collections.singletonMap(
"org.apache.cxf.systest.jaxrs.codegen.schema.Book",
"{http://superbooks}thebook"));
org.apache.cxf.systest.jaxrs.codegen.service.BookStore bookStore =
JAXRSClientFactory.create(baseAddress,
org.apache.cxf.systest.jaxrs.codegen.service.BookStore.class,
Collections.singletonList(provider));
org.apache.cxf.systest.jaxrs.codegen.schema.Book book =
new org.apache.cxf.systest.jaxrs.codegen.schema.Book();
book.setId(123);
bookStore.addBook(123, book);
Response r = WebClient.client(bookStore).getResponse();
assertEquals(204, r.getStatus());
}
@Test
public void testGetWadlFromWadlLocation() throws Exception {
String address = "http://localhost:" + PORT + "/the/generated";
WebClient client = WebClient.create(address + "/bookstore" + "?_wadl&_type=xml");
Document doc = StaxUtils.read(new InputStreamReader(client.get(InputStream.class), StandardCharsets.UTF_8));
List<Element> resources = checkWadlResourcesInfo(doc, address, "/schemas/book.xsd", 2);
assertEquals("", resources.get(0).getAttribute("type"));
String type = resources.get(1).getAttribute("type");
String resourceTypeAddress = address + "/bookstoreImportResourceType.wadl#bookstoreType";
assertEquals(resourceTypeAddress, type);
checkSchemas(address, "/schemas/book.xsd", "/schemas/chapter.xsd", "include");
checkSchemas(address, "/schemas/chapter.xsd", null, null);
// check resource type resource
checkWadlResourcesType(address, resourceTypeAddress, "/schemas/book.xsd");
String templateRef = null;
NodeList nd = doc.getChildNodes();
for (int i = 0; i < nd.getLength(); i++) {
Node n = nd.item(i);
if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
String piData = ((ProcessingInstruction)n).getData();
int hRefStart = piData.indexOf("href=\"");
if (hRefStart > 0) {
int hRefEnd = piData.indexOf("\"", hRefStart + 6);
templateRef = piData.substring(hRefStart + 6, hRefEnd);
}
}
}
assertNotNull(templateRef);
WebClient client2 = WebClient.create(templateRef);
WebClient.getConfig(client2).getHttpConduit().getClient().setReceiveTimeout(1000000L);
String template = client2.get(String.class);
assertNotNull(template);
assertTrue(template.indexOf("<xsl:stylesheet") != -1);
}
@Test
public void testGetGeneratedWadlWithExternalSchemas() throws Exception {
String address = "http://localhost:" + PORT + "/the/bookstore";
checkWadlResourcesInfo(address, address, "/book.xsd", 2);
checkSchemas(address, "/book.xsd", "/bookid.xsd", "import");
checkSchemas(address, "/bookid.xsd", null, null);
checkWadlResourcesInfo(address, address, "/book.xsd", 2);
}
@Test
public void testGetBookISOJson() throws Exception {
doTestGetBookISO("ISO-8859-1", "1");
}
@Test
public void testGetBookISO2Json() throws Exception {
doTestGetBookISO("ISO-8859-1", "2");
}
@Test
public void testGetBookISO3Json() throws Exception {
doTestGetBookISO(null, "1");
}
@Test
public void testGetBookISOXML() throws Exception {
doTestGetBookISOXML("ISO-8859-1", "1");
}
@Test
public void testGetBookISOXML2() throws Exception {
doTestGetBookISOXML("ISO-8859-1", "2");
}
@Test
public void testGetBookISOXML3() throws Exception {
doTestGetBookISOXML(null, "1");
}
@Test
public void testGetBookLink() throws Exception {
String address = "http://localhost:" + PORT + "/the/bookstore/link";
WebClient wc = WebClient.create(address);
Response r = wc.get();
Link l = r.getLink("self");
assertEquals("<http://localhost:" + PORT + "/the/bookstore/>;rel=\"self\"",
l.toString());
}
private void doTestGetBookISO(String charset, String pathSegment) throws Exception {
String address = "http://localhost:" + PORT + "/the/bookstore/ISO-8859-1/" + pathSegment;
WebClient wc = WebClient.create(address);
wc.accept("application/json" + (charset == null ? "" : ";charset=ISO-8859-1"));
byte[] iso88591bytes = wc.get(byte[].class);
String helloStringISO88591 = new String(iso88591bytes, "ISO-8859-1");
String name = helloStringISO88591.substring(
helloStringISO88591.indexOf("\"name\":\"") + "\"name\":\"".length(),
helloStringISO88591.lastIndexOf('"'));
compareNames(name);
}
private void doTestGetBookISOXML(String charset, String pathSegment) throws Exception {
String address = "http://localhost:" + PORT + "/the/bookstore/ISO-8859-1/" + pathSegment;
WebClient wc = WebClient.create(address);
WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L);
wc.accept("application/xml" + (charset == null ? "" : ";charset=ISO-8859-1"));
byte[] iso88591bytes = wc.get(byte[].class);
String helloStringISO88591 = new String(iso88591bytes, "ISO-8859-1");
String name = helloStringISO88591.substring(
helloStringISO88591.indexOf("<name>") + "<name>".length(),
helloStringISO88591.indexOf("</name>"));
compareNames(name);
}
private void compareNames(String name) throws Exception {
String eWithAcute = "\u00E9";
String nameUTF16 = "F" + eWithAcute + "lix";
String nameExpected = new String(nameUTF16.getBytes("ISO-8859-1"), "ISO-8859-1");
assertEquals(nameExpected, name);
}
private void checkSchemas(String address, String schemaSegment,
String includedSchema,
String refAttrName) throws Exception {
WebClient client = WebClient.create(address + schemaSegment);
WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(10000000L);
Document doc = StaxUtils.read(new InputStreamReader(client.get(InputStream.class), StandardCharsets.UTF_8));
Element root = doc.getDocumentElement();
assertEquals(Constants.URI_2001_SCHEMA_XSD, root.getNamespaceURI());
assertEquals("schema", root.getLocalName());
if (includedSchema != null) {
List<Element> includeEls = DOMUtils.getChildrenWithName(root,
Constants.URI_2001_SCHEMA_XSD,
refAttrName);
assertEquals(1, includeEls.size());
String href = includeEls.get(0).getAttribute("schemaLocation");
assertEquals(address + includedSchema, href);
}
}
private void checkWadlResourcesType(String baseURI, String requestTypeURI, String schemaRef) throws Exception {
WebClient client = WebClient.create(requestTypeURI);
WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(1000000);
Document doc = StaxUtils.read(new InputStreamReader(client.get(InputStream.class), StandardCharsets.UTF_8));
Element root = doc.getDocumentElement();
assertEquals(WadlGenerator.WADL_NS, root.getNamespaceURI());
assertEquals("application", root.getLocalName());
List<Element> grammarEls = DOMUtils.getChildrenWithName(root,
WadlGenerator.WADL_NS, "grammars");
assertEquals(1, grammarEls.size());
List<Element> includeEls = DOMUtils.getChildrenWithName(grammarEls.get(0),
WadlGenerator.WADL_NS, "include");
assertEquals(1, includeEls.size());
String href = includeEls.get(0).getAttribute("href");
assertEquals(baseURI + schemaRef, href);
List<Element> resourcesEls = DOMUtils.getChildrenWithName(root,
WadlGenerator.WADL_NS, "resources");
assertEquals(0, resourcesEls.size());
List<Element> resourceTypeEls =
DOMUtils.getChildrenWithName(root, WadlGenerator.WADL_NS, "resource_type");
assertEquals(1, resourceTypeEls.size());
}
private List<Element> checkWadlResourcesInfo(String baseURI, String requestURI,
String schemaRef, int size) throws Exception {
WebClient client = WebClient.create(requestURI + "?_wadl&_type=xml");
Document doc = StaxUtils.read(new InputStreamReader(client.get(InputStream.class), StandardCharsets.UTF_8));
return checkWadlResourcesInfo(doc, baseURI, schemaRef, size);
}
private List<Element> checkWadlResourcesInfo(Document doc, String baseURI, String schemaRef, int size)
throws Exception {
Element root = doc.getDocumentElement();
assertEquals(WadlGenerator.WADL_NS, root.getNamespaceURI());
assertEquals("application", root.getLocalName());
List<Element> grammarEls = DOMUtils.getChildrenWithName(root,
WadlGenerator.WADL_NS, "grammars");
assertEquals(1, grammarEls.size());
List<Element> includeEls = DOMUtils.getChildrenWithName(grammarEls.get(0),
WadlGenerator.WADL_NS, "include");
assertEquals(1, includeEls.size());
String href = includeEls.get(0).getAttribute("href");
assertEquals(baseURI + schemaRef, href);
List<Element> resourcesEls = DOMUtils.getChildrenWithName(root,
WadlGenerator.WADL_NS, "resources");
assertEquals(1, resourcesEls.size());
Element resourcesEl = resourcesEls.get(0);
assertEquals(baseURI, resourcesEl.getAttribute("base"));
List<Element> resourceEls =
DOMUtils.getChildrenWithName(resourcesEl,
WadlGenerator.WADL_NS, "resource");
assertEquals(size, resourceEls.size());
return resourceEls;
}
@Test
public void testGetBookByUriInfo() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks/bookstore/bookinfo?"
+ "param1=12¶m2=3";
getBook(endpointAddress, "resources/expected_get_book123json.txt");
}
@Test
public void testGetBookWithEncodedSemicolon() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks/bookstore/semicolon%3B";
WebClient client = WebClient.create(endpointAddress);
Book book = client.get(Book.class);
assertEquals(333L, book.getId());
assertEquals(";", book.getName());
}
@Test
public void testGetBookWithEncodedSemicolonAndMatrixParam() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks/bookstore/semicolon2%3B;a=b";
WebClient client = WebClient.create(endpointAddress);
WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(1000000);
Book book = client.get(Book.class);
assertEquals(333L, book.getId());
assertEquals(";b", book.getName());
}
@Test
@org.junit.Ignore("Fails after removing Xalan")
public void testGetBookXSLTHtml() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks5/bookstore/books/xslt";
WebClient wc = WebClient.create(endpointAddress);
wc.accept("application/xhtml+xml").path(666).matrix("name2", 2).query("name", "Action - ");
XMLSource source = wc.get(XMLSource.class);
source.setBuffering();
Map<String, String> namespaces = Collections.singletonMap("xhtml", "http://www.w3.org/1999/xhtml");
Book2 b = source.getNode("xhtml:html/xhtml:body/xhtml:ul/xhtml:Book", namespaces, Book2.class);
assertEquals(666, b.getId());
assertEquals("CXF in Action - 2", b.getName());
}
@Test
public void testGetBookByUriInfo2() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks%203/bookstore/bookinfo?"
+ "param1=12¶m2=3";
getBook(endpointAddress, "resources/expected_get_book123json.txt");
}
@Test
public void testGetBook123() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/bookstore/books/123";
getBook(endpointAddress, "resources/expected_get_book123json.txt");
getBook(endpointAddress, "resources/expected_get_book123json.txt",
"application/vnd.example-com.foo+json");
}
@Test
public void testBookDepthExceededXML() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks9/depth";
WebClient wc = WebClient.create(endpointAddress);
Response r = wc.type("application/xml").post(new Book("CXF", 123L));
assertEquals(413, r.getStatus());
}
@Test
public void testBookDepthExceededXMLStax() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks9stax/depth";
WebClient wc = WebClient.create(endpointAddress);
Response r = wc.type("application/xml").post(new Book("CXF", 123L));
assertEquals(413, r.getStatus());
}
@Test
public void testBookDepthExceededXMLSource() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks9/depth-source";
WebClient wc = WebClient.create(endpointAddress);
WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
Response r = wc.type("application/xml").post(new Book("CXF", 123L));
assertEquals(413, r.getStatus());
}
@Test
public void testBookDepthExceededXMLSourceStax() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks9stax/depth-source";
WebClient wc = WebClient.create(endpointAddress);
Response r = wc.type("application/xml").post(new Book("CXF", 123L));
assertEquals(413, r.getStatus());
}
@Test
public void testBookDepthExceededXMLDom() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks9/depth-dom";
WebClient wc = WebClient.create(endpointAddress);
Response r = wc.type("application/xml").post(new Book("CXF", 123L));
assertEquals(413, r.getStatus());
}
@Test
public void testBookDepthExceededXMLDomStax() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks9stax/depth-dom";
WebClient wc = WebClient.create(endpointAddress);
Response r = wc.type("application/xml").post(new Book("CXF", 123L));
assertEquals(413, r.getStatus());
}
@Test
public void testBookDepthExceededJettison() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks10/depth";
WebClient wc = WebClient.create(endpointAddress);
wc.accept("application/json").type("application/json");
Response r = wc.post(new Book("CXF", 123L));
assertEquals(413, r.getStatus());
}
@Test
public void testTooManyFormParams() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks9/depth-form";
WebClient wc = WebClient.create(endpointAddress);
Response r = wc.form(new Form().param("a", "b"));
assertEquals(204, r.getStatus());
r = wc.form(new Form().param("a", "b").param("c", "b"));
assertEquals(413, r.getStatus());
}
@Test
public void testGetBookJsonp() throws Exception {
String url = "http://localhost:" + PORT + "/the/jsonp/books/123";
WebClient client = WebClient.create(url);
client.accept("application/json, application/x-javascript");
client.query("_jsonp", "callback");
Response r = client.get();
assertEquals("application/x-javascript", r.getMetadata().getFirst("Content-Type"));
assertEquals("callback({\"Book\":{\"id\":123,\"name\":\"CXF in Action\"}});",
IOUtils.readStringFromStream((InputStream)r.getEntity()));
}
@Test
public void testGetBookJsonpJackson() throws Exception {
String url = "http://localhost:" + PORT + "/bus/jsonp2/books/123";
WebClient client = WebClient.create(url);
WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(10000000);
client.accept("application/json, application/x-javascript");
client.query("_jsonp", "callback");
Response r = client.get();
assertEquals("application/x-javascript", r.getMetadata().getFirst("Content-Type"));
String response = IOUtils.readStringFromStream((InputStream)r.getEntity());
assertTrue(response.startsWith("callback({\"class\":\"org.apache.cxf.systest.jaxrs.Book\","));
assertTrue(response.endsWith("});"));
assertTrue(response.contains("\"id\":123"));
assertTrue(response.contains("\"name\":\"CXF in Action\""));
}
@Test
public void testGetBookWithoutJsonpCallback() throws Exception {
String url = "http://localhost:" + PORT + "/the/jsonp/books/123";
WebClient client = WebClient.create(url);
client.accept("application/json, application/x-javascript");
WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(1000000L);
Response r = client.get();
assertEquals("application/json", r.getMetadata().getFirst("Content-Type"));
assertEquals("{\"Book\":{\"id\":123,\"name\":\"CXF in Action\"}}",
IOUtils.readStringFromStream((InputStream)r.getEntity()));
}
@Test
public void testGetBookAsArray() throws Exception {
URL url = new URL("http://localhost:" + PORT + "/the/bookstore/books/list/123");
URLConnection connect = url.openConnection();
connect.addRequestProperty("Accept", "application/json");
InputStream in = connect.getInputStream();
assertEquals("{\"Books\":{\"books\":[{\"id\":123,\"name\":\"CXF in Action\"}]}}",
getStringFromInputStream(in));
}
@Test
public void testGetBookXsiType() throws Exception {
String address = "http://localhost:" + PORT + "/the/thebooksxsi/bookstore/books/xsitype";
WebClient wc = WebClient.create(address);
WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000);
wc.accept("application/xml");
Book book = wc.get(Book.class);
assertEquals("SuperBook", book.getName());
}
@Test
public void testPostBookXsiType() throws Exception {
String address = "http://localhost:" + PORT + "/the/thebooksxsi/bookstore/books/xsitype";
JAXBElementProvider<Book> provider = new JAXBElementProvider<>();
provider.setExtraClass(new Class[]{SuperBook.class});
provider.setJaxbElementClassNames(Collections.singletonList(Book.class.getName()));
WebClient wc = WebClient.create(address, Collections.singletonList(provider));
wc.accept("application/xml");
wc.type("application/xml");
SuperBook book = new SuperBook("SuperBook2", 999L, true);
Book book2 = wc.invoke("POST", book, Book.class, Book.class);
assertEquals("SuperBook2", book2.getName());
}
@Test
public void testPostBookXsiTypeProxy() throws Exception {
String address = "http://localhost:" + PORT + "/the/thebooksxsi/bookstore";
JAXBElementProvider<Book> provider = new JAXBElementProvider<>();
provider.setExtraClass(new Class[]{SuperBook.class});
provider.setJaxbElementClassNames(Collections.singletonList(Book.class.getName()));
BookStoreSpring bookStore = JAXRSClientFactory.create(address, BookStoreSpring.class,
Collections.singletonList(provider));
SuperBook book = new SuperBook("SuperBook2", 999L, true);
Book book2 = bookStore.postGetBookXsiType(book);
assertEquals("SuperBook2", book2.getName());
}
@Test
public void testGetBookWithEncodedQueryValue() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/bookstore/booksquery?id=12%2B3";
getBook(endpointAddress, "resources/expected_get_book123json.txt");
}
@Test
public void testGetBookWithEncodedPathValue() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/bookstore/id=12%2B3";
getBook(endpointAddress, "resources/expected_get_book123json.txt");
}
@Test
public void testGetBookWithEncodedPathValue2() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/bookstore/id=12+3";
getBook(endpointAddress, "resources/expected_get_book123json.txt");
}
@Test
public void testGetDefaultBook() throws Exception {
String endpointAddress = "http://localhost:" + PORT + "/the/bookstore";
WebClient wc = WebClient.create(endpointAddress);
wc.accept("application/json");
Book book = wc.get(Book.class);
assertEquals(123L, book.getId());
}
@Test
public void testGetDefaultBook2() throws Exception {
String endpointAddress = "http://localhost:" + PORT + "/the/bookstore/2/";
WebClient wc = WebClient.create(endpointAddress);
wc.accept("application/json");
Book book = wc.get(Book.class);
assertEquals(123L, book.getId());
assertEquals("Default", book.getName());
}
@Test
public void testGetDefaultBookMatrixParam() throws Exception {
String endpointAddress = "http://localhost:" + PORT + "/the/bookstore/2/";
WebClient wc = WebClient.create(endpointAddress);
wc.matrix("a", "b");
wc.accept("application/json");
WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
Book book = wc.get(Book.class);
assertEquals(123L, book.getId());
assertEquals("Defaultb", book.getName());
}
@Test
public void testGetBookById() throws Exception {
String endpointAddress = "http://localhost:" + PORT + "/the/bookstore/2/123";
WebClient wc = WebClient.create(endpointAddress);
wc.accept("application/json");
Book book = wc.get(Book.class);
assertEquals(123L, book.getId());
assertEquals("Id", book.getName());
}
@Test
public void testGetDefaultBookJSessionID() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/bookstore/;JSESSIONID=123";
getBook(endpointAddress, "resources/expected_get_book123json.txt");
}
private void getBook(String endpointAddress, String resource) throws Exception {
getBook(endpointAddress, resource, "application/json");
}
private void getBook(String endpointAddress, String resource, String type) throws Exception {
getBook(endpointAddress, resource, type, null);
}
private void getBook(String endpointAddress, String resource, String type, String mHeader)
throws Exception {
URL url = new URL(endpointAddress);
URLConnection connect = url.openConnection();
connect.addRequestProperty("Content-Type", "*/*");
connect.addRequestProperty("Accept", type);
connect.addRequestProperty("Accept-Encoding", "gzip;q=1.0, identity; q=0.5, *;q=0");
if (mHeader != null) {
connect.addRequestProperty("X-HTTP-Method-Override", mHeader);
}
InputStream in = connect.getInputStream();
InputStream expected = getClass().getResourceAsStream(resource);
assertEquals(stripXmlInstructionIfNeeded(getStringFromInputStream(expected)),
stripXmlInstructionIfNeeded(getStringFromInputStream(in)));
}
private void getBookAegis(String endpointAddress, String type) throws Exception {
WebClient client = WebClient.create(endpointAddress,
Collections.singletonList(new AegisElementProvider<Object>()));
Book book = client.accept(type).get(Book.class);
assertEquals(124L, book.getId());
assertEquals("CXF in Action - 2", book.getName());
}
@Test
public void testAddInvalidXmlBook() throws Exception {
doPost("http://localhost:" + PORT + "/the/bookstore/books/convert",
400,
"application/xml",
"resources/add_book.txt",
null);
doPost("http://localhost:" + PORT + "/the/thebooks/bookstore/books/convert",
400,
"application/xml",
"resources/add_book.txt",
null);
}
@Test
public void testAddInvalidJsonBook() throws Exception {
doPost("http://localhost:" + PORT + "/the/bookstore/books/convert",
400,
"application/json",
"resources/add_book2json_invalid.txt",
null);
doPost("http://localhost:" + PORT + "/the/thebooks/bookstore/books/convert",
400,
"application/json",
"resources/add_book2json_invalid.txt",
null);
}
@Test
public void testAddValidXmlBook() throws Exception {
doPost("http://localhost:" + PORT + "/the/bookstore/books/convert",
200,
"application/xml",
"resources/add_book2.txt",
"resources/expected_get_book123.txt");
doPost("http://localhost:" + PORT + "/the/thebooks/bookstore/books/convert",
200,
"application/xml",
"resources/add_book2.txt",
"resources/expected_get_book123.txt");
}
@Test
public void testGetBookAegis() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks4/bookstore/books/aegis";
getBookAegis(endpointAddress, "application/xml");
}
@Test
public void testRetrieveGetBookAegis() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks4/bookstore/books/aegis/retrieve/get";
getBookAegis(endpointAddress, "application/xml");
}
@Test
public void testRetrieveBookAegis3() throws Exception {
try (Socket s = new Socket("localhost", Integer.parseInt(PORT));
InputStream is = this.getClass().getResourceAsStream("resources/retrieveRequest.txt")) {
byte[] bytes = IOUtils.readBytesFromStream(is);
s.getOutputStream().write(bytes);
s.getOutputStream().flush();
BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));
StringBuilder sb = new StringBuilder();
String str = null;
while ((str = r.readLine()) != null) {
sb.append(str);
}
String aegisData = sb.toString();
s.getInputStream().close();
s.close();
assertTrue(aegisData.contains("CXF in Action - 2"));
}
}
@Test
public void testGetBookUserResource() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks6/bookstore/books/123";
getBook(endpointAddress, "resources/expected_get_book123.txt", "application/xml");
}
@Test
public void testGetBookUserResource2() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks7/bookstore/books/123";
getBook(endpointAddress, "resources/expected_get_book123.txt", "application/xml");
}
@Test
public void testGetBookUserResourceFromProxy() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks6";
BookStoreNoAnnotations bStore = JAXRSClientFactory.createFromModel(
endpointAddress,
BookStoreNoAnnotations.class,
"classpath:/org/apache/cxf/systest/jaxrs/resources/resources.xml",
Collections.singletonList(new LongTypeParamConverterProvider()),
null);
Book b = bStore.getBook(null);
assertNotNull(b);
assertEquals(123L, b.getId());
assertEquals("CXF in Action", b.getName());
ChapterNoAnnotations proxy = bStore.getBookChapter(123L);
ChapterNoAnnotations c = proxy.getItself();
assertNotNull(c);
assertEquals(1, c.getId());
assertEquals("chapter 1", c.getTitle());
}
@Test
public void testGetBookXSLTXml() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks5/bookstore/books/xslt";
WebClient wc = WebClient.create(endpointAddress);
wc.accept("application/xml").path(666).matrix("name2", 2).query("name", "Action - ");
Book b = wc.get(Book.class);
assertEquals(666, b.getId());
assertEquals("CXF in Action - 2", b.getName());
}
@Test
public void testReaderWriterFromJaxrsFilters() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooksWithStax/bookstore/books/convert2/123";
WebClient wc = WebClient.create(endpointAddress);
wc.type("application/xml").accept("application/xml");
Book2 b = new Book2();
b.setId(777L);
b.setName("CXF - 777");
Book2 b2 = wc.invoke("PUT", b, Book2.class);
assertNotSame(b, b2);
assertEquals(777, b2.getId());
assertEquals("CXF - 777", b2.getName());
}
@Test
public void testReaderWriterFromInterceptors() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooksWithStax/bookstore/books/convert";
WebClient wc = WebClient.create(endpointAddress);
wc.type("application/xml").accept("application/xml");
Book2 b = new Book2();
b.setId(777L);
b.setName("CXF - 777");
Book2 b2 = wc.invoke("POST", b, Book2.class);
assertNotSame(b, b2);
assertEquals(777, b2.getId());
assertEquals("CXF - 777", b2.getName());
}
@Test
public void testAddValidBookJson() throws Exception {
doPost("http://localhost:" + PORT + "/the/bookstore/books/convert",
200,
"application/json",
"resources/add_book2json.txt",
"resources/expected_get_book123.txt");
doPost("http://localhost:" + PORT + "/the/thebooks/bookstore/books/convert",
200,
"application/json",
"resources/add_book2json.txt",
"resources/expected_get_book123.txt");
doPost("http://localhost:" + PORT + "/the/thebooks/bookstore/books/convert",
200,
"application/vnd.example-com.foo+json",
"resources/add_book2json.txt",
"resources/expected_get_book123.txt");
}
@Test
public void testAddInvalidBookDuplicateElementJson() throws Exception {
WebClient wc = WebClient.create("http://localhost:" + PORT + "/the/bookstore/books/convert");
wc.type("application/json");
InputStream is = getClass().getResourceAsStream("resources/add_book2json_duplicate.txt");
assertNotNull(is);
Response r = wc.post(is);
assertEquals(400, r.getStatus());
String content = IOUtils.readStringFromStream((InputStream)r.getEntity());
assertTrue(content, content.contains("Invalid content was found starting with element"));
}
private void doPost(String endpointAddress, int expectedStatus, String contentType,
String inResource, String expectedResource) throws Exception {
File input = new File(getClass().getResource(inResource).toURI());
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(endpointAddress);
post.setHeader("Content-Type", contentType);
post.setEntity(new FileEntity(input, ContentType.TEXT_XML));