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