1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
35
36
37 public static class A {
38 public int f1;
39 }
40
41 public static class SwapA1 extends StringSwap<A> {
42 @Override
43 public String swap(BeanSession session, A a) throws SerializeException {
44 return "A1-" + a.f1;
45 }
46 @Override
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
58 public String swap(BeanSession session, A a) throws SerializeException {
59 return "A2-" + a.f1;
60 }
61 @Override
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
73 public String swap(BeanSession session, A a) throws SerializeException {
74 return "A3-" + a.f1;
75 }
76 @Override
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();
97 }
98 @RestPut
99 public A b(@Content A a) {
100 return a;
101 }
102 @RestPut(path="/c/{a}")
103 public A c(@Path("a") A a) {
104 return a;
105 }
106 @RestGet
107 @BeanConfig(swaps={SwapA3.class})
108 public A d() {
109 return new A();
110 }
111 @RestPut
112 @BeanConfig(swaps={SwapA3.class})
113 public A e(@Content A a) {
114 return a;
115 }
116 @RestPut(path="/f/{a}")
117 @BeanConfig(swaps={SwapA3.class})
118 public A f(@Path("a") A a) {
119 return a;
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 }