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.swagger;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.bean.swagger.SwaggerBuilder.*;
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 SecurityScheme}.
28   */
29  class SecurityScheme_Test extends TestBase {
30  
31  	@Nested class A_basicTests extends TestBase {
32  
33  		private static final BeanTester<SecurityScheme> TESTER =
34  			testBean(
35  				bean()
36  					.setAuthorizationUrl("a")
37  					.setDescription("b")
38  					.setFlow("c")
39  					.setIn("d")
40  					.setName("e")
41  					.setScopes(map("x1", "x2"))
42  					.setTokenUrl("f")
43  					.setType("g")
44  			)
45  			.props("authorizationUrl,description,flow,in,name,scopes{x1},tokenUrl,type")
46  			.vals("a,b,c,d,e,{x2},f,g")
47  			.json("{authorizationUrl:'a',description:'b',flow:'c','in':'d',name:'e',scopes:{x1:'x2'},tokenUrl:'f',type:'g'}")
48  			.string("{'authorizationUrl':'a','description':'b','flow':'c','in':'d','name':'e','scopes':{'x1':'x2'},'tokenUrl':'f','type':'g'}".replace('\'', '"'))
49  		;
50  
51  		@Test void a01_gettersAndSetters() {
52  			TESTER.assertGettersAndSetters();
53  		}
54  
55  		@Test void a02_copy() {
56  			TESTER.assertCopy();
57  		}
58  
59  		@Test void a03_toJson() {
60  			TESTER.assertToJson();
61  		}
62  
63  		@Test void a04_fromJson() {
64  			TESTER.assertFromJson();
65  		}
66  
67  		@Test void a05_roundTrip() {
68  			TESTER.assertRoundTrip();
69  		}
70  
71  		@Test void a06_toString() {
72  			TESTER.assertToString();
73  		}
74  
75  		@Test void a07_keySet() {
76  			assertList(TESTER.bean().keySet(), "authorizationUrl", "description", "flow", "in", "name", "scopes", "tokenUrl", "type");
77  		}
78  
79  		@Test void a08_addMethods() {
80  			var x = bean()
81  				.setScopes(map("a1", "a2"))
82  				.addScope("b1", "b2");
83  
84  			assertBean(x, "scopes{a1,b1}", "{a2,b2}");
85  		}
86  
87  		@Test void a09_addMethodsWithNullParameters() {
88  			var x = bean();
89  			assertThrows(IllegalArgumentException.class, () -> x.addScope(null, "a"));
90  			assertThrows(IllegalArgumentException.class, () -> x.addScope("a", null));
91  		}
92  
93  		@Test void a10_asMap() {
94  			assertBean(
95  				bean()
96  					.setDescription("a")
97  					.setType("b")
98  					.set("x1", "x1a")
99  					.asMap(),
100 				"description,type,x1",
101 				"a,b,x1a"
102 			);
103 		}
104 
105 		@Test void a11_extraKeys() {
106 			var x = bean().set("x1", "x1a").set("x2", "x2a");
107 			assertList(x.extraKeys(), "x1", "x2");
108 			assertEmpty(bean().extraKeys());
109 		}
110 
111 		@Test void a12_strictMode() {
112 			assertThrows(RuntimeException.class, () -> bean().strict().set("foo", "bar"));
113 			assertDoesNotThrow(() -> bean().set("foo", "bar"));
114 
115 			assertFalse(bean().isStrict());
116 			assertTrue(bean().strict().isStrict());
117 			assertFalse(bean().strict(false).isStrict());
118 
119 			var x = bean().strict();
120 			var y = bean(); // not strict
121 
122 			assertThrowsWithMessage(RuntimeException.class, "Invalid value passed in to setType(String).  Value='invalid', valid values=['basic','apiKey','oauth2']", () -> x.setType("invalid"));
123 			assertDoesNotThrow(() -> x.setType("basic"));
124 			assertDoesNotThrow(() -> x.setType("apiKey"));
125 			assertDoesNotThrow(() -> x.setType("oauth2"));
126 			assertDoesNotThrow(() -> y.setType("invalid"));
127 		}
128 	}
129 
130 	@Nested class B_emptyTests extends TestBase {
131 
132 		private static final BeanTester<SecurityScheme> TESTER =
133 			testBean(bean())
134 			.props("description,type,name,in,flow,authorizationUrl,tokenUrl,scopes")
135 			.vals("<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>")
136 			.json("{}")
137 			.string("{}")
138 		;
139 
140 		@Test void b01_gettersAndSetters() {
141 			TESTER.assertGettersAndSetters();
142 		}
143 
144 		@Test void b02_copy() {
145 			TESTER.assertCopy();
146 		}
147 
148 		@Test void b03_toJson() {
149 			TESTER.assertToJson();
150 		}
151 
152 		@Test void b04_fromJson() {
153 			TESTER.assertFromJson();
154 		}
155 
156 		@Test void b05_roundTrip() {
157 			TESTER.assertRoundTrip();
158 		}
159 
160 		@Test void b06_toString() {
161 			TESTER.assertToString();
162 		}
163 
164 		@Test void b07_keySet() {
165 			assertEmpty(TESTER.bean().keySet());
166 		}
167 
168 		@Test void b08_nullParameters() {
169 			var x = bean();
170 			assertThrows(IllegalArgumentException.class, () -> x.get(null, String.class));
171 			assertThrows(IllegalArgumentException.class, () -> x.set(null, "value"));
172 		}
173 	}
174 
175 	@Nested class C_extraProperties extends TestBase {
176 
177 		private static final BeanTester<SecurityScheme> TESTER =
178 			testBean(
179 				bean()
180 					.set("authorizationUrl", "a")
181 					.set("description", "b")
182 					.set("flow", "c")
183 					.set("in", "d")
184 					.set("name", "e")
185 					.set("scopes", map("x1", "x2"))
186 					.set("tokenUrl", "f")
187 					.set("type", "g")
188 					.set("x3", "x3a")
189 					.set("x4", null)
190 			)
191 			.props("authorizationUrl,description,flow,in,name,scopes{x1},tokenUrl,type,x3,x4")
192 			.vals("a,b,c,d,e,{x2},f,g,x3a,<null>")
193 			.json("{authorizationUrl:'a',description:'b',flow:'c','in':'d',name:'e',scopes:{x1:'x2'},tokenUrl:'f',type:'g',x3:'x3a'}")
194 			.string("{'authorizationUrl':'a','description':'b','flow':'c','in':'d','name':'e','scopes':{'x1':'x2'},'tokenUrl':'f','type':'g','x3':'x3a'}".replace('\'', '"'))
195 		;
196 
197 		@Test void c01_gettersAndSetters() {
198 			TESTER.assertGettersAndSetters();
199 		}
200 
201 		@Test void c02_copy() {
202 			TESTER.assertCopy();
203 		}
204 
205 		@Test void c03_toJson() {
206 			TESTER.assertToJson();
207 		}
208 
209 		@Test void c04_fromJson() {
210 			TESTER.assertFromJson();
211 		}
212 
213 		@Test void c05_roundTrip() {
214 			TESTER.assertRoundTrip();
215 		}
216 
217 		@Test void c06_toString() {
218 			TESTER.assertToString();
219 		}
220 
221 		@Test void c07_keySet() {
222 			assertList(TESTER.bean().keySet(), "authorizationUrl", "description", "flow", "in", "name", "scopes", "tokenUrl", "type", "x3", "x4");
223 		}
224 
225 		@Test void c08_get() {
226 			assertMapped(
227 				TESTER.bean(), (obj,prop) -> obj.get(prop, Object.class),
228 				"authorizationUrl,description,flow,in,name,scopes{x1},tokenUrl,type,x3,x4",
229 				"a,b,c,d,e,{x2},f,g,x3a,<null>"
230 			);
231 		}
232 
233 		@Test void c09_getTypes() {
234 			assertMapped(
235 				TESTER.bean(), (obj,prop) -> simpleClassNameOf(obj.get(prop, Object.class)),
236 				"authorizationUrl,description,flow,in,name,scopes,tokenUrl,type,x3,x4",
237 				"String,String,String,String,String,LinkedHashMap,String,String,String,<null>"
238 			);
239 		}
240 
241 		@Test void c10_nullPropertyValue() {
242 			assertThrows(IllegalArgumentException.class, ()->bean().get(null));
243 			assertThrows(IllegalArgumentException.class, ()->bean().get(null, String.class));
244 			assertThrows(IllegalArgumentException.class, ()->bean().set(null, "a"));
245 		}
246 	}
247 
248 	//---------------------------------------------------------------------------------------------
249 	// Helper methods
250 	//---------------------------------------------------------------------------------------------
251 
252 	private static SecurityScheme bean() {
253 		return securityScheme();
254 	}
255 
256 }