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 Example}.
28   */
29  class Example_Test extends TestBase {
30  
31  	@Nested class A_basicTests extends TestBase {
32  
33  		private static final BeanTester<Example> TESTER =
34  			testBean(
35  				bean()
36  					.setDescription("a")
37  					.setExternalValue("b")
38  					.setSummary("c")
39  					.setValue("d")
40  			)
41  			.props("description,externalValue,summary,value")
42  			.vals("a,b,c,d")
43  			.json("{description:'a',externalValue:'b',summary:'c',value:'d'}")
44  			.string("{'description':'a','externalValue':'b','summary':'c','value':'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(), "description", "externalValue", "summary", "value");
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  					.setSummary("a")
85  					.set("x1", "x1a")
86  					.asMap(),
87  				"summary,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<Example> TESTER =
111 			testBean(bean())
112 			.props("summary,description,externalValue,value")
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<Example> TESTER =
149 			testBean(
150 				bean()
151 					.set("description", "a")
152 					.set("externalValue", "b")
153 					.set("summary", "c")
154 					.set("value", "d")
155 					.set("x1", "x1a")
156 					.set("x2", null)
157 			)
158 			.props("description,externalValue,summary,value,x1,x2")
159 			.vals("a,b,c,d,x1a,<null>")
160 			.json("{description:'a',externalValue:'b',summary:'c',value:'d',x1:'x1a'}")
161 			.string("{'description':'a','externalValue':'b','summary':'c','value':'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(), "description", "externalValue", "summary", "value", "x1", "x2");
190 		}
191 
192 		@Test void c08_get() {
193 			assertMapped(
194 				TESTER.bean(), (obj,prop) -> obj.get(prop, Object.class),
195 				"description,externalValue,summary,value,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 				"description,externalValue,summary,value,x1,x2",
204 				"String,String,String,String,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 Example bean() {
220 		return example();
221 	}
222 }