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