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 OAuthFlows}.
28   */
29  class OAuthFlows_Test extends TestBase {
30  
31  	@Nested class A_basicTests extends TestBase {
32  
33  		private static final BeanTester<OAuthFlows> TESTER =
34  			testBean(
35  				bean()
36  					.setAuthorizationCode(oAuthFlow().setAuthorizationUrl("a1").setTokenUrl("a2"))
37  					.setClientCredentials(oAuthFlow().setTokenUrl("b"))
38  					.setImplicit(oAuthFlow().setAuthorizationUrl("c"))
39  					.setPassword(oAuthFlow().setTokenUrl("d"))
40  			)
41  			.props("authorizationCode{authorizationUrl,tokenUrl},clientCredentials{tokenUrl},implicit{authorizationUrl},password{tokenUrl}")
42  			.vals("{a1,a2},{b},{c},{d}")
43  			.json("{authorizationCode:{authorizationUrl:'a1',tokenUrl:'a2'},clientCredentials:{tokenUrl:'b'},implicit:{authorizationUrl:'c'},password:{tokenUrl:'d'}}")
44  			.string("{'authorizationCode':{'authorizationUrl':'a1','tokenUrl':'a2'},'clientCredentials':{'tokenUrl':'b'},'implicit':{'authorizationUrl':'c'},'password':{'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(), "authorizationCode", "clientCredentials", "implicit", "password");
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_asMap() {
82  			assertBean(
83  				bean()
84  					.setImplicit(oAuthFlow().setAuthorizationUrl("a"))
85  					.set("x1", "x1a")
86  					.asMap(),
87  				"implicit{authorizationUrl},x1",
88  				"{a},x1a"
89  			);
90  		}
91  
92  		@Test void a10_extraKeys() {
93  			var x = bean().set("x1", "x1a").set("x2", "x2a");
94  			assertList(x.extraKeys(), "x1", "x2");
95  			assertEmpty(bean().extraKeys());
96  		}
97  
98  		@Test void a11_strictMode() {
99  			assertThrows(RuntimeException.class, () -> bean().strict().set("foo", "bar"));
100 			assertDoesNotThrow(() -> bean().set("foo", "bar"));
101 
102 			assertFalse(bean().isStrict());
103 			assertTrue(bean().strict().isStrict());
104 			assertFalse(bean().strict(false).isStrict());
105 		}
106 	}
107 
108 	@Nested class B_emptyTests extends TestBase {
109 
110 		private static final BeanTester<OAuthFlows> TESTER =
111 			testBean(bean())
112 			.props("implicit,password,clientCredentials,authorizationCode")
113 			.vals("<null>,<null>,<null>,<null>")
114 			.json("{}")
115 			.string("{}")
116 		;
117 
118 		@Test void b01_gettersAndSetters() {
119 			TESTER.assertGettersAndSetters();
120 		}
121 
122 		@Test void b02_copy() {
123 			TESTER.assertCopy();
124 		}
125 
126 		@Test void b03_toJson() {
127 			TESTER.assertToJson();
128 		}
129 
130 		@Test void b04_fromJson() {
131 			TESTER.assertFromJson();
132 		}
133 
134 		@Test void b05_roundTrip() {
135 			TESTER.assertRoundTrip();
136 		}
137 
138 		@Test void b06_toString() {
139 			TESTER.assertToString();
140 		}
141 
142 		@Test void b07_keySet() {
143 			assertEmpty(TESTER.bean().keySet());
144 		}
145 	}
146 
147 	@Nested class C_extraProperties extends TestBase {
148 		private static final BeanTester<OAuthFlows> TESTER =
149 			testBean(
150 				bean()
151 					.set("authorizationCode", oAuthFlow().setAuthorizationUrl("a"))
152 					.set("clientCredentials", oAuthFlow().setTokenUrl("b"))
153 					.set("implicit", oAuthFlow().setAuthorizationUrl("c"))
154 					.set("password", oAuthFlow().setTokenUrl("d"))
155 					.set("x1", "x1a")
156 					.set("x2", null)
157 			)
158 			.props("authorizationCode{authorizationUrl},clientCredentials{tokenUrl},implicit{authorizationUrl},password{tokenUrl},x1,x2")
159 			.vals("{a},{b},{c},{d},x1a,<null>")
160 			.json("{authorizationCode:{authorizationUrl:'a'},clientCredentials:{tokenUrl:'b'},implicit:{authorizationUrl:'c'},password:{tokenUrl:'d'},x1:'x1a'}")
161 			.string("{'authorizationCode':{'authorizationUrl':'a'},'clientCredentials':{'tokenUrl':'b'},'implicit':{'authorizationUrl':'c'},'password':{'tokenUrl':'d'},'x1':'x1a'}".replace('\'', '"'))
162 		;
163 
164 		@Test void c01_gettersAndSetters() {
165 			TESTER.assertGettersAndSetters();
166 		}
167 
168 		@Test void c02_copy() {
169 			TESTER.assertCopy();
170 		}
171 
172 		@Test void c03_toJson() {
173 			TESTER.assertToJson();
174 		}
175 
176 		@Test void c04_fromJson() {
177 			TESTER.assertFromJson();
178 		}
179 
180 		@Test void c05_roundTrip() {
181 			TESTER.assertRoundTrip();
182 		}
183 
184 		@Test void c06_toString() {
185 			TESTER.assertToString();
186 		}
187 
188 		@Test void c07_keySet() {
189 			assertList(TESTER.bean().keySet(), "authorizationCode", "clientCredentials", "implicit", "password", "x1", "x2");
190 		}
191 
192 		@Test void c08_get() {
193 			assertMapped(
194 				TESTER.bean(), (obj,prop) -> obj.get(prop, Object.class),
195 				"authorizationCode{authorizationUrl},clientCredentials{tokenUrl},implicit{authorizationUrl},password{tokenUrl},x1,x2",
196 				"{a},{b},{c},{d},x1a,<null>"
197 			);
198 		}
199 
200 		@Test void c09_getTypes() {
201 			assertMapped(
202 				TESTER.bean(), (obj,prop) -> simpleClassNameOf(obj.get(prop, Object.class)),
203 				"authorizationCode,clientCredentials,implicit,password,x1,x2",
204 				"OAuthFlow,OAuthFlow,OAuthFlow,OAuthFlow,String,<null>"
205 			);
206 		}
207 
208 		@Test void c10_nullPropertyValue() {
209 			assertThrows(IllegalArgumentException.class, ()->bean().get(null));
210 			assertThrows(IllegalArgumentException.class, ()->bean().get(null, String.class));
211 			assertThrows(IllegalArgumentException.class, ()->bean().set(null, "a"));
212 		}
213 	}
214 
215 	//---------------------------------------------------------------------------------------------
216 	// Helper methods
217 	//---------------------------------------------------------------------------------------------
218 
219 	private static OAuthFlows bean() {
220 		return oAuthFlows();
221 	}
222 }