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.rest.annotation;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.junit.bct.BctAssertions.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import java.util.*;
24  
25  import org.apache.juneau.*;
26  import org.apache.juneau.annotation.*;
27  import org.apache.juneau.http.annotation.*;
28  import org.junit.jupiter.api.*;
29  
30  class Swagger_FormData_Test extends TestBase {
31  
32  	//-----------------------------------------------------------------------------------------------------------------
33  	// Swagger
34  	//-----------------------------------------------------------------------------------------------------------------
35  
36  	@Rest
37  	public static class A {
38  
39  		@FormData(
40  			name="F"
41  		)
42  		@Schema(description={"a","b"}, type="string")
43  		public static class A1 {
44  			public A1(String x) {}
45  		}
46  		@RestGet
47  		public void a(A1 f) { /* no-op */ }
48  
49  		@FormData(
50  			name="F",
51  			schema=@Schema(description="a\nb",type="string")
52  		)
53  		public static class A2 {
54  			public A2(String x) {}
55  		}
56  		@RestPut
57  		public void b(A2 f) { /* no-op */ }
58  
59  		@FormData(
60  			name="F",
61  			schema=@Schema(description="b\nc",type="string")
62  		)
63  		@Schema(description={"a","b"}, type="string")
64  		public static class A3 {
65  			public A3(String x) {}
66  		}
67  		@RestPost
68  		public void c(A3 f) { /* no-op */ }
69  
70  		@FormData("F")
71  		public static class A4 {}
72  		@RestDelete
73  		public void d(A4 f) { /* no-op */ }
74  	}
75  
76  	@Test void a01_fromPojo() {
77  		var s = getSwagger(A.class);
78  		var x = s.getParameterInfo("/a","get","formData","F");
79  		assertBean(x, "name,description,type", "F,a\nb,string");
80  
81  		x = s.getParameterInfo("/b","put","formData","F");
82  		assertBean(x, "name,description,type", "F,a\nb,string");
83  
84  		x = s.getParameterInfo("/c","post","formData","F");
85  		assertBean(x, "name,description,type", "F,b\nc,string");
86  
87  		x = s.getParameterInfo("/d","delete","formData","F");
88  		assertEquals("F", x.getName());
89  	}
90  
91  	@Rest
92  	public static class B {
93  
94  		@FormData(name="F")
95  		public static class B1 {}
96  		@RestGet
97  		public void a(B1 f) { /* no-op */ }
98  
99  		@FormData("F")
100 		public static class B2 {
101 			public String f1;
102 		}
103 		@RestPut
104 		public void b(B2 f) { /* no-op */ }
105 
106 		@FormData("F")
107 		public static class B3 extends LinkedList<String> {
108 			private static final long serialVersionUID = 1L;
109 		}
110 		@RestPost
111 		public void c(B3 f) { /* no-op */ }
112 
113 		@FormData("F")
114 		public static class B4 {}
115 		@RestDelete
116 		public void d(B4 f) { /* no-op */ }
117 	}
118 
119 	@Test void b01_schemaFromPojo() {
120 		var s = getSwagger(B.class);
121 		var x = s.getParameterInfo("/a","get","formData","F");
122 
123 		assertBean(x, "in,name,type", "formData,F,string");
124 
125 		x = s.getParameterInfo("/b","put","formData","F");
126 		assertBean(x, "in,name,type,schema{properties{f1{type}}}", "formData,F,object,{{{string}}}");
127 
128 		x = s.getParameterInfo("/c","post","formData","F");
129 		assertBean(x, "in,name,type,items{type}", "formData,F,array,{string}");
130 
131 		x = s.getParameterInfo("/d","delete","formData","F");
132 		assertBean(x, "in,name,type", "formData,F,string");
133 	}
134 
135 	@Rest
136 	public static class D {
137 
138 		@RestGet
139 		public void a(
140 			@FormData(
141 				name="F"
142 			)
143 			@Schema(description={"a","b"}, type="string")
144 			String f) { /* no-op */ }
145 
146 		@RestPut
147 		public void b(
148 			@FormData(
149 				name="F",
150 				schema=@Schema(description="a\nb",type="string")
151 			) String f) { /* no-op */ }
152 
153 		@RestPost
154 		public void c(
155 			@FormData(
156 				name="F",
157 				schema=@Schema(description="b\nc",type="string")
158 			)
159 			@Schema(description={"a","b"}, type="string")
160 			String f) { /* no-op */ }
161 
162 		@RestDelete
163 		public void d(@FormData("F") String f) { /* no-op */ }
164 	}
165 
166 	@Test void d01_fromParameter() {
167 		var s = getSwagger(D.class);
168 		var x = s.getParameterInfo("/a","get","formData","F");
169 		assertBean(x, "name,description,type", "F,a\nb,string");
170 
171 		x = s.getParameterInfo("/b","put","formData","F");
172 		assertBean(x, "name,description,type", "F,a\nb,string");
173 
174 		x = s.getParameterInfo("/c","post","formData","F");
175 		assertBean(x, "name,description,type", "F,b\nc,string");
176 
177 		x = s.getParameterInfo("/d","delete","formData","F");
178 		assertEquals("F", x.getName());
179 	}
180 
181 	@Rest
182 	public static class E {
183 
184 		@RestGet
185 		public void a(@FormData(name="F") String f) { /* no-op */ }
186 
187 		public static class E2 {
188 			public String f1;
189 		}
190 		@RestPut
191 		public void b(@FormData("F") E2 b) { /* no-op */ }
192 
193 		public static class E3 extends LinkedList<String> {
194 			private static final long serialVersionUID = 1L;
195 		}
196 		@RestPost
197 		public void c(@FormData("F") E3 b) { /* no-op */ }
198 
199 		public static class E4 {}
200 		@RestDelete
201 		public void d(@FormData("F") E4 b) { /* no-op */ }
202 
203 		@RestOp
204 		public void e(@FormData("F") Integer b) { /* no-op */ }
205 
206 		@RestGet
207 		public void f(@FormData("F") Boolean b) { /* no-op */ }
208 	}
209 
210 	@Test void e01_schemaFromParameter() {
211 		var s = getSwagger(E.class);
212 		var x = s.getParameterInfo("/a","get","formData","F");
213 
214 		assertBean(x, "in,name,type", "formData,F,string");
215 
216 		x = s.getParameterInfo("/b","put","formData","F");
217 		assertBean(x, "in,name,type,schema{properties{f1{type}}}", "formData,F,object,{{{string}}}");
218 
219 		x = s.getParameterInfo("/c","post","formData","F");
220 		assertBean(x, "in,name,type,items{type}", "formData,F,array,{string}");
221 
222 		x = s.getParameterInfo("/d","delete","formData","F");
223 		assertBean(x, "in,name,type", "formData,F,string");
224 
225 		x = s.getParameterInfo("/e","get","formData","F");
226 		assertBean(x, "in,name,type,format", "formData,F,integer,int32");
227 
228 		x = s.getParameterInfo("/f","get","formData","F");
229 		assertBean(x, "in,name,type", "formData,F,boolean");
230 	}
231 }