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 Parameter}.
30   */
31  class Parameter_Test extends TestBase {
32  
33  	@Nested class A_basicTests extends TestBase {
34  
35  		private static final BeanTester<Parameter> TESTER =
36  			testBean(
37  				bean()
38  					.setAllowEmptyValue(true)
39  					.setAllowReserved(true)
40  					.setDeprecated(true)
41  					.setDescription("a")
42  					.setExample("b")
43  					.setExamples(map("c1", example().setSummary("c2")))
44  					.setExplode(true)
45  					.setIn("d")
46  					.setName("e")
47  					.setRequired(true)
48  					.setSchema(schemaInfo().setType("f"))
49  					.setStyle("g")
50  			)
51  			.props("allowEmptyValue,allowReserved,deprecated,description,example,examples{c1{summary}},explode,in,name,required,schema{type},style")
52  			.vals("true,true,true,a,b,{{c2}},true,d,e,true,{f},g")
53  			.json("{allowEmptyValue:true,allowReserved:true,deprecated:true,description:'a',example:'b',examples:{c1:{summary:'c2'}},explode:true,'in':'d',name:'e',required:true,schema:{type:'f'},style:'g'}")
54  			.string("{'allowEmptyValue':true,'allowReserved':true,'deprecated':true,'description':'a','example':'b','examples':{'c1':{'summary':'c2'}},'explode':true,'in':'d','name':'e','required':true,'schema':{'type':'f'},'style':'g'}".replace('\'','"'))
55  		;
56  
57  		@Test void a01_gettersAndSetters() {
58  			TESTER.assertGettersAndSetters();
59  		}
60  
61  		@Test void a02_copy() {
62  			var t = new Parameter(TESTER.bean());
63  			assertNotSame(TESTER.bean(), t);
64  			assertBean(t, TESTER.props(), TESTER.vals());
65  		}
66  
67  		@Test void a03_toJson() {
68  			TESTER.assertToJson();
69  		}
70  
71  		@Test void a04_fromJson() {
72  			TESTER.assertFromJson();
73  		}
74  
75  		@Test void a05_roundTrip() {
76  			TESTER.assertRoundTrip();
77  		}
78  
79  		@Test void a06_toString() {
80  			TESTER.assertToString();
81  		}
82  
83  		@Test void a07_keySet() {
84  			assertList(TESTER.bean().keySet(), "allowEmptyValue", "allowReserved", "deprecated", "description", "example", "examples", "explode", "in", "name", "required", "schema", "style");
85  		}
86  
87  		@Test void a08_nullParameters() {
88  			var x = bean();
89  			assertThrows(IllegalArgumentException.class, () -> x.get(null, String.class));
90  			assertThrows(IllegalArgumentException.class, () -> x.set(null, "value"));
91  		}
92  
93  		@Test void a09_asMap() {
94  			assertBean(
95  				bean()
96  					.setName("a")
97  					.set("x1", "x1a")
98  					.asMap(),
99  				"name,x1",
100 				"a,x1a"
101 			);
102 		}
103 
104 		@Test void a10_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 a11_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 			var x = bean().strict();
119 			var y = bean(); // not strict
120 			assertThrowsWithMessage(RuntimeException.class, "Invalid value passed in to setIn(String).  Value='invalid', valid values=['query','header','path','cookie']", () -> x.setIn("invalid"));
121 			assertDoesNotThrow(() -> x.setIn("query"));
122 			assertDoesNotThrow(() -> x.setIn("header"));
123 			assertDoesNotThrow(() -> x.setIn("path"));
124 			assertDoesNotThrow(() -> x.setIn("cookie"));
125 			assertDoesNotThrow(() -> y.setIn("invalid"));
126 
127 			assertThrowsWithMessage(RuntimeException.class, "Invalid value passed in to setStyle(String).  Value='invalid', valid values=['matrix','label','form','simple','spaceDelimited','pipeDelimited','deepObject']", () -> x.setStyle("invalid"));
128 			assertDoesNotThrow(() -> x.setStyle("matrix"));
129 			assertDoesNotThrow(() -> x.setStyle("label"));
130 			assertDoesNotThrow(() -> x.setStyle("form"));
131 			assertDoesNotThrow(() -> x.setStyle("simple"));
132 			assertDoesNotThrow(() -> x.setStyle("spaceDelimited"));
133 			assertDoesNotThrow(() -> x.setStyle("pipeDelimited"));
134 			assertDoesNotThrow(() -> x.setStyle("deepObject"));
135 			assertDoesNotThrow(() -> y.setStyle("invalid"));
136 		}
137 	}
138 
139 	@Nested class B_emptyTests extends TestBase {
140 
141 		private static final BeanTester<Parameter> TESTER =
142 			testBean(bean())
143 			.props("name,in,description,required,deprecated,allowEmptyValue,style,explode,allowReserved,schema,example,examples")
144 			.vals("<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>")
145 			.json("{}")
146 			.string("{}")
147 		;
148 
149 		@Test void b01_gettersAndSetters() {
150 			TESTER.assertGettersAndSetters();
151 		}
152 
153 		@Test void b02_copy() {
154 			var t = new Parameter(TESTER.bean());
155 			assertNotSame(TESTER.bean(), t);
156 			assertBean(t, TESTER.props(), TESTER.vals());
157 		}
158 
159 		@Test void b03_toJson() {
160 			TESTER.assertToJson();
161 		}
162 
163 		@Test void b04_fromJson() {
164 			TESTER.assertFromJson();
165 		}
166 
167 		@Test void b05_roundTrip() {
168 			TESTER.assertRoundTrip();
169 		}
170 
171 		@Test void b06_toString() {
172 			TESTER.assertToString();
173 		}
174 
175 		@Test void b07_keySet() {
176 			assertEmpty(TESTER.bean().keySet());
177 		}
178 	}
179 
180 	@Nested class C_extraProperties extends TestBase {
181 		private static final BeanTester<Parameter> TESTER =
182 			testBean(
183 				bean()
184 					.set("allowEmptyValue", true)
185 					.set("allowReserved", true)
186 					.set("content", map("a1", mediaType().setSchema(schemaInfo("a2"))))
187 					.set("deprecated", true)
188 					.set("description", "b")
189 					.set("example", "c")
190 					.set("examples", map("d1", example().setSummary("d2")))
191 					.set("explode", true)
192 					.set("in", "e")
193 					.set("name", "f")
194 					.set("required", true)
195 					.set("schema", schemaInfo("g"))
196 					.set("style", "h")
197 					.set("x1", "x1a")
198 					.set("x2", null)
199 			)
200 			.props("allowEmptyValue,allowReserved,content{a1{schema{type}}},deprecated,description,example,examples{d1{summary}},explode,in,name,required,schema{type},style,x1,x2")
201 			.vals("true,true,{{{a2}}},true,b,c,{{d2}},true,e,f,true,{g},h,x1a,<null>")
202 			.json("{allowEmptyValue:true,allowReserved:true,content:{a1:{schema:{type:'a2'}}},deprecated:true,description:'b',example:'c',examples:{d1:{summary:'d2'}},explode:true,'in':'e',name:'f',required:true,schema:{type:'g'},style:'h',x1:'x1a'}")
203 			.string("{'allowEmptyValue':true,'allowReserved':true,'content':{'a1':{'schema':{'type':'a2'}}},'deprecated':true,'description':'b','example':'c','examples':{'d1':{'summary':'d2'}},'explode':true,'in':'e','name':'f','required':true,'schema':{'type':'g'},'style':'h','x1':'x1a'}".replace('\'', '"'))
204 		;
205 
206 		@Test void c01_gettersAndSetters() {
207 			TESTER.assertGettersAndSetters();
208 		}
209 
210 		@Test void c02_copy() {
211 			TESTER.assertCopy();
212 		}
213 
214 		@Test void c03_toJson() {
215 			TESTER.assertToJson();
216 		}
217 
218 		@Test void c04_fromJson() {
219 			TESTER.assertFromJson();
220 		}
221 
222 		@Test void c05_roundTrip() {
223 			TESTER.assertRoundTrip();
224 		}
225 
226 		@Test void c06_toString() {
227 			TESTER.assertToString();
228 		}
229 
230 		@Test void c07_keySet() {
231 			assertList(TESTER.bean().keySet(), "allowEmptyValue", "allowReserved", "deprecated", "description", "example", "examples", "explode", "in", "name", "required", "schema", "style", "content", "x1", "x2");
232 		}
233 
234 		@Test void c08_get() {
235 			assertMapped(
236 				TESTER.bean(), (obj,prop) -> obj.get(prop, Object.class),
237 				"allowEmptyValue,allowReserved,content{a1{schema{type}}},deprecated,description,example,examples{d1{summary}},explode,in,name,required,schema{type},style,x1,x2",
238 				"true,true,{{{a2}}},true,b,c,{{d2}},true,e,f,true,{g},h,x1a,<null>"
239 			);
240 		}
241 
242 		@Test void c09_getTypes() {
243 			assertMapped(
244 				TESTER.bean(), (obj,prop) -> cns(obj.get(prop, Object.class)),
245 				"allowEmptyValue,allowReserved,content,deprecated,description,example,examples,explode,in,name,required,schema,style,x1,x2",
246 				"Boolean,Boolean,LinkedHashMap,Boolean,String,String,FilteredMap,Boolean,String,String,Boolean,SchemaInfo,String,String,<null>"
247 			);
248 		}
249 
250 		@Test void c10_nullPropertyValue() {
251 			assertThrows(IllegalArgumentException.class, ()->bean().get(null));
252 			assertThrows(IllegalArgumentException.class, ()->bean().get(null, String.class));
253 			assertThrows(IllegalArgumentException.class, ()->bean().set(null, "a"));
254 		}
255 	}
256 
257 	//---------------------------------------------------------------------------------------------
258 	// Helper methods
259 	//---------------------------------------------------------------------------------------------
260 
261 	private static Parameter bean() {
262 		return parameter();
263 	}
264 }