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 Xml}.
28   */
29  class Xml_Test extends TestBase {
30  
31  	@Nested class A_basicTests extends TestBase {
32  
33  		private static final BeanTester<Xml> TESTER =
34  			testBean(
35  				bean()
36  					.setAttribute(true)
37  					.setName("a")
38  					.setNamespace("b")
39  					.setPrefix("c")
40  					.setWrapped(true)
41  			)
42  			.props("attribute,name,namespace,prefix,wrapped")
43  			.vals("true,a,b,c,true")
44  			.json("{attribute:true,name:'a',namespace:'b',prefix:'c',wrapped:true}")
45  			.string("{'attribute':true,'name':'a','namespace':'b','prefix':'c','wrapped':true}".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(), "attribute", "name", "namespace", "prefix", "wrapped");
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  					.setName("a")
86  					.set("x1", "x1a")
87  					.asMap(),
88  				"name,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<Xml> TESTER =
112 			testBean(bean())
113 			.props("name,namespace,prefix,attribute,wrapped")
114 			.vals("<null>,<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<Xml> TESTER =
150 			testBean(
151 				bean()
152 					.set("attribute", true)
153 					.set("name", "a")
154 					.set("namespace", "b")
155 					.set("prefix", "c")
156 					.set("wrapped", true)
157 					.set("x1", "x1a")
158 					.set("x2", null)
159 			)
160 			.props("attribute,name,namespace,prefix,wrapped,x1,x2")
161 			.vals("true,a,b,c,true,x1a,<null>")
162 			.json("{attribute:true,name:'a',namespace:'b',prefix:'c',wrapped:true,x1:'x1a'}")
163 			.string("{'attribute':true,'name':'a','namespace':'b','prefix':'c','wrapped':true,'x1':'x1a'}".replace('\'', '"'))
164 		;
165 
166 		@Test void c01_gettersAndSetters() {
167 			TESTER.assertGettersAndSetters();
168 		}
169 
170 		@Test void c02_copy() {
171 			TESTER.assertCopy();
172 		}
173 
174 		@Test void c03_toJson() {
175 			TESTER.assertToJson();
176 		}
177 
178 		@Test void c04_fromJson() {
179 			TESTER.assertFromJson();
180 		}
181 
182 		@Test void c05_roundTrip() {
183 			TESTER.assertRoundTrip();
184 		}
185 
186 		@Test void c06_toString() {
187 			TESTER.assertToString();
188 		}
189 
190 		@Test void c07_keySet() {
191 			assertList(TESTER.bean().keySet(), "attribute", "name", "namespace", "prefix", "wrapped", "x1", "x2");
192 		}
193 
194 		@Test void c08_get() {
195 			assertMapped(
196 				TESTER.bean(), (obj,prop) -> obj.get(prop, Object.class),
197 				"attribute,name,namespace,prefix,wrapped,x1,x2",
198 				"true,a,b,c,true,x1a,<null>"
199 			);
200 		}
201 
202 		@Test void c09_getTypes() {
203 			assertMapped(
204 				TESTER.bean(), (obj,prop) -> simpleClassNameOf(obj.get(prop, Object.class)),
205 				"attribute,name,namespace,prefix,wrapped,x1,x2",
206 				"Boolean,String,String,String,Boolean,String,<null>"
207 			);
208 		}
209 
210 		@Test void c10_nullPropertyValue() {
211 			assertThrows(IllegalArgumentException.class, ()->bean().get(null));
212 			assertThrows(IllegalArgumentException.class, ()->bean().get(null, String.class));
213 			assertThrows(IllegalArgumentException.class, ()->bean().set(null, "a"));
214 		}
215 	}
216 
217 	//---------------------------------------------------------------------------------------------
218 	// Helper methods
219 	//---------------------------------------------------------------------------------------------
220 
221 	private static Xml bean() {
222 		return xml();
223 	}
224 }