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