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.swagger;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.bean.swagger.SwaggerBuilder.*;
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_asMap() {
78  			assertBean(
79  				bean()
80  					.setName("a")
81  					.setNamespace("b")
82  					.set("x1", "x1a")
83  					.asMap(),
84  				"name,namespace,x1",
85  				"a,b,x1a"
86  			);
87  		}
88  
89  		@Test void a09_extraKeys() {
90  			var x = bean().set("x1", "x1a").set("x2", "x2a");
91  			assertList(x.extraKeys(), "x1", "x2");
92  			assertEmpty(bean().extraKeys());
93  		}
94  
95  		@Test void a10_strictMode() {
96  			assertThrows(RuntimeException.class, () -> bean().strict().set("foo", "bar"));
97  			assertDoesNotThrow(() -> bean().set("foo", "bar"));
98  
99  			assertFalse(bean().isStrict());
100 			assertTrue(bean().strict().isStrict());
101 			assertFalse(bean().strict(false).isStrict());
102 		}
103 	}
104 
105 	@Nested class B_emptyTests extends TestBase {
106 
107 		private static final BeanTester<Xml> TESTER =
108 			testBean(bean())
109 			.props("name,namespace,prefix,attribute,wrapped")
110 			.vals("<null>,<null>,<null>,<null>,<null>")
111 			.json("{}")
112 			.string("{}")
113 		;
114 
115 		@Test void b01_gettersAndSetters() {
116 			TESTER.assertGettersAndSetters();
117 		}
118 
119 		@Test void b02_copy() {
120 			TESTER.assertCopy();
121 		}
122 
123 		@Test void b03_toJson() {
124 			TESTER.assertToJson();
125 		}
126 
127 		@Test void b04_fromJson() {
128 			TESTER.assertFromJson();
129 		}
130 
131 		@Test void b05_roundTrip() {
132 			TESTER.assertRoundTrip();
133 		}
134 
135 		@Test void b06_toString() {
136 			TESTER.assertToString();
137 		}
138 
139 		@Test void b07_keySet() {
140 			assertEmpty(TESTER.bean().keySet());
141 		}
142 
143 		@Test void b08_nullParameters() {
144 			var x = bean();
145 			assertThrows(IllegalArgumentException.class, () -> x.get(null, String.class));
146 			assertThrows(IllegalArgumentException.class, () -> x.set(null, "value"));
147 		}
148 	}
149 
150 	@Nested class C_extraProperties extends TestBase {
151 
152 		private static final BeanTester<Xml> TESTER =
153 			testBean(
154 				bean()
155 					.set("attribute", true)
156 					.set("name", "a")
157 					.set("namespace", "b")
158 					.set("prefix", "c")
159 					.set("wrapped", true)
160 					.set("x1", "x1a")
161 					.set("x2", null)
162 			)
163 			.props("attribute,name,namespace,prefix,wrapped,x1,x2")
164 			.vals("true,a,b,c,true,x1a,<null>")
165 			.json("{attribute:true,name:'a',namespace:'b',prefix:'c',wrapped:true,x1:'x1a'}")
166 			.string("{'attribute':true,'name':'a','namespace':'b','prefix':'c','wrapped':true,'x1':'x1a'}".replace('\'', '"'))
167 		;
168 
169 		@Test void c01_gettersAndSetters() {
170 			TESTER.assertGettersAndSetters();
171 		}
172 
173 		@Test void c02_copy() {
174 			TESTER.assertCopy();
175 		}
176 
177 		@Test void c03_toJson() {
178 			TESTER.assertToJson();
179 		}
180 
181 		@Test void c04_fromJson() {
182 			TESTER.assertFromJson();
183 		}
184 
185 		@Test void c05_roundTrip() {
186 			TESTER.assertRoundTrip();
187 		}
188 
189 		@Test void c06_toString() {
190 			TESTER.assertToString();
191 		}
192 
193 		@Test void c07_keySet() {
194 			assertList(TESTER.bean().keySet(), "attribute", "name", "namespace", "prefix", "wrapped", "x1", "x2");
195 		}
196 
197 		@Test void c08_get() {
198 			assertMapped(
199 				TESTER.bean(), (obj,prop) -> obj.get(prop, Object.class),
200 				"attribute,name,namespace,prefix,wrapped,x1,x2",
201 				"true,a,b,c,true,x1a,<null>"
202 			);
203 		}
204 
205 		@Test void c09_getTypes() {
206 			assertMapped(
207 				TESTER.bean(), (obj,prop) -> cns(obj.get(prop, Object.class)),
208 				"attribute,name,namespace,prefix,wrapped,x1,x2",
209 				"Boolean,String,String,String,Boolean,String,<null>"
210 			);
211 		}
212 
213 		@Test void c10_nullPropertyValue() {
214 			assertThrows(IllegalArgumentException.class, ()->bean().get(null));
215 			assertThrows(IllegalArgumentException.class, ()->bean().get(null, String.class));
216 			assertThrows(IllegalArgumentException.class, ()->bean().set(null, "a"));
217 		}
218 	}
219 
220 	//---------------------------------------------------------------------------------------------
221 	// Helper methods
222 	//---------------------------------------------------------------------------------------------
223 
224 	private static Xml bean() {
225 		return xml();
226 	}
227 }