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