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_Query_Test extends TestBase {
30  
31  	//-----------------------------------------------------------------------------------------------------------------
32  	// Swagger tests
33  	//-----------------------------------------------------------------------------------------------------------------
34  
35  	@Rest
36  	public static class A {
37  
38  		@Query("Q")
39  		@Schema(d= {"a","b"}, t="string")
40  		public static class A1 {
41  			public A1(String x) { /* no-op */ }
42  		}
43  		@RestGet
44  		public void a(A1 q) { /* no-op */ }
45  
46  		@Query(
47  			name="Q",
48  			schema=@Schema(description="a\nb",type="string")
49  		)
50  		public static class A2 {
51  			public A2(String x) { /* no-op */ }
52  		}
53  		@RestPut
54  		public void b(A2 q) { /* no-op */ }
55  
56  		@Query(
57  			name="Q",
58  			schema=@Schema(description="b\nc",type="string")
59  		)
60  		@Schema(d={"a","b"}, t="string")
61  		public static class A3 {
62  			public A3(String x) { /* no-op */ }
63  		}
64  		@RestPost
65  		public void c(A3 q) { /* no-op */ }
66  
67  		@Query("Q")
68  		public static class A4 {}
69  		@RestDelete
70  		public void d(A4 q) { /* no-op */ }
71  	}
72  
73  	@Test void a01_fromPojo() {
74  		var s = getSwagger(A.class);
75  		var x = s.getParameterInfo("/a","get","query","Q");
76  
77  		assertBean(x, "name,description,type", "Q,a\nb,string");
78  
79  		x = s.getParameterInfo("/b","put","query","Q");
80  		assertBean(x, "name,description,type", "Q,a\nb,string");
81  
82  		x = s.getParameterInfo("/c","post","query","Q");
83  		assertBean(x, "name,description,type", "Q,b\nc,string");
84  
85  		x = s.getParameterInfo("/d","delete","query","Q");
86  		assertEquals("Q", x.getName());
87  	}
88  
89  	@Rest
90  	public static class B {
91  
92  		@Query(name="Q")
93  		public static class B1 {}
94  		@RestGet
95  		public void a(B1 q) { /* no-op */ }
96  
97  		@Query("Q")
98  		public static class B2 {
99  			public String f1;
100 		}
101 		@RestPut
102 		public void b(B2 q) { /* no-op */ }
103 
104 		@Query("Q")
105 		public static class B3 extends LinkedList<String> {
106 			private static final long serialVersionUID = 1L;
107 		}
108 		@RestPost
109 		public void c(B3 q) { /* no-op */ }
110 
111 		@Query("Q")
112 		public static class B4 {}
113 		@RestDelete
114 		public void d(B4 q) { /* no-op */ }
115 	}
116 
117 	@Test void b01_schemaFromPojo() {
118 		var s = getSwagger(B.class);
119 		var x = s.getParameterInfo("/a","get","query","Q");
120 
121 		assertJson("{'in':'query',name:'Q',type:'string'}", x);
122 
123 		x = s.getParameterInfo("/b","put","query","Q");
124 		assertJson("{'in':'query',name:'Q',schema:{properties:{f1:{type:'string'}}},type:'object'}", x);
125 
126 		x = s.getParameterInfo("/c","post","query","Q");
127 		assertJson("{'in':'query',items:{type:'string'},name:'Q',type:'array'}", x);
128 
129 		x = s.getParameterInfo("/d","delete","query","Q");
130 		assertJson("{'in':'query',name:'Q',type:'string'}", x);
131 	}
132 
133 	@Rest
134 	public static class D {
135 
136 		@RestGet
137 		public void a(
138 			@Query("Q")
139 			@Schema(d= {"a","b"}, t="string")
140 			String q
141 		) { /* no-op */ }
142 
143 		@RestPut
144 		public void b(
145 			@Query(
146 				name="Q",
147 				schema=@Schema(description="a\nb",type="string")
148 			)
149 			String q
150 		) { /* no-op */ }
151 
152 		@RestPost
153 		public void c(
154 			@Query(
155 				name="Q",
156 				schema=@Schema(description="b\nc",type="string")
157 			)
158 			@Schema(d= {"a","b"}, t="string")
159 			String q
160 		) { /* no-op */ }
161 
162 		@RestDelete
163 		public void d(@Query("Q") String q) {/* no-op */}
164 	}
165 
166 	@Test void d01_fromParameter() {
167 		var s = getSwagger(D.class);
168 		var x = s.getParameterInfo("/a","get","query","Q");
169 
170 		assertBean(x, "name,description,type", "Q,a\nb,string");
171 
172 		x = s.getParameterInfo("/b","put","query","Q");
173 		assertBean(x, "name,description,type", "Q,a\nb,string");
174 
175 		x = s.getParameterInfo("/c","post","query","Q");
176 		assertBean(x, "name,description,type", "Q,b\nc,string");
177 
178 		x = s.getParameterInfo("/d","delete","query","Q");
179 		assertEquals("Q", x.getName());
180 	}
181 
182 	//-----------------------------------------------------------------------------------------------------------------
183 	// Schema
184 	//-----------------------------------------------------------------------------------------------------------------
185 
186 	@Rest
187 	public static class E {
188 
189 		@RestGet
190 		public void a(@Query("Q") String q) { /* no-op */ }
191 	}
192 
193 	@Test void e01_schemaFromParameter() {
194 		var s = getSwagger(E.class);
195 
196 		var x = s.getParameterInfo("/a","get","query","Q");
197 		assertJson("{'in':'query',name:'Q',type:'string'}", x);
198 	}
199 }