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  
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_Path_Test extends TestBase {
30  
31  	//-----------------------------------------------------------------------------------------------------------------
32  	// Swagger tests
33  	//-----------------------------------------------------------------------------------------------------------------
34  
35  	@Rest
36  	public static class A {
37  
38  		@Path("P")
39  		@Schema(
40  			d={"a","b"},
41  			e="a,b",
42  			t="string"
43  		)
44  		public static class A1 {
45  			public A1(String x) {}
46  			@Override
47  			public String toString() {
48  				return "a";
49  			}
50  		}
51  		@RestGet(path="/a/{P}")
52  		public void a(A1 f) { /* no-op */ }
53  
54  		@Path(
55  			name="P",
56  			schema=@Schema(description="a\nb",type="string",enum_={"a","b"})
57  		)
58  		public static class A2 {
59  			public A2(String x) {}
60  			@Override
61  			public String toString() {
62  				return "b";
63  			}
64  		}
65  		@RestPut(path="/b/{P}")
66  		public void b(A2 f) { /* no-op */ }
67  
68  		@Path(
69  			name="P",
70  			schema=@Schema(description="b\nc",type="string",enum_={"b","c"})
71  		)
72  		@Schema(
73  			d={"a","b"},
74  			t="string",
75  			e="a,b"
76  		)
77  		public static class A3 {
78  			public A3(String x) {}
79  			@Override
80  			public String toString() {
81  				return "c";
82  			}
83  		}
84  		@RestPost(path="/c/{P}")
85  		public void c(A3 f) { /* no-op */ }
86  
87  		@Path("P")
88  		public static class A4 {
89  			@Override
90  			public String toString() {
91  				return "d";
92  			}
93  		}
94  		@RestDelete(path="/d/{P}")
95  		public void d(A4 f) { /* no-op */ }
96  
97  		@Path("P")
98  		@Schema(e="a,b")
99  		public static class A5 {
100 			@Override
101 			public String toString() {
102 				return "e";
103 			}
104 		}
105 		@RestOp(path="/e/{P}")
106 		public void e(A5 f) { /* no-op */ }
107 	}
108 
109 	@Test void a01_fromPojo() {
110 		var s = getSwagger(A.class);
111 		var x = s.getParameterInfo("/a/{P}","get","path","P");
112 
113 		assertBean(x, "in,name,type,description,required,enum", "path,P,string,a\nb,true,[a,b]");
114 
115 		x = s.getParameterInfo("/b/{P}","put","path","P");
116 		assertBean(x, "in,name,type,description,required,enum", "path,P,string,a\nb,true,[a,b]");
117 
118 		x = s.getParameterInfo("/c/{P}","post","path","P");
119 		assertBean(x, "in,name,type,description,required,enum", "path,P,string,b\nc,true,[b,c]");
120 
121 		x = s.getParameterInfo("/d/{P}","delete","path","P");
122 		assertBean(x, "in,name,type,required", "path,P,string,true");
123 
124 		x = s.getParameterInfo("/e/{P}","get","path","P");
125 		assertBean(x, "in,name,type,required,enum", "path,P,string,true,[a,b]");
126 	}
127 
128 	@Rest
129 	public static class B {
130 
131 		@Path(name="P")
132 		public static class B1 {}
133 		@RestGet(path="/a/{P}")
134 		public void a(B1 f) { /* no-op */ }
135 
136 		@Path("P")
137 		public static class B2 {
138 			public String f1;
139 		}
140 		@RestPut(path="/b/{P}")
141 		public void b(B2 b) { /* no-op */ }
142 
143 		@Path("P")
144 		public static class B3 extends LinkedList<String> {
145 			private static final long serialVersionUID = 1L;
146 		}
147 		@RestPost(path="/c/{P}")
148 		public void c(B3 b) { /* no-op */ }
149 
150 		@Path("P")
151 		public static class B4 {}
152 		@RestDelete(path="/d/{P}")
153 		public void d(B4 b) { /* no-op */ }
154 	}
155 
156 	@Test void b01_schemaFromPojo() {
157 		var s = getSwagger(B.class);
158 		var x = s.getParameterInfo("/a/{P}","get","path","P");
159 
160 		assertBean(x, "in,name,type,required", "path,P,string,true");
161 
162 		x = s.getParameterInfo("/b/{P}","put","path","P");
163 		assertBean(x, "in,name,type,required,schema{properties{f1{type}}}", "path,P,object,true,{{{string}}}");
164 
165 		x = s.getParameterInfo("/c/{P}","post","path","P");
166 		assertBean(x, "in,name,type,required,items{type}", "path,P,array,true,{string}");
167 
168 		x = s.getParameterInfo("/d/{P}","delete","path","P");
169 		assertBean(x, "in,name,type,required", "path,P,string,true");
170 	}
171 
172 	@Rest
173 	public static class D {
174 
175 		@RestGet(path="/a/{P}")
176 		public void a(
177 			@Path("P")
178 			@Schema(d="a", t="string")
179 			String h
180 		) { /* no-op */ }
181 
182 		@RestPut(path="/b/{P}")
183 		public void b(
184 			@Path(
185 				name="P",
186 				schema=@Schema(description="a",type="string")
187 			)
188 			String h
189 		) { /* no-op */ }
190 
191 		@RestPost(path="/c/{P}")
192 		public void c(
193 			@Path(
194 				name="P",
195 				schema=@Schema(description="b",type="string")
196 			)
197 			@Schema(d="a", t="string")
198 			String h
199 		) { /* no-op */ }
200 
201 		@RestDelete(path="/d/{P}")
202 		public void d(@Path("P") String h) { /* no-op */ }
203 
204 		@RestOp(path="/e/{P}")
205 		public void e(@Path("P") @Schema(e="a,b") String h) { /* no-op */ }
206 	}
207 
208 	@Test void d01_fromParameter() {
209 		var s = getSwagger(D.class);
210 		var x = s.getParameterInfo("/a/{P}","get","path","P");
211 
212 		assertBean(x, "description,type", "a,string");
213 
214 		x = s.getParameterInfo("/b/{P}","put","path","P");
215 		assertBean(x, "description,type", "a,string");
216 
217 		x = s.getParameterInfo("/c/{P}","post","path","P");
218 		assertBean(x, "description,type", "b,string");
219 
220 		x = s.getParameterInfo("/d/{P}","delete","path","P");
221 
222 		x = s.getParameterInfo("/e/{P}","get","path","P");
223 		assertList(x.getEnum(), "a", "b");
224 	}
225 
226 	@Rest
227 	public static class E {
228 
229 		@RestGet(path="/a/{P}")
230 		public void a(@Path("P") String h) { /* no-op */ }
231 
232 		public static class E2 {
233 			public String f1;
234 		}
235 		@RestPut(path="/b/{P}")
236 		public void b(@Path("P") E2 b) { /* no-op */ }
237 
238 		public static class E3 extends LinkedList<String> {
239 			private static final long serialVersionUID = 1L;
240 		}
241 		@RestPost(path="/c/{P}")
242 		public void c(@Path("P") E3 b) { /* no-op */ }
243 
244 		public static class E4 {}
245 		@RestDelete(path="/d/{P}")
246 		public void d(@Path("P") E4 b) { /* no-op */ }
247 
248 		@RestOp(path="/e/{P}")
249 		public void e(@Path("P") Integer b) { /* no-op */ }
250 
251 		@RestGet(path="/f/{P}")
252 		public void f(@Path("P") Boolean b) { /* no-op */ }
253 	}
254 
255 	@Test void d01_schemaFromParameter() {
256 		var s = getSwagger(E.class);
257 		var x = s.getParameterInfo("/a/{P}","get","path","P");
258 
259 		assertBean(x, "in,name,type,required", "path,P,string,true");
260 
261 		x = s.getParameterInfo("/b/{P}","put","path","P");
262 		assertBean(x, "in,name,type,required,schema{properties{f1{type}}}", "path,P,object,true,{{{string}}}");
263 
264 		x = s.getParameterInfo("/c/{P}","post","path","P");
265 		assertBean(x, "in,name,type,required,items{type}", "path,P,array,true,{string}");
266 
267 		x = s.getParameterInfo("/d/{P}","delete","path","P");
268 		assertBean(x, "in,name,type,required", "path,P,string,true");
269 
270 		x = s.getParameterInfo("/e/{P}","get","path","P");
271 		assertBean(x, "in,name,type,required,format", "path,P,integer,true,int32");
272 
273 		x = s.getParameterInfo("/f/{P}","get","path","P");
274 		assertBean(x, "in,name,type,required", "path,P,boolean,true");
275 	}
276 }