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