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 RequestBodyInfo}.
30   */
31  class RequestBodyInfo_Test extends TestBase {
32  
33  	@Nested class A_basicTests extends TestBase {
34  
35  		private static final BeanTester<RequestBodyInfo> TESTER =
36  			testBean(
37  				bean()
38  					.setContent(map("a1", mediaType().setSchema(schemaInfo("a2"))))
39  					.setDescription("b")
40  					.setRequired(true)
41  			)
42  			.props("content{a1{schema{type}}},description,required")
43  			.vals("{{{a2}}},b,true")
44  			.json("{content:{a1:{schema:{type:'a2'}}},description:'b',required:true}")
45  			.string("{'content':{'a1':{'schema':{'type':'a2'}}},'description':'b','required':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(), "content", "description", "required");
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_addMethods() {
83  			assertBean(
84  				bean()
85  					.addContent("a1", mediaType().setSchema(schemaInfo("a2")))
86  					.addContent("a3", mediaType().setSchema(schemaInfo("a4"))),
87  				"content{a1{schema{type}},a3{schema{type}}}",
88  				"{{{a2}},{{a4}}}"
89  			);
90  		}
91  
92  		@Test void a10_asMap() {
93  			assertBean(
94  				bean()
95  					.setDescription("a")
96  					.set("x1", "x1a")
97  					.asMap(),
98  				"description,x1",
99  				"a,x1a"
100 			);
101 		}
102 
103 		@Test void a11_extraKeys() {
104 			var x = bean().set("x1", "x1a").set("x2", "x2a");
105 			assertList(x.extraKeys(), "x1", "x2");
106 			assertEmpty(bean().extraKeys());
107 		}
108 
109 		@Test void a12_strictMode() {
110 			assertThrows(RuntimeException.class, () -> bean().strict().set("foo", "bar"));
111 			assertDoesNotThrow(() -> bean().set("foo", "bar"));
112 
113 			assertFalse(bean().isStrict());
114 			assertTrue(bean().strict().isStrict());
115 			assertFalse(bean().strict(false).isStrict());
116 		}
117 	}
118 
119 	@Nested class B_emptyTests extends TestBase {
120 
121 		private static final BeanTester<RequestBodyInfo> TESTER =
122 			testBean(bean())
123 			.props("description,content,required")
124 			.vals("<null>,<null>,<null>")
125 			.json("{}")
126 			.string("{}")
127 		;
128 
129 		@Test void b01_gettersAndSetters() {
130 			TESTER.assertGettersAndSetters();
131 		}
132 
133 		@Test void b02_copy() {
134 			TESTER.assertCopy();
135 		}
136 
137 		@Test void b03_toJson() {
138 			TESTER.assertToJson();
139 		}
140 
141 		@Test void b04_fromJson() {
142 			TESTER.assertFromJson();
143 		}
144 
145 		@Test void b05_roundTrip() {
146 			TESTER.assertRoundTrip();
147 		}
148 
149 		@Test void b06_toString() {
150 			TESTER.assertToString();
151 		}
152 
153 		@Test void b07_keySet() {
154 			assertEmpty(TESTER.bean().keySet());
155 		}
156 	}
157 
158 	@Nested class C_extraProperties extends TestBase {
159 		private static final BeanTester<RequestBodyInfo> TESTER =
160 			testBean(
161 				bean()
162 					.set("content", m("a1", mediaType().setSchema(schemaInfo("a2"))))
163 					.set("description", "b")
164 					.set("required", true)
165 					.set("x1", "x1a")
166 					.set("x2", null)
167 			)
168 			.props("content{a1{schema{type}}},description,required,x1,x2")
169 			.vals("{{{a2}}},b,true,x1a,<null>")
170 			.json("{content:{a1:{schema:{type:'a2'}}},description:'b',required:true,x1:'x1a'}")
171 			.string("{'content':{'a1':{'schema':{'type':'a2'}}},'description':'b','required':true,'x1':'x1a'}".replace('\'', '"'))
172 		;
173 
174 		@Test void c01_gettersAndSetters() {
175 			TESTER.assertGettersAndSetters();
176 		}
177 
178 		@Test void c02_copy() {
179 			TESTER.assertCopy();
180 		}
181 
182 		@Test void c03_toJson() {
183 			TESTER.assertToJson();
184 		}
185 
186 		@Test void c04_fromJson() {
187 			TESTER.assertFromJson();
188 		}
189 
190 		@Test void c05_roundTrip() {
191 			TESTER.assertRoundTrip();
192 		}
193 
194 		@Test void c06_toString() {
195 			TESTER.assertToString();
196 		}
197 
198 		@Test void c07_keySet() {
199 			assertList(TESTER.bean().keySet(), "content", "description", "required", "x1", "x2");
200 		}
201 
202 		@Test void c08_get() {
203 			assertMapped(
204 				TESTER.bean(), (obj,prop) -> obj.get(prop, Object.class),
205 				"content{a1{schema{type}}},description,required,x1,x2",
206 				"{{{a2}}},b,true,x1a,<null>"
207 			);
208 		}
209 
210 		@Test void c09_getTypes() {
211 			assertMapped(
212 				TESTER.bean(), (obj,prop) -> cns(obj.get(prop, Object.class)),
213 				"content,description,required,x1,x2",
214 				"LinkedHashMap,String,Boolean,String,<null>"
215 			);
216 		}
217 
218 		@Test void c10_nullPropertyValue() {
219 			assertThrows(IllegalArgumentException.class, ()->bean().get(null));
220 			assertThrows(IllegalArgumentException.class, ()->bean().get(null, String.class));
221 			assertThrows(IllegalArgumentException.class, ()->bean().set(null, "a"));
222 		}
223 	}
224 
225 	//---------------------------------------------------------------------------------------------
226 	// Helper methods
227 	//---------------------------------------------------------------------------------------------
228 
229 	private static RequestBodyInfo bean() {
230 		return requestBodyInfo();
231 	}
232 }