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.http.HttpMethod.*;
20  
21  import org.apache.juneau.*;
22  import org.apache.juneau.http.header.*;
23  import org.apache.juneau.rest.mock.*;
24  import org.junit.jupiter.api.*;
25  
26  class RestOp_ClientVersion_Test extends TestBase {
27  
28  	//------------------------------------------------------------------------------------------------------------------
29  	// Basic tests - default Client-Version header.
30  	//------------------------------------------------------------------------------------------------------------------
31  
32  	@Rest
33  	public static class A1 {
34  		@RestOp(method=GET, path="/")
35  		public String a() {
36  			return "no-version";
37  		}
38  		@RestOp(method=GET, path="/", clientVersion="[0.0,1.0)")
39  		public String b() {
40  			return "[0.0,1.0)";
41  		}
42  		@RestOp(method=GET, path="/", clientVersion="[1.0,1.0]")
43  		public String c() {
44  			return "[1.0,1.0]";
45  		}
46  		@RestOp(method=GET, path="/", clientVersion="[1.1,2)")
47  		public String d() {
48  			return "[1.1,2)";
49  		}
50  		@RestOp(method=GET, path="/", clientVersion="2")
51  		public String e() {
52  			return "2";
53  		}
54  	}
55  
56  	@Test void a01_defaultHeader() throws Exception {
57  		var a = MockRestClient.build(A1.class);
58  		a.get("/").run().assertContent("no-version");
59  		for (String s : "1, 1.0, 1.0.0, 1.0.1".split("\\s*,\\s*")) {
60  			a.get("/").header(ClientVersion.of(s)).run().assertContent().setMsg("s=[{0}]",s).is("[1.0,1.0]");
61  		}
62  		for (String s : "1.1, 1.1.1, 1.2, 1.9.9".split("\\s*,\\s*")) {
63  			a.get("/").header(ClientVersion.of(s)).run().assertContent().setMsg("s=[{0}]").is("[1.1,2)");
64  		}
65  		for (String s : "2, 2.0, 2.1, 9, 9.9".split("\\s*,\\s*")) {
66  			a.get("/").header(ClientVersion.of(s)).run().assertContent().setMsg("s=[{0}]").is("2");
67  		}
68  	}
69  
70  	@Rest
71  	public static class A2 {
72  		@RestGet(path="/")
73  		public String a() {
74  			return "no-version";
75  		}
76  		@RestGet(path="/", clientVersion="[0.0,1.0)")
77  		public String b() {
78  			return "[0.0,1.0)";
79  		}
80  		@RestGet(path="/", clientVersion="[1.0,1.0]")
81  		public String c() {
82  			return "[1.0,1.0]";
83  		}
84  		@RestGet(path="/", clientVersion="[1.1,2)")
85  		public String d() {
86  			return "[1.1,2)";
87  		}
88  		@RestGet(path="/", clientVersion="2")
89  		public String e() {
90  			return "2";
91  		}
92  	}
93  
94  	@Test void a02_defaultHeader() throws Exception {
95  		var a = MockRestClient.build(A2.class);
96  		a.get("/").run().assertContent("no-version");
97  		for (String s : "1, 1.0, 1.0.0, 1.0.1".split("\\s*,\\s*")) {
98  			a.get("/").header(ClientVersion.of(s)).run().assertContent().setMsg("s=[{0}]",s).is("[1.0,1.0]");
99  		}
100 		for (String s : "1.1, 1.1.1, 1.2, 1.9.9".split("\\s*,\\s*")) {
101 			a.get("/").header(ClientVersion.of(s)).run().assertContent().setMsg("s=[{0}]").is("[1.1,2)");
102 		}
103 		for (String s : "2, 2.0, 2.1, 9, 9.9".split("\\s*,\\s*")) {
104 			a.get("/").header(ClientVersion.of(s)).run().assertContent().setMsg("s=[{0}]").is("2");
105 		}
106 	}
107 
108 	//------------------------------------------------------------------------------------------------------------------
109 	// Basic tests - Custom-Client-Version header.
110 	//------------------------------------------------------------------------------------------------------------------
111 
112 	@Rest(clientVersionHeader="Custom-Client-Version")
113 	public static class B1 {
114 		@RestOp(method=GET, path="/")
115 		public String a() {
116 			return "no-version";
117 		}
118 		@RestOp(method=GET, path="/", clientVersion="[0.0,1.0)")
119 		public String b() {
120 			return "[0.0,1.0)";
121 		}
122 		@RestOp(method=GET, path="/", clientVersion="[1.0,1.0]")
123 		public String c() {
124 			return "[1.0,1.0]";
125 		}
126 		@RestOp(method=GET, path="/", clientVersion="[1.1,2)")
127 		public String d() {
128 			return "[1.1,2)";
129 		}
130 		@RestOp(method=GET, path="/", clientVersion="2")
131 		public String e() {
132 			return "2";
133 		}
134 	}
135 
136 	@Test void b01_testCustomHeader() throws Exception {
137 		var b = MockRestClient.build(B1.class);
138 		b.get("/").run().assertContent("no-version");
139 		for (String s : "0, 0.0, 0.1, .1, .9, .99".split("\\s*,\\s*")) {
140 			b.get("/").header("Custom-Client-Version", s).run().assertContent("[0.0,1.0)");
141 		}
142 		for (String s : "1, 1.0, 1.0.0, 1.0.1".split("\\s*,\\s*")) {
143 			b.get("/").header("Custom-Client-Version", s).run().assertContent("[1.0,1.0]");
144 		}
145 		for (String s : "1.1, 1.1.1, 1.2, 1.9.9".split("\\s*,\\s*")) {
146 			b.get("/").header("Custom-Client-Version", s).run().assertContent("[1.1,2)");
147 		}
148 		for (String s : "2, 2.0, 2.1, 9, 9.9".split("\\s*,\\s*")) {
149 			b.get("/").header("Custom-Client-Version", s).run().assertContent("2");
150 		}
151 	}
152 
153 	@Rest(clientVersionHeader="Custom-Client-Version")
154 	public static class B2 {
155 		@RestGet(path="/")
156 		public String a() {
157 			return "no-version";
158 		}
159 		@RestGet(path="/", clientVersion="[0.0,1.0)")
160 		public String b() {
161 			return "[0.0,1.0)";
162 		}
163 		@RestGet(path="/", clientVersion="[1.0,1.0]")
164 		public String c() {
165 			return "[1.0,1.0]";
166 		}
167 		@RestGet(path="/", clientVersion="[1.1,2)")
168 		public String d() {
169 			return "[1.1,2)";
170 		}
171 		@RestGet(path="/", clientVersion="2")
172 		public String e() {
173 			return "2";
174 		}
175 	}
176 
177 	@Test void b02_testCustomHeader() throws Exception {
178 		var b = MockRestClient.build(B2.class);
179 		b.get("/").run().assertContent("no-version");
180 		for (String s : "0, 0.0, 0.1, .1, .9, .99".split("\\s*,\\s*")) {
181 			b.get("/").header("Custom-Client-Version", s).run().assertContent("[0.0,1.0)");
182 		}
183 		for (String s : "1, 1.0, 1.0.0, 1.0.1".split("\\s*,\\s*")) {
184 			b.get("/").header("Custom-Client-Version", s).run().assertContent("[1.0,1.0]");
185 		}
186 		for (String s : "1.1, 1.1.1, 1.2, 1.9.9".split("\\s*,\\s*")) {
187 			b.get("/").header("Custom-Client-Version", s).run().assertContent("[1.1,2)");
188 		}
189 		for (String s : "2, 2.0, 2.1, 9, 9.9".split("\\s*,\\s*")) {
190 			b.get("/").header("Custom-Client-Version", s).run().assertContent("2");
191 		}
192 	}
193 }