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 Discriminator}.
30   */
31  class Discriminator_Test extends TestBase {
32  
33  	@Nested class A_basicTests extends TestBase {
34  
35  		private static final BeanTester<Discriminator> TESTER =
36  			testBean(
37  				bean()
38  					.setMapping(map("a1", "a2"))
39  					.setPropertyName("b")
40  			)
41  			.props("mapping{a1},propertyName")
42  			.vals("{a2},b")
43  			.json("{mapping:{a1:'a2'},propertyName:'b'}")
44  			.string("{'mapping':{'a1':'a2'},'propertyName':'b'}".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(), "mapping", "propertyName");
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_addMethods() {
82  			assertBean(
83  				bean()
84  					.addMapping("a1", "a2"),
85  				"mapping{a1}",
86  				"{a2}"
87  			);
88  		}
89  
90  		@Test void a10_asMap() {
91  			assertBean(
92  				bean()
93  					.setPropertyName("a")
94  					.set("x1", "x1a")
95  					.asMap(),
96  				"propertyName,x1",
97  				"a,x1a"
98  			);
99  		}
100 
101 		@Test void a11_extraKeys() {
102 			var x = bean().set("x1", "x1a").set("x2", "x2a");
103 			assertList(x.extraKeys(), "x1", "x2");
104 			assertEmpty(bean().extraKeys());
105 		}
106 
107 		@Test void a12_strictMode() {
108 			assertThrows(RuntimeException.class, () -> bean().strict().set("foo", "bar"));
109 			assertDoesNotThrow(() -> bean().set("foo", "bar"));
110 
111 			assertFalse(bean().isStrict());
112 			assertTrue(bean().strict().isStrict());
113 			assertFalse(bean().strict(false).isStrict());
114 		}
115 	}
116 
117 	@Nested class B_emptyTests extends TestBase {
118 
119 		private static final BeanTester<Discriminator> TESTER =
120 			testBean(bean())
121 			.props("propertyName,mapping")
122 			.vals("<null>,<null>")
123 			.json("{}")
124 			.string("{}")
125 		;
126 
127 		@Test void b01_gettersAndSetters() {
128 			TESTER.assertGettersAndSetters();
129 		}
130 
131 		@Test void b02_copy() {
132 			TESTER.assertCopy();
133 		}
134 
135 		@Test void b03_toJson() {
136 			TESTER.assertToJson();
137 		}
138 
139 		@Test void b04_fromJson() {
140 			TESTER.assertFromJson();
141 		}
142 
143 		@Test void b05_roundTrip() {
144 			TESTER.assertRoundTrip();
145 		}
146 
147 		@Test void b06_toString() {
148 			TESTER.assertToString();
149 		}
150 
151 		@Test void b07_keySet() {
152 			assertEmpty(TESTER.bean().keySet());
153 		}
154 	}
155 
156 	@Nested class C_extraProperties extends TestBase {
157 		private static final BeanTester<Discriminator> TESTER =
158 			testBean(
159 				bean()
160 					.set("mapping", m("a1", "a2"))
161 					.set("propertyName", "b")
162 					.set("x1", "x1a")
163 					.set("x2", null)
164 			)
165 			.props("mapping{a1},propertyName,x1,x2")
166 			.vals("{a2},b,x1a,<null>")
167 			.json("{mapping:{a1:'a2'},propertyName:'b',x1:'x1a'}")
168 			.string("{'mapping':{'a1':'a2'},'propertyName':'b','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(), "mapping", "propertyName", "x1", "x2");
197 		}
198 
199 		@Test void c08_get() {
200 			assertMapped(
201 				TESTER.bean(), (obj,prop) -> obj.get(prop, Object.class),
202 				"mapping{a1},propertyName,x1,x2",
203 				"{a2},b,x1a,<null>"
204 			);
205 		}
206 
207 		@Test void c09_getTypes() {
208 			assertMapped(
209 				TESTER.bean(), (obj,prop) -> cns(obj.get(prop, Object.class)),
210 				"mapping,propertyName,x1,x2",
211 				"LinkedHashMap,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 Discriminator bean() {
227 		return discriminator();
228 	}
229 }