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