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 org.apache.juneau.http.header.ContentType.*;
20  
21  import org.apache.juneau.*;
22  import org.apache.juneau.annotation.*;
23  import org.apache.juneau.http.annotation.*;
24  import org.apache.juneau.json.*;
25  import org.apache.juneau.parser.*;
26  import org.apache.juneau.rest.mock.*;
27  import org.apache.juneau.serializer.*;
28  import org.apache.juneau.swap.*;
29  import org.junit.jupiter.api.*;
30  
31  class BeanConfig_Swaps_Test extends TestBase {
32  
33  	//------------------------------------------------------------------------------------------------------------------
34  	// Basic tests
35  	//------------------------------------------------------------------------------------------------------------------
36  
37  	public static class A {
38  		public int f1;
39  	}
40  
41  	public static class SwapA1 extends StringSwap<A> {
42  		@Override /* ObjectSwap */
43  		public String swap(BeanSession session, A a) throws SerializeException {
44  			return "A1-" + a.f1;
45  		}
46  		@Override /* ObjectSwap */
47  		public A unswap(BeanSession session, String in, ClassMeta<?> hint) throws ParseException {
48  			if (! in.startsWith("A1"))
49  				throw new IllegalArgumentException("Invalid input for SwapA1!");
50  			var a = new A();
51  			a.f1 = Integer.parseInt(in.substring(3));
52  			return a;
53  		}
54  	}
55  
56  	public static class SwapA2 extends StringSwap<A> {
57  		@Override /* ObjectSwap */
58  		public String swap(BeanSession session, A a) throws SerializeException {
59  			return "A2-" + a.f1;
60  		}
61  		@Override /* ObjectSwap */
62  		public A unswap(BeanSession session, String in, ClassMeta<?> hint) throws ParseException {
63  			if (! in.startsWith("A2"))
64  				throw new IllegalArgumentException("Invalid input for SwapA2!");
65  			var a = new A();
66  			a.f1 = Integer.parseInt(in.substring(3));
67  			return a;
68  		}
69  	}
70  
71  	public static class SwapA3 extends StringSwap<A> {
72  		@Override /* ObjectSwap */
73  		public String swap(BeanSession session, A a) throws SerializeException {
74  			return "A3-" + a.f1;
75  		}
76  		@Override /* ObjectSwap */
77  		public A unswap(BeanSession session, String in, ClassMeta<?> hint) throws ParseException {
78  			if (! in.startsWith("A3"))
79  				throw new IllegalArgumentException("Invalid input for SwapA3!");
80  			var a = new A();
81  			a.f1 = Integer.parseInt(in.substring(3));
82  			return a;
83  		}
84  	}
85  
86  	@Rest(serializers=Json5Serializer.class, parsers=JsonParser.class)
87  	@BeanConfig(swaps={SwapA1.class})
88  	public static class A2 {}
89  
90  	@Rest
91  	@BeanConfig(swaps={SwapA2.class})
92  	public static class A1 extends A2 {
93  
94  		@RestGet
95  		public A a() {
96  			return new A(); // Should return "A2-1".
97  		}
98  		@RestPut
99  		public A b(@Content A a) {
100 			return a; // Should return "A2-1".
101 		}
102 		@RestPut(path="/c/{a}")
103 		public A c(@Path("a") A a) {
104 			return a; // Should return "A2-1".
105 		}
106 		@RestGet
107 		@BeanConfig(swaps={SwapA3.class})
108 		public A d() {
109 			return new A(); // Should return "A3-1".
110 		}
111 		@RestPut
112 		@BeanConfig(swaps={SwapA3.class})
113 		public A e(@Content A a) {
114 			return a; // Should return "A3-1".
115 		}
116 		@RestPut(path="/f/{a}")
117 		@BeanConfig(swaps={SwapA3.class})
118 		public A f(@Path("a") A a) {
119 			return a; // Should return "A3-1".
120 		}
121 	}
122 
123 	@Test void a01_swaps() throws Exception {
124 		var a = MockRestClient.build(A1.class);
125 		a.get("/a").json().run().assertContent("'A2-0'");
126 		a.put("/b", "'A2-1'", APPLICATION_JSON).run().assertContent("'A2-1'");
127 		a.put("/c/A2-2", null, APPLICATION_JSON).run().assertContent("'A2-2'");
128 		a.get("/d").json().run().assertContent("'A3-0'");
129 		a.put("/e", "'A3-1'", APPLICATION_JSON).run().assertContent("'A3-1'");
130 		a.put("/f/A3-2", null, APPLICATION_JSON).run().assertContent("'A3-2'");
131 	}
132 }