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