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