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.common.utils.Utils.*;
20  
21  import org.apache.juneau.*;
22  import org.apache.juneau.rest.matcher.*;
23  import org.apache.juneau.rest.mock.*;
24  import org.junit.jupiter.api.*;
25  
26  import jakarta.servlet.http.*;
27  
28  class RestOp_Matchers_Test extends TestBase {
29  
30  	//------------------------------------------------------------------------------------------------------------------
31  	// Overlapping matchers
32  	//------------------------------------------------------------------------------------------------------------------
33  
34  	@Rest
35  	public static class A {
36  		@RestOp(path="/one", matchers=A1.class)
37  		public String a() {
38  			return "OK-1a";
39  		}
40  		@RestGet(path="/one", matchers=A2.class)
41  		public String b() {
42  			return "OK-1b";
43  		}
44  		@RestGet(path="/one")
45  		public String c() {
46  			return "OK-1c";
47  		}
48  		@RestOp(path="/two")
49  		public String d() {
50  			return "OK-2a";
51  		}
52  		@RestGet(path="/two", matchers={A1.class, A2.class})
53  		public String e() {
54  			return "OK-2b";
55  		}
56  
57  		public static class A1 extends RestMatcher {
58  			@Override /* RestMatcher */
59  			public boolean matches(HttpServletRequest req) {
60  				return emptyIfNull(req.getQueryString()).contains("t1=1");
61  			}
62  		}
63  		public static class A2 extends RestMatcher {
64  			@Override /* RestMatcher */
65  			public boolean matches(HttpServletRequest req) {
66  				return emptyIfNull(req.getQueryString()).contains("t2=2");
67  			}
68  		}
69  	}
70  
71  	@Test void a01_overlapping() throws Exception {
72  		var a = MockRestClient.build(A.class);
73  		a.get("/one?t1=1").run().assertContent("OK-1a");
74  		a.get("/one?t2=2").run().assertContent("OK-1b");
75  		a.get("/one").run().assertContent("OK-1c");
76  		a.get("/two?t1=1").run().assertContent("OK-2b");
77  		a.get("/two?t2=2").run().assertContent("OK-2b");
78  		a.get("/two?t1=1&t2=2").run().assertContent("OK-2b");
79  		a.get("/two?tx=x").run().assertContent("OK-2a");
80  	}
81  }