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