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