1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.rest.client;
18
19 import static org.apache.juneau.TestUtils.*;
20 import static org.apache.juneau.commons.utils.CollectionUtils.*;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.apache.juneau.*;
26 import org.apache.juneau.annotation.*;
27 import org.apache.juneau.collections.*;
28 import org.apache.juneau.rest.annotation.*;
29 import org.apache.juneau.rest.mock.*;
30 import org.apache.juneau.rest.servlet.*;
31 import org.junit.jupiter.api.*;
32
33 class RestClient_Config_Serializer_Test extends TestBase {
34
35 public static class ABean {
36 public int f;
37 static ABean get() {
38 var x = new ABean();
39 x.f = 1;
40 return x;
41 }
42 }
43
44 private static ABean bean = ABean.get();
45
46 @Rest
47 public static class A extends BasicRestObject {
48 @RestPost
49 public Reader echoBody(org.apache.juneau.rest.RestRequest req) throws IOException {
50 return req.getContent().getReader();
51 }
52 }
53
54
55
56
57
58 public static class A1 {
59 public Object f1;
60 static A1 get() {
61 var x = new A1();
62 x.f1 = A2.get();
63 return x;
64 }
65 }
66
67 @Test void a01_addBeanTypes() throws Exception {
68 var l1 = A1.get();
69 client().addBeanTypes().build().post("/echoBody",l1).run().assertContent("{f1:{_type:'L',f2:1}}");
70 }
71
72 @org.apache.juneau.annotation.Bean(typeName="L")
73 public static class A2 {
74 public int f2;
75 static A2 get() {
76 var x = new A2();
77 x.f2 = 1;
78 return x;
79 }
80 }
81
82 @Test void a02_addRootType() throws Exception {
83 var l2 = A2.get();
84 client().addBeanTypes().addRootType().build().post("/echoBody",l2).run().assertContent("{_type:'L',f2:1}");
85 }
86
87 @Test void a03_detectRecursions() {
88 var l1 = new A1();
89 l1.f1 = l1;
90 assertThrowsWithMessage(Exception.class, "Recursion occurred", ()->client().detectRecursions().build().post("/echoBody",l1).run());
91 }
92
93 @Test void a04_ignoreRecursions() throws Exception {
94 var l1 = new A1();
95 l1.f1 = l1;
96 client().ignoreRecursions().build().post("/echoBody",l1).run().assertContent("{}");
97 }
98
99 @Test void a05_initialDepth() throws Exception {
100 client().initialDepth(2).ws().build().post("/echoBody",bean).run().assertContent("\t\t{\n\t\t\tf: 1\n\t\t}");
101 }
102
103 public static class A6 {
104 public ABean f;
105 static A6 get() {
106 var x = new A6();
107 x.f = bean;
108 return x;
109 }
110 }
111
112 @Test void a06_maxDepth() throws Exception {
113 client().maxDepth(1).build().post("/echoBody",A6.get()).run().assertContent("{}");
114 }
115
116 @Test void a07_sortCollections() throws Exception {
117 var x = a("c","a","b");
118 client().sortCollections().build().post("/echoBody",x).run().assertContent("['a','b','c']");
119 }
120
121 @Test void a08_sortMapsBoolean() throws Exception {
122 var x = m("c",3,"a",1,"b",2);
123 client().sortMaps().build().post("/echoBody",x).run().assertContent("{a:1,b:2,c:3}");
124 }
125
126 public static class A9 {
127 public List<String> f1 = list();
128 public String[] f2 = {};
129 }
130
131 @Test void a09_trimEmptyCollections() throws Exception {
132 var x = new A9();
133 client().trimEmptyCollections().build().post("/echoBody",x).run().assertContent("{}");
134 }
135
136 public static class A10 {
137 public Map<String,String> f1 = map();
138 public JsonMap f2 = JsonMap.create();
139 }
140
141 @Test void a10_trimEmptyMaps() throws Exception {
142 var x = new A10();
143 client().trimEmptyMaps().build().post("/echoBody",x).run().assertContent("{}");
144 }
145
146 public static class A11 {
147 public String f;
148 }
149
150 @Test void a11_trimNullPropertiesBoolean() throws Exception {
151 var x = new A11();
152 client().keepNullProperties().build().post("/echoBody",x).run().assertContent("{f:null}");
153 }
154
155 public static class A12 {
156 public String f = " foo ";
157 }
158
159 @Test void a12_trimStringsOnWrite() throws Exception {
160 var x = new A12();
161 client().trimStringsOnWrite().build().post("/echoBody",x).run().assertContent("{f:'foo'}");
162 }
163
164 public static class A13 {
165 @Uri public String f = "foo";
166 }
167
168 @Test void a13_uriContext_uriResolution_uriRelativity() throws Exception {
169 var x = new A13();
170 client().uriResolution(UriResolution.ABSOLUTE).uriRelativity(UriRelativity.PATH_INFO).uriContext(UriContext.of("http://localhost:80","/context","/resource","/path")).build().post("/echoBody",x).run().assertContent("{f:'http://localhost:80/context/resource/foo'}");
171 client().uriResolution(UriResolution.NONE).uriRelativity(UriRelativity.RESOURCE).uriContext(UriContext.of("http://localhost:80","/context","/resource","/path")).build().post("/echoBody",x).run().assertContent("{f:'foo'}");
172 }
173
174 public static class A14 {
175 public int f1;
176 public A14 f2;
177
178 static A14 get() {
179 var x = new A14();
180 var x2 = new A14();
181 var x3 = new A14();
182 x.f1 = 1;
183 x2.f1 = 2;
184 x3.f1 = 3;
185 x.f2 = x2;
186 x2.f2 = x3;
187 return x;
188 }
189 }
190
191 @Test void a14_maxIndent() throws Exception {
192 var x = A14.get();
193 client().maxIndent(2).ws().build().post("/echoBody",x).run().assertContent("{\n\tf1: 1,\n\tf2: {\n\t\tf1: 2,\n\t\tf2: {f1:3}\n\t}\n}");
194 }
195
196 public static class A15 {
197 public String f1 = "foo";
198 }
199
200 @Test void a15_quoteChar() throws Exception {
201 var x = new A15();
202 MockRestClient.create(A.class).json().quoteChar('\'').build().post("/echoBody",x).run().assertContent("{'f1':'foo'}");
203 MockRestClient.create(A.class).json().quoteChar('|').build().post("/echoBody",x).run().assertContent("{|f1|:|foo|}");
204 }
205
206 @Test void a16_sq() throws Exception {
207 var x = new A15();
208 MockRestClient.create(A.class).json().sq().build().post("/echoBody",x).run().assertContent("{'f1':'foo'}");
209 client().sq().build().post("/echoBody",x).run().assertContent("{f1:'foo'}");
210 }
211
212 @Test void a17_useWhitespace() throws Exception {
213 var x = new A15();
214 client().ws().build().post("/echoBody",x).run().assertContent("{\n\tf1: 'foo'\n}");
215 client().useWhitespace().build().post("/echoBody",x).run().assertContent("{\n\tf1: 'foo'\n}");
216 }
217
218
219
220
221
222 private static RestClient.Builder client() {
223 return MockRestClient.create(A.class).json5();
224 }
225 }