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 org.apache.juneau.*;
20  import org.apache.juneau.collections.*;
21  import org.apache.juneau.rest.*;
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 Restx_Serializers_Test extends TestBase {
28  
29  	//------------------------------------------------------------------------------------------------------------------
30  	// Basic tests
31  	//------------------------------------------------------------------------------------------------------------------
32  
33  	public static class SA extends FakeWriterSerializer {
34  		public SA(FakeWriterSerializer.Builder b) {
35  			super(b.produces("text/a").function((s,o)->"text/a - "+o));
36  		}
37  	}
38  
39  	public static class SB extends FakeWriterSerializer {
40  		public SB(FakeWriterSerializer.Builder b) {
41  			super(b.produces("text/b").function((s,o)->"text/b - "+o));
42  		}
43  	}
44  
45  	public static class SC extends FakeWriterSerializer {
46  		public SC(FakeWriterSerializer.Builder b) {
47  			super(b.produces("text/a").function((s,o)->"text/c - "+o));
48  		}
49  	}
50  
51  	public static class SD extends FakeWriterSerializer {
52  		public SD(FakeWriterSerializer.Builder b) {
53  			super(b.produces("text/d").accept("text/a,text/d").function((s,o)->"text/d - "+o));
54  		}
55  	}
56  
57  	@Rest(serializers=SA.class)
58  	public static class A {
59  		@RestGet
60  		public String a() {
61  			return "test1";
62  		}
63  		@RestGet(serializers=SB.class)
64  		public String b() {
65  			return "test2";
66  		}
67  		@RestGet(serializers={SB.class,SC.class,SerializerSet.Inherit.class})
68  		public String c() {
69  			return "test3";
70  		}
71  		@RestGet(serializers={SD.class,SerializerSet.Inherit.class})
72  		public String d() {
73  			return "test4";
74  		}
75  		@RestGet
76  		public String e() {
77  			return "test406";
78  		}
79  	}
80  
81  	@Test void a01_basic() throws Exception {
82  		var a = MockRestClient.buildLax(A.class);
83  		a.get("/a")
84  			.accept("text/a")
85  			.run()
86  			.assertContent("text/a - test1");
87  		a.get("/a?noTrace=true")
88  			.accept("text/b")
89  			.run()
90  			.assertStatus(406)
91  			.assertContent().isContains(
92  				"Unsupported media-type in request header 'Accept': 'text/b'",
93  				"Supported media-types: ['text/a'"
94  			);
95  		a.get("/b?noTrace=true")
96  			.accept("text/a")
97  			.run()
98  			.assertStatus(406)
99  			.assertContent().isContains(
100 				"Unsupported media-type in request header 'Accept': 'text/a'",
101 				"Supported media-types: ['text/b']"
102 			);
103 		a.get("/c")
104 			.accept("text/a")
105 			.run()
106 			.assertContent("text/c - test3");
107 		a.get("/c")
108 			.accept("text/b")
109 			.run()
110 			.assertContent("text/b - test3");
111 		a.get("/d")
112 			.accept("text/a")
113 			.run()
114 			.assertContent("text/d - test4");
115 		a.get("/d")
116 			.accept("text/d")
117 			.run()
118 			.assertContent("text/d - test4");
119 		a.get("/e?noTrace=true")
120 			.accept("text/bad")
121 			.run()
122 			.assertStatus(406)
123 			.assertContent().isContains(
124 				"Unsupported media-type in request header 'Accept': 'text/bad'",
125 				"Supported media-types: ['text/a"
126 			);
127 	}
128 
129 	//------------------------------------------------------------------------------------------------------------------
130 	// Test serializer inheritance.
131 	//------------------------------------------------------------------------------------------------------------------
132 
133 	public static class DummySerializer extends FakeWriterSerializer {
134 		public DummySerializer(FakeWriterSerializer.Builder b, String produces) {
135 			super(b.produces(produces));
136 		}
137 	}
138 
139 	public static class S1 extends DummySerializer{ public S1(FakeWriterSerializer.Builder b) {super(b, "text/s1");} }
140 	public static class S2 extends DummySerializer{ public S2(FakeWriterSerializer.Builder b) {super(b, "text/s2");} }
141 	public static class S3 extends DummySerializer{ public S3(FakeWriterSerializer.Builder b) {super(b, "text/s3");} }
142 	public static class S4 extends DummySerializer{ public S4(FakeWriterSerializer.Builder b) {super(b, "text/s4");} }
143 	public static class S5 extends DummySerializer{ public S5(FakeWriterSerializer.Builder b) {super(b, "text/s5");} }
144 
145 	@Rest(serializers={S1.class,S2.class})
146 	public static class B {}
147 
148 	@Rest(serializers={S3.class,S4.class})
149 	public static class B1 extends B {}
150 
151 	@Rest
152 	public static class B2 extends B1 {
153 		@RestGet
154 		public JsonList a(RestResponse res) {
155 			// Should show ['text/s3','text/s4','text/s1','text/s2']
156 			return JsonList.of(res.getOpContext().getSupportedAcceptTypes());
157 		}
158 		@RestGet(serializers=S5.class)
159 		public JsonList b(RestResponse res) {
160 			// Should show ['text/s5']
161 			return JsonList.of(res.getOpContext().getSupportedAcceptTypes());
162 		}
163 		@RestGet(serializers={S5.class,SerializerSet.Inherit.class})
164 		public JsonList c(RestResponse res) {
165 			// Should show ['text/s5','text/s3','text/s4','text/s1','text/s2']
166 			return JsonList.of(res.getOpContext().getSupportedAcceptTypes());
167 		}
168 	}
169 
170 	@Test void b01_inheritence() throws Exception {
171 		var b = MockRestClient.build(B2.class);
172 		b.get("/a").run().assertContent("['text/s3','text/s4','text/s1','text/s2']");
173 		b.get("/b").run().assertContent("['text/s5']");
174 		b.get("/c").run().assertContent("['text/s5','text/s3','text/s4','text/s1','text/s2']");
175 	}
176 }