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