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 java.util.*;
20  
21  import org.apache.juneau.*;
22  import org.apache.juneau.collections.*;
23  import org.apache.juneau.cp.*;
24  import org.apache.juneau.http.annotation.*;
25  import org.apache.juneau.rest.*;
26  import org.apache.juneau.rest.mock.*;
27  import org.junit.jupiter.api.*;
28  
29  class Rest_Messages_Test extends TestBase {
30  
31  	//------------------------------------------------------------------------------------------------------------------
32  	// Basic tests
33  	//------------------------------------------------------------------------------------------------------------------
34  
35  	@Rest
36  	public static class A1 {
37  		@RestGet
38  		public JsonMap a(ResourceBundle rb) {
39  			return asMap(rb);
40  		}
41  		@RestGet
42  		public JsonMap b(Messages m) {
43  			return asMap(m);
44  		}
45  		@RestGet
46  		public String c(Messages m, @Query("name") String name) {
47  			return m.getString(name);
48  		}
49  	}
50  
51  	@Test void a01_default() throws Exception {
52  		var a1 = MockRestClient.build(A1.class);
53  		a1.get("/a").run().assertContent("{'A1.key2':'A1.value2a',key1:'value1a',key2:'A1.value2a'}");
54  		a1.get("/b").run().assertContent("{'A1.key2':'A1.value2a',key1:'value1a',key2:'A1.value2a'}");
55  		a1.get("/c?name=key1").run().assertContent("value1a");
56  		a1.get("/c?name=key2").run().assertContent("A1.value2a");
57  		a1.get("/c?name=key3").run().assertContent("{!key3}");
58  	}
59  
60  	//------------------------------------------------------------------------------------------------------------------
61  	// Overridden on subclass.
62  	//------------------------------------------------------------------------------------------------------------------
63  
64  	@Rest(messages="B1x")
65  	public static class B1 {
66  		@RestGet
67  		public JsonMap a(ResourceBundle rb) {
68  			return asMap(rb);
69  		}
70  		@RestGet
71  		public JsonMap b(Messages m) {
72  			return asMap(m);
73  		}
74  		@RestGet
75  		public String c(Messages m, @Query("name") String name) {
76  			return m.getString(name);
77  		}
78  	}
79  
80  	@Test void b01_customName() throws Exception {
81  		var b1 = MockRestClient.build(B1.class);
82  		b1.get("/a").run().assertContent("{'B1.key2':'B1.value2a',key1:'value1a',key2:'B1.value2a'}");
83  		b1.get("/b").run().assertContent("{'B1.key2':'B1.value2a',key1:'value1a',key2:'B1.value2a'}");
84  		b1.get("/c?name=key1").run().assertContent("value1a");
85  		b1.get("/c?name=key2").run().assertContent("B1.value2a");
86  		b1.get("/c?name=key3").run().assertContent("{!key3}");
87  	}
88  
89  	@Rest(messages="B2x")
90  	public static class B2 extends B1 {}
91  
92  	@Test void b02_subclassed_customName() throws Exception {
93  		var b2 = MockRestClient.build(B2.class);
94  		b2.get("/a").run().assertContent("{'B1.key2':'B1.value2a','B2.key3':'B2.value3b',key1:'value1a',key2:'value2b',key3:'B2.value3b'}");
95  		b2.get("/b").run().assertContent("{'B1.key2':'B1.value2a','B2.key3':'B2.value3b',key1:'value1a',key2:'value2b',key3:'B2.value3b'}");
96  		b2.get("/c?name=key1").run().assertContent("value1a");
97  		b2.get("/c?name=key2").run().assertContent("value2b");
98  		b2.get("/c?name=key3").run().assertContent("B2.value3b");
99  		b2.get("/c?name=key4").run().assertContent("{!key4}");
100 	}
101 
102 	public static class B3 extends B1 {
103 		@RestInit
104 		public void init(RestContext.Builder builder) {
105 			builder.messages().location(null, "B2x").location(B1.class, "B1x");
106 		}
107 	}
108 
109 	@Test void b03_viaBuilder() throws Exception {
110 		var b3 = MockRestClient.build(B3.class);
111 		b3.get("/a").run().assertContent("{'B1.key2':'B1.value2a','B2.key3':'B2.value3b',key1:'value1a',key2:'value2b'}");
112 		b3.get("/b").run().assertContent("{'B1.key2':'B1.value2a','B2.key3':'B2.value3b',key1:'value1a',key2:'value2b'}");
113 		b3.get("/c?name=key1").run().assertContent("value1a");
114 		b3.get("/c?name=key3").run().assertContent("{!key3}");
115 		b3.get("/c?name=key4").run().assertContent("{!key4}");
116 	}
117 
118 	//------------------------------------------------------------------------------------------------------------------
119 	// Helper methods.
120 	//------------------------------------------------------------------------------------------------------------------
121 
122 	private static JsonMap asMap(ResourceBundle rb) {
123 		var m = new JsonMap();
124 		for (String k : new TreeSet<>(rb.keySet()))
125 			m.put(k, rb.getString(k));
126 		return m;
127 	}
128 }