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