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 RequestBodyInfo}.
28   */
29  class RequestBodyInfo_Test extends TestBase {
30  
31  	@Nested class A_basicTests extends TestBase {
32  
33  		private static final BeanTester<RequestBodyInfo> TESTER =
34  			testBean(
35  				bean()
36  					.setContent(map("a1", mediaType().setSchema(schemaInfo("a2"))))
37  					.setDescription("b")
38  					.setRequired(true)
39  			)
40  			.props("content{a1{schema{type}}},description,required")
41  			.vals("{{{a2}}},b,true")
42  			.json("{content:{a1:{schema:{type:'a2'}}},description:'b',required:true}")
43  			.string("{'content':{'a1':{'schema':{'type':'a2'}}},'description':'b','required':true}".replace('\'','"'))
44  		;
45  
46  		@Test void a01_gettersAndSetters() {
47  			TESTER.assertGettersAndSetters();
48  		}
49  
50  		@Test void a02_copy() {
51  			TESTER.assertCopy();
52  		}
53  
54  		@Test void a03_toJson() {
55  			TESTER.assertToJson();
56  		}
57  
58  		@Test void a04_fromJson() {
59  			TESTER.assertFromJson();
60  		}
61  
62  		@Test void a05_roundTrip() {
63  			TESTER.assertRoundTrip();
64  		}
65  
66  		@Test void a06_toString() {
67  			TESTER.assertToString();
68  		}
69  
70  		@Test void a07_keySet() {
71  			assertList(TESTER.bean().keySet(), "content", "description", "required");
72  		}
73  
74  		@Test void a08_nullParameters() {
75  			var x = bean();
76  			assertThrows(IllegalArgumentException.class, () -> x.get(null, String.class));
77  			assertThrows(IllegalArgumentException.class, () -> x.set(null, "value"));
78  		}
79  
80  		@Test void a09_addMethods() {
81  			assertBean(
82  				bean()
83  					.addContent("a1", mediaType().setSchema(schemaInfo("a2")))
84  					.addContent("a3", mediaType().setSchema(schemaInfo("a4"))),
85  				"content{a1{schema{type}},a3{schema{type}}}",
86  				"{{{a2}},{{a4}}}"
87  			);
88  		}
89  
90  		@Test void a10_asMap() {
91  			assertBean(
92  				bean()
93  					.setDescription("a")
94  					.set("x1", "x1a")
95  					.asMap(),
96  				"description,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<RequestBodyInfo> TESTER =
120 			testBean(bean())
121 			.props("description,content,required")
122 			.vals("<null>,<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<RequestBodyInfo> TESTER =
158 			testBean(
159 				bean()
160 					.set("content", map("a1", mediaType().setSchema(schemaInfo("a2"))))
161 					.set("description", "b")
162 					.set("required", true)
163 					.set("x1", "x1a")
164 					.set("x2", null)
165 			)
166 			.props("content{a1{schema{type}}},description,required,x1,x2")
167 			.vals("{{{a2}}},b,true,x1a,<null>")
168 			.json("{content:{a1:{schema:{type:'a2'}}},description:'b',required:true,x1:'x1a'}")
169 			.string("{'content':{'a1':{'schema':{'type':'a2'}}},'description':'b','required':true,'x1':'x1a'}".replace('\'', '"'))
170 		;
171 
172 		@Test void c01_gettersAndSetters() {
173 			TESTER.assertGettersAndSetters();
174 		}
175 
176 		@Test void c02_copy() {
177 			TESTER.assertCopy();
178 		}
179 
180 		@Test void c03_toJson() {
181 			TESTER.assertToJson();
182 		}
183 
184 		@Test void c04_fromJson() {
185 			TESTER.assertFromJson();
186 		}
187 
188 		@Test void c05_roundTrip() {
189 			TESTER.assertRoundTrip();
190 		}
191 
192 		@Test void c06_toString() {
193 			TESTER.assertToString();
194 		}
195 
196 		@Test void c07_keySet() {
197 			assertList(TESTER.bean().keySet(), "content", "description", "required", "x1", "x2");
198 		}
199 
200 		@Test void c08_get() {
201 			assertMapped(
202 				TESTER.bean(), (obj,prop) -> obj.get(prop, Object.class),
203 				"content{a1{schema{type}}},description,required,x1,x2",
204 				"{{{a2}}},b,true,x1a,<null>"
205 			);
206 		}
207 
208 		@Test void c09_getTypes() {
209 			assertMapped(
210 				TESTER.bean(), (obj,prop) -> simpleClassNameOf(obj.get(prop, Object.class)),
211 				"content,description,required,x1,x2",
212 				"LinkedHashMap,String,Boolean,String,<null>"
213 			);
214 		}
215 
216 		@Test void c10_nullPropertyValue() {
217 			assertThrows(IllegalArgumentException.class, ()->bean().get(null));
218 			assertThrows(IllegalArgumentException.class, ()->bean().get(null, String.class));
219 			assertThrows(IllegalArgumentException.class, ()->bean().set(null, "a"));
220 		}
221 	}
222 
223 	//---------------------------------------------------------------------------------------------
224 	// Helper methods
225 	//---------------------------------------------------------------------------------------------
226 
227 	private static RequestBodyInfo bean() {
228 		return requestBodyInfo();
229 	}
230 }