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