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 java.lang.String.*;
20  
21  import org.apache.juneau.*;
22  import org.apache.juneau.rest.*;
23  import org.apache.juneau.rest.mock.*;
24  import org.apache.juneau.serializer.*;
25  import org.apache.juneau.utest.utils.*;
26  import org.junit.jupiter.api.*;
27  
28  class Rest_RVars_Test extends TestBase {
29  
30  	//------------------------------------------------------------------------------------------------------------------
31  	// Basic tests
32  	//------------------------------------------------------------------------------------------------------------------
33  
34  	@Rest(
35  		path="/p1",
36  		defaultRequestAttributes={
37  			"A1: a1",
38  			"A2: a2",
39  			"foo: bar",
40  			"bar: baz",
41  			"R1a: $R{requestURI}",
42  			"R1b: $R{requestParentURI}",
43  			"R2: $R{foo}",
44  			"R3: $R{$R{foo}}",
45  			"R4: $R{A1}",
46  			"R5: $R{A2}",
47  			"R6: $R{C}",
48  		}
49  	)
50  	public static class A {
51  
52  		@RestGet(
53  			path="/p2",
54  			defaultRequestAttributes={"B1: b1", "B2:b"},
55  			serializers=A1.class
56  		)
57  		public void a(RestResponse res) {
58  			res.setAttribute("A2", "c");
59  			res.setAttribute("B2", "c");
60  			res.setAttribute("C", "c");
61  			res.setContent(null);
62  		}
63  
64  		public static class A1 extends FakeWriterSerializer {
65  			public A1(FakeWriterSerializer.Builder b) {
66  				super(b.produces("text/plain").accept("*/*").function((s,o) -> out(s)));
67  			}
68  			public static String out(SerializerSession s) {
69  				var sp = s.getSessionProperties();
70  				return format("A1=%s,A2=%s,B1=%s,B2=%s,C=%s,R1a=%s,R1b=%s,R2=%s,R3=%s,R4=%s,R5=%s,R6=%s",
71  					sp.get("A1",null), sp.get("A2",null), sp.get("B1",null), sp.get("B2",null), sp.get("C",null),
72  					sp.get("R1a",null), sp.get("R1b",null), sp.get("R2",null), sp.get("R3",null), sp.get("R4",null), sp.get("R5",null), sp.get("R6",null));
73  			}
74  		}
75  	}
76  
77  	@Test void a01_basic() throws Exception {
78  		var a = MockRestClient.build(A.class);
79  		a.get("/p2").accept("text/plain").run().assertContent("A1=a1,A2=c,B1=b1,B2=c,C=c,R1a=/p1/p2,R1b=/p1,R2=bar,R3=,R4=a1,R5=a2,R6=");
80  	}
81  }