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 SecuritySchemeInfo}.
28   */
29  class SecuritySchemeInfo_Test extends TestBase {
30  
31  	@Nested class A_basicTests extends TestBase {
32  
33  		private static final BeanTester<SecuritySchemeInfo> TESTER =
34  			testBean(
35  				bean()
36  					.setBearerFormat("a")
37  					.setDescription("b")
38  					.setFlows(oAuthFlow().setAuthorizationUrl("c"))
39  					.setIn("d")
40  					.setName("e")
41  					.setScheme("f")
42  					.setType("g")
43  			)
44  			.props("bearerFormat,description,flows{authorizationUrl},in,name,scheme,type")
45  			.vals("a,b,{c},d,e,f,g")
46  			.json("{bearerFormat:'a',description:'b',flows:{authorizationUrl:'c'},'in':'d',name:'e',scheme:'f',type:'g'}")
47  			.string("{'bearerFormat':'a','description':'b','flows':{'authorizationUrl':'c'},'in':'d','name':'e','scheme':'f','type':'g'}".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(), "bearerFormat", "description", "flows", "in", "name", "scheme", "type");
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_asMap() {
85  			assertBean(
86  				bean()
87  					.setType("a")
88  					.set("x1", "x1a")
89  					.asMap(),
90  				"type,x1",
91  				"a,x1a"
92  			);
93  		}
94  
95  		@Test void a10_extraKeys() {
96  			var x = bean().set("x1", "x1a").set("x2", "x2a");
97  			assertList(x.extraKeys(), "x1", "x2");
98  			assertEmpty(bean().extraKeys());
99  		}
100 
101 		@Test void a11_strictMode() {
102 			assertThrows(RuntimeException.class, () -> bean().strict().set("foo", "bar"));
103 			assertDoesNotThrow(() -> bean().set("foo", "bar"));
104 
105 			assertFalse(bean().isStrict());
106 			assertTrue(bean().strict().isStrict());
107 			assertFalse(bean().strict(false).isStrict());
108 
109 			var x = bean().strict();
110 			var y = bean(); // not strict
111 
112 			assertThrowsWithMessage(RuntimeException.class, "Invalid value passed in to setIn(String).  Value='invalid', valid values=[query, header, cookie]", () -> x.setIn("invalid"));
113 			assertDoesNotThrow(() -> x.setIn("query"));
114 			assertDoesNotThrow(() -> x.setIn("header"));
115 			assertDoesNotThrow(() -> x.setIn("cookie"));
116 			assertDoesNotThrow(() -> y.setIn("invalid"));
117 
118 			assertThrowsWithMessage(RuntimeException.class, "Invalid value passed in to setType(String).  Value='invalid', valid values=[apiKey, http, oauth2, openIdConnect]", () -> x.setType("invalid"));
119 			assertDoesNotThrow(() -> x.setType("apiKey"));
120 			assertDoesNotThrow(() -> x.setType("http"));
121 			assertDoesNotThrow(() -> x.setType("oauth2"));
122 			assertDoesNotThrow(() -> x.setType("openIdConnect"));
123 			assertDoesNotThrow(() -> y.setType("invalid"));
124 		}
125 	}
126 
127 	@Nested class B_emptyTests extends TestBase {
128 
129 		private static final BeanTester<SecuritySchemeInfo> TESTER =
130 			testBean(bean())
131 			.props("type,description,name,in,scheme,bearerFormat,flows")
132 			.vals("<null>,<null>,<null>,<null>,<null>,<null>,<null>")
133 			.json("{}")
134 			.string("{}")
135 		;
136 
137 		@Test void b01_gettersAndSetters() {
138 			TESTER.assertGettersAndSetters();
139 		}
140 
141 		@Test void b02_copy() {
142 			TESTER.assertCopy();
143 		}
144 
145 		@Test void b03_toJson() {
146 			TESTER.assertToJson();
147 		}
148 
149 		@Test void b04_fromJson() {
150 			TESTER.assertFromJson();
151 		}
152 
153 		@Test void b05_roundTrip() {
154 			TESTER.assertRoundTrip();
155 		}
156 
157 		@Test void b06_toString() {
158 			TESTER.assertToString();
159 		}
160 
161 		@Test void b07_keySet() {
162 			assertEmpty(TESTER.bean().keySet());
163 		}
164 	}
165 
166 	@Nested class C_extraProperties extends TestBase {
167 		private static final BeanTester<SecuritySchemeInfo> TESTER =
168 			testBean(
169 				bean()
170 					.set("bearerFormat", "a")
171 					.set("description", "b")
172 					.set("flows", oAuthFlows().setImplicit(oAuthFlow().setAuthorizationUrl("c")))
173 					.set("in", "d")
174 					.set("name", "e")
175 					.set("openIdConnectUrl", "f")
176 					.set("scheme", "g")
177 					.set("type", "h")
178 					.set("x1", "x1a")
179 					.set("x2", null)
180 			)
181 			.props("bearerFormat,description,flows{implicit{authorizationUrl}},in,name,openIdConnectUrl,scheme,type,x1,x2")
182 			.vals("a,b,{{c}},d,e,f,g,h,x1a,<null>")
183 			.json("{bearerFormat:'a',description:'b',flows:{implicit:{authorizationUrl:'c'}},'in':'d',name:'e',openIdConnectUrl:'f',scheme:'g',type:'h',x1:'x1a'}")
184 			.string("{'bearerFormat':'a','description':'b','flows':{'implicit':{'authorizationUrl':'c'}},'in':'d','name':'e','openIdConnectUrl':'f','scheme':'g','type':'h','x1':'x1a'}".replace('\'', '"'))
185 		;
186 
187 		@Test void c01_gettersAndSetters() {
188 			TESTER.assertGettersAndSetters();
189 		}
190 
191 		@Test void c02_copy() {
192 			TESTER.assertCopy();
193 		}
194 
195 		@Test void c03_toJson() {
196 			TESTER.assertToJson();
197 		}
198 
199 		@Test void c04_fromJson() {
200 			TESTER.assertFromJson();
201 		}
202 
203 		@Test void c05_roundTrip() {
204 			TESTER.assertRoundTrip();
205 		}
206 
207 		@Test void c06_toString() {
208 			TESTER.assertToString();
209 		}
210 
211 		@Test void c07_keySet() {
212 			assertList(TESTER.bean().keySet(), "bearerFormat", "description", "flows", "in", "name", "openIdConnectUrl", "scheme", "type", "x1", "x2");
213 		}
214 
215 		@Test void c08_get() {
216 			assertMapped(
217 				TESTER.bean(), (obj,prop) -> obj.get(prop, Object.class),
218 				"bearerFormat,description,flows{implicit{authorizationUrl}},in,name,openIdConnectUrl,scheme,type,x1,x2",
219 				"a,b,{{c}},d,e,f,g,h,x1a,<null>"
220 			);
221 		}
222 
223 		@Test void c09_getTypes() {
224 			assertMapped(
225 				TESTER.bean(), (obj,prop) -> simpleClassNameOf(obj.get(prop, Object.class)),
226 				"bearerFormat,description,flows,in,name,openIdConnectUrl,scheme,type,x1,x2",
227 				"String,String,OAuthFlow,String,String,String,String,String,String,<null>"
228 			);
229 		}
230 
231 		@Test void c10_nullPropertyValue() {
232 			assertThrows(IllegalArgumentException.class, ()->bean().get(null));
233 			assertThrows(IllegalArgumentException.class, ()->bean().get(null, String.class));
234 			assertThrows(IllegalArgumentException.class, ()->bean().set(null, "a"));
235 		}
236 	}
237 
238 	//---------------------------------------------------------------------------------------------
239 	// Helper methods
240 	//---------------------------------------------------------------------------------------------
241 
242 	private static SecuritySchemeInfo bean() {
243 		return securitySchemeInfo();
244 	}
245 }