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;
18  
19  import org.apache.juneau.*;
20  import org.apache.juneau.bean.swagger.Swagger;
21  import org.apache.juneau.rest.annotation.*;
22  import org.apache.juneau.rest.mock.*;
23  import org.apache.juneau.serializer.*;
24  import org.apache.juneau.utest.utils.*;
25  import org.junit.jupiter.api.*;
26  
27  class Nls_Test extends TestBase {
28  
29  	//------------------------------------------------------------------------------------------------------------------
30  	// Test getting an NLS property defined on a class or method.
31  	//------------------------------------------------------------------------------------------------------------------
32  
33  	@Rest(
34  		serializers={A1.class},
35  		defaultRequestAttributes={"TestProperty:$L{key1}"},
36  		messages="NlsTest"
37  	)
38  	public static class A {
39  		@RestGet
40  		public String a() {
41  			return null;
42  		}
43  		@RestGet(
44  			defaultRequestAttributes={"TestProperty:$L{key2}"}
45  		)
46  		public String b() {
47  			return null;
48  		}
49  	}
50  	public static class A1 extends FakeWriterSerializer {
51  		public A1(FakeWriterSerializer.Builder builder) {
52  			super(builder.accept("*/*").function((s,o)->out(s)));
53  		}
54  
55  		public static String out(SerializerSession s) {
56  			return s.getSessionProperties().getString("TestProperty",null);
57  		}
58  	}
59  
60  	@Test void a01_basic() throws Exception {
61  		var a = MockRestClient.build(A.class);
62  		a.get("/a").run().assertContent("value1");
63  		a.get("/b").run().assertContent("value2");
64  	}
65  
66  	//------------------------------------------------------------------------------------------------------------------
67  	// Test OPTIONS pages without NLS
68  	//------------------------------------------------------------------------------------------------------------------
69  
70  	@Rest(title="test")
71  	public static class B {
72  		@RestOp(description="foo")
73  		public Swagger options(RestRequest req) {
74  			// Should get to the options page without errors
75  			return req.getSwagger().orElse(null);
76  		}
77  	}
78  
79  	@Test void b01_optionsPageWithoutNls() throws Exception {
80  		var b = MockRestClient.build(B.class);
81  		b.options("/").run().assertContent().isContains("foo");
82  	}
83  
84  	//------------------------------------------------------------------------------------------------------------------
85  	// Test Missing resource bundles.
86  	//------------------------------------------------------------------------------------------------------------------
87  
88  	@Rest
89  	public static class C {
90  		@RestGet
91  		public String a(RestRequest req) {
92  			// Missing resource bundle should cause {!!x} string.
93  			return req.getMessage("bad", 1, 2, 3);
94  		}
95  	}
96  
97  	@Test void c01_missingResourceBundle() throws Exception {
98  		var c = MockRestClient.build(C.class);
99  		c.get("/a").run().assertContent("{!bad}");
100 	}
101 }