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.bean.openapi3;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.bean.openapi3.OpenApiBuilder.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import org.apache.juneau.*;
24  import org.junit.jupiter.api.*;
25  
26  /**
27   * Testcase for {@link Encoding}.
28   */
29  class Encoding_Test extends TestBase {
30  
31  	@Nested class A_basicTests extends TestBase {
32  
33  		private static final BeanTester<Encoding> TESTER =
34  			testBean(
35  				bean()
36  					.setAllowReserved(true)
37  					.setContentType("a")
38  					.setExplode(true)
39  					.setHeaders(map("b1", headerInfo(schemaInfo().setType("b2"))))
40  					.setStyle("c")
41  			)
42  			.props("allowReserved,contentType,explode,headers{b1{schema{type}}},style")
43  			.vals("true,a,true,{{{b2}}},c")
44  			.json("{allowReserved:true,contentType:'a',explode:true,headers:{b1:{schema:{type:'b2'}}},style:'c'}")
45  			.string("{'allowReserved':true,'contentType':'a','explode':true,'headers':{'b1':{'schema':{'type':'b2'}}},'style':'c'}".replace('\'','"'))
46  		;
47  
48  		@Test void a01_gettersAndSetters() {
49  			TESTER.assertGettersAndSetters();
50  		}
51  
52  		@Test void a02_copy() {
53  			TESTER.assertCopy();
54  		}
55  
56  		@Test void a03_toJson() {
57  			TESTER.assertToJson();
58  		}
59  
60  		@Test void a04_fromJson() {
61  			TESTER.assertFromJson();
62  		}
63  
64  		@Test void a05_roundTrip() {
65  			TESTER.assertRoundTrip();
66  		}
67  
68  		@Test void a06_toString() {
69  			TESTER.assertToString();
70  		}
71  
72  		@Test void a07_keySet() {
73  			assertList(TESTER.bean().keySet(), "allowReserved", "contentType", "explode", "headers", "style");
74  		}
75  
76  		@Test void a08_nullParameters() {
77  			var x = bean();
78  			assertThrows(IllegalArgumentException.class, () -> x.get(null, String.class));
79  			assertThrows(IllegalArgumentException.class, () -> x.set(null, "value"));
80  		}
81  
82  		@Test void a09_addMethods() {
83  			assertBean(
84  				bean()
85  					.addHeader("a1", headerInfo(schemaInfo("a2"))),
86  				"headers{a1{schema{type}}}",
87  				"{{{a2}}}"
88  			);
89  		}
90  
91  		@Test void a10_asMap() {
92  			assertBean(
93  				bean()
94  					.setContentType("a")
95  					.set("x1", "x1a")
96  					.asMap(),
97  				"contentType,x1",
98  				"a,x1a"
99  			);
100 		}
101 
102 		@Test void a11_extraKeys() {
103 			var x = bean().set("x1", "x1a").set("x2", "x2a");
104 			assertList(x.extraKeys(), "x1", "x2");
105 			assertEmpty(bean().extraKeys());
106 		}
107 
108 		@Test void a12_strictMode() {
109 			assertThrows(RuntimeException.class, () -> bean().strict().set("foo", "bar"));
110 			assertDoesNotThrow(() -> bean().set("foo", "bar"));
111 
112 			assertFalse(bean().isStrict());
113 			assertTrue(bean().strict().isStrict());
114 			assertFalse(bean().strict(false).isStrict());
115 		}
116 	}
117 
118 	@Nested class B_emptyTests extends TestBase {
119 
120 		private static final BeanTester<Encoding> TESTER =
121 			testBean(bean())
122 			.props("contentType,style,explode,allowReserved,headers")
123 			.vals("<null>,<null>,<null>,<null>,<null>")
124 			.json("{}")
125 			.string("{}")
126 		;
127 
128 		@Test void b01_gettersAndSetters() {
129 			TESTER.assertGettersAndSetters();
130 		}
131 
132 		@Test void b02_copy() {
133 			TESTER.assertCopy();
134 		}
135 
136 		@Test void b03_toJson() {
137 			TESTER.assertToJson();
138 		}
139 
140 		@Test void b04_fromJson() {
141 			TESTER.assertFromJson();
142 		}
143 
144 		@Test void b05_roundTrip() {
145 			TESTER.assertRoundTrip();
146 		}
147 
148 		@Test void b06_toString() {
149 			TESTER.assertToString();
150 		}
151 
152 		@Test void b07_keySet() {
153 			assertEmpty(TESTER.bean().keySet());
154 		}
155 	}
156 
157 	@Nested class C_extraProperties extends TestBase {
158 		private static final BeanTester<Encoding> TESTER =
159 			testBean(
160 				bean()
161 					.set("allowReserved", true)
162 					.set("contentType", "a")
163 					.set("explode", true)
164 					.set("headers", map("b1", headerInfo(schemaInfo("b2"))))
165 					.set("style", "c")
166 					.set("x1", "x1a")
167 					.set("x2", null)
168 			)
169 			.props("allowReserved,contentType,explode,headers{b1{schema{type}}},style,x1,x2")
170 			.vals("true,a,true,{{{b2}}},c,x1a,<null>")
171 			.json("{allowReserved:true,contentType:'a',explode:true,headers:{b1:{schema:{type:'b2'}}},style:'c',x1:'x1a'}")
172 			.string("{'allowReserved':true,'contentType':'a','explode':true,'headers':{'b1':{'schema':{'type':'b2'}}},'style':'c','x1':'x1a'}".replace('\'', '"'))
173 		;
174 
175 		@Test void c01_gettersAndSetters() {
176 			TESTER.assertGettersAndSetters();
177 		}
178 
179 		@Test void c02_copy() {
180 			TESTER.assertCopy();
181 		}
182 
183 		@Test void c03_toJson() {
184 			TESTER.assertToJson();
185 		}
186 
187 		@Test void c04_fromJson() {
188 			TESTER.assertFromJson();
189 		}
190 
191 		@Test void c05_roundTrip() {
192 			TESTER.assertRoundTrip();
193 		}
194 
195 		@Test void c06_toString() {
196 			TESTER.assertToString();
197 		}
198 
199 		@Test void c07_keySet() {
200 			assertList(TESTER.bean().keySet(), "allowReserved", "contentType", "explode", "headers", "style", "x1", "x2");
201 		}
202 
203 		@Test void c08_get() {
204 			assertMapped(
205 				TESTER.bean(), (obj,prop) -> obj.get(prop, Object.class),
206 				"allowReserved,contentType,explode,headers{b1{schema{type}}},style,x1,x2",
207 				"true,a,true,{{{b2}}},c,x1a,<null>"
208 			);
209 		}
210 
211 		@Test void c09_getTypes() {
212 			assertMapped(
213 				TESTER.bean(), (obj,prop) -> simpleClassNameOf(obj.get(prop, Object.class)),
214 				"allowReserved,contentType,explode,headers,style,x1,x2",
215 				"Boolean,String,Boolean,LinkedHashMap,String,String,<null>"
216 			);
217 		}
218 
219 		@Test void c10_nullPropertyValue() {
220 			assertThrows(IllegalArgumentException.class, ()->bean().get(null));
221 			assertThrows(IllegalArgumentException.class, ()->bean().get(null, String.class));
222 			assertThrows(IllegalArgumentException.class, ()->bean().set(null, "a"));
223 		}
224 	}
225 
226 	//---------------------------------------------------------------------------------------------
227 	// Helper methods
228 	//---------------------------------------------------------------------------------------------
229 
230 	private static Encoding bean() {
231 		return encoding();
232 	}
233 }