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