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.junit.jupiter.api.Assertions.*;
20  
21  import java.util.*;
22  
23  import org.apache.juneau.*;
24  import org.apache.juneau.http.annotation.*;
25  import org.apache.juneau.json.*;
26  import org.apache.juneau.rest.mock.*;
27  import org.apache.juneau.testutils.pojos.*;
28  import org.junit.jupiter.api.*;
29  
30  class PathRemainder_Test extends TestBase {
31  
32  	//------------------------------------------------------------------------------------------------------------------
33  	// Simple tests
34  	//------------------------------------------------------------------------------------------------------------------
35  
36  	@Rest
37  	public static class A  {
38  		@RestOp(path="/a/*")
39  		public String a(@Path("/*") String remainder) {
40  			return ""+remainder;
41  		}
42  		@RestGet(path="/b/*")
43  		public String b(@Path("/*") String remainder) {
44  			return ""+remainder;
45  		}
46  		@RestPut(path="/c/*")
47  		public String c(@Path("/*") String remainder) {
48  			return ""+remainder;
49  		}
50  		@RestPost(path="/d/*")
51  		public String d(@Path("/*") String remainder) {
52  			return ""+remainder;
53  		}
54  		@RestDelete(path="/e/*")
55  		public String e(@Path("/*") String remainder) {
56  			return ""+remainder;
57  		}
58  	}
59  
60  	@Test void a01_basic() throws Exception {
61  		var a = MockRestClient.build(A.class);
62  
63  		a.get("/a").run().assertContent("null");
64  		a.get("/a/").run().assertContent("");
65  		a.get("/a/foo").run().assertContent("foo");
66  		a.get("/a/foo/bar").run().assertContent("foo/bar");
67  
68  		a.get("/b").run().assertContent("null");
69  		a.get("/b/").run().assertContent("");
70  		a.get("/b/foo").run().assertContent("foo");
71  		a.get("/b/foo/bar").run().assertContent("foo/bar");
72  
73  		a.put("/c").run().assertContent("null");
74  		a.put("/c/").run().assertContent("");
75  		a.put("/c/foo").run().assertContent("foo");
76  		a.put("/c/foo/bar").run().assertContent("foo/bar");
77  
78  		a.post("/d").run().assertContent("null");
79  		a.post("/d/").run().assertContent("");
80  		a.post("/d/foo").run().assertContent("foo");
81  		a.post("/d/foo/bar").run().assertContent("foo/bar");
82  
83  		a.delete("/e").run().assertContent("null");
84  		a.delete("/e/").run().assertContent("");
85  		a.delete("/e/foo").run().assertContent("foo");
86  		a.delete("/e/foo/bar").run().assertContent("foo/bar");
87  	}
88  
89  	//------------------------------------------------------------------------------------------------------------------
90  	// Optional path remainder parameter.
91  	//------------------------------------------------------------------------------------------------------------------
92  
93  	@Rest(serializers=Json5Serializer.class)
94  	public static class B {
95  		@RestGet(path="/a/*")
96  		public Object a(@Path("/*") Optional<Integer> f1) {
97  			assertNotNull(f1);
98  			return f1;
99  		}
100 		@RestPut(path="/b/*")
101 		public Object b(@Path("/*") Optional<ABean> f1) {
102 			assertNotNull(f1);
103 			return f1;
104 		}
105 		@RestPost(path="/c/*")
106 		public Object c(@Path("/*") Optional<List<ABean>> f1) {
107 			assertNotNull(f1);
108 			return f1;
109 		}
110 		@RestDelete(path="/d/*")
111 		public Object d(@Path("/*") List<Optional<ABean>> f1) {
112 			return f1;
113 		}
114 	}
115 
116 	@Test void b01_optionalParam() throws Exception {
117 		var b = MockRestClient.buildJson(B.class);
118 		b.get("/a/123")
119 			.run()
120 			.assertStatus(200)
121 			.assertContent("123");
122 		b.put("/b/a=1,b=foo")
123 			.run()
124 			.assertStatus(200)
125 			.assertContent("{a:1,b:'foo'}");
126 		b.post("/c/@((a=1,b=foo))")
127 			.run()
128 			.assertStatus(200)
129 			.assertContent("[{a:1,b:'foo'}]");
130 		b.delete("/d/@((a=1,b=foo))")
131 			.run()
132 			.assertStatus(200)
133 			.assertContent("[{a:1,b:'foo'}]");
134 	}
135 }