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.client;
18  
19  import static org.apache.juneau.Context.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.io.*;
23  
24  import org.apache.juneau.*;
25  import org.apache.juneau.annotation.*;
26  import org.apache.juneau.reflect.*;
27  import org.apache.juneau.rest.annotation.*;
28  import org.apache.juneau.rest.mock.*;
29  import org.apache.juneau.rest.servlet.*;
30  import org.apache.juneau.swap.*;
31  import org.junit.jupiter.api.*;
32  
33  class RestClient_Config_Context_Test extends TestBase {
34  
35  	@Rest
36  	public static class A extends BasicRestObject {
37  		@RestPost
38  		public Reader echoBody(org.apache.juneau.rest.RestRequest req) throws IOException {
39  			return req.getContent().getReader();
40  		}
41  	}
42  
43  	public static class A2 {
44  		public String foo;
45  		@Override
46  		public String toString() {
47  			return foo;
48  		}
49  		public static A2 fromString(String s) {
50  			var p2 = new A2();
51  			p2.foo = s;
52  			return p2;
53  		}
54  	}
55  
56  	@Test void a02_addToStringObject() throws Exception {
57  		client().notBeanClasses(A2.class).build().post("/echoBody",A2.fromString("bar")).run().cacheContent().assertContent("'bar'").getContent().as(A2.class);
58  	}
59  
60  	public static class A3a {
61  		public int foo;
62  		static A3a get() {
63  			var x = new A3a();
64  			x.foo = 1;
65  			return x;
66  		}
67  	}
68  
69  	public static class A3b extends ObjectSwap<A3a,Integer> {
70  		@Override
71  		public Integer swap(BeanSession session, A3a o) { return o.foo; }
72  		@Override
73  		public A3a unswap(BeanSession session, Integer f, ClassMeta<?> hint) {return A3a.get(); }
74  	}
75  
76  	@Test void a03_appendToStringObject() throws Exception {
77  		var x = client().swaps(A3b.class).build().post("/echoBody",A3a.get()).run().cacheContent().assertContent("1").getContent().as(A3a.class);
78  		assertEquals(1,x.foo);
79  	}
80  
81  	@Test void a04_prependToStringObject() throws Exception {
82  		var x = client().swaps(A3b.class).build().post("/echoBody",A3a.get()).run().cacheContent().assertContent("1").getContent().as(A3a.class);
83  		assertEquals(1,x.foo);
84  	}
85  
86  	public static class A6a {
87  		public int foo,bar,baz;
88  		static A6a get() {
89  			var x = new A6a();
90  			x.foo = 1;
91  			x.bar = 2;
92  			x.baz = 3;
93  			return x;
94  		}
95  	}
96  
97  	@org.apache.juneau.annotation.Bean(sort=true,on="A6a")
98  	public static class A6b {}
99  
100 	@BeanConfig(sortProperties="true")
101 	public static class A6c {}
102 
103 	public static class A6d {
104 		@BeanConfig(sortProperties="true")
105 		public void foo() { /* no-op */ }
106 	}
107 
108 	@Test void a06_applyAnnotations() throws Exception {
109 		new A6b();
110 		new A6c();
111 		new A6d().foo();
112 		client().applyAnnotations(A6b.class).build().post("/echoBody",A6a.get()).run().cacheContent().assertContent("{bar:2,baz:3,foo:1}").getContent().as(A6a.class);
113 		client().applyAnnotations(A6c.class).build().post("/echoBody",A6a.get()).run().cacheContent().assertContent("{bar:2,baz:3,foo:1}").getContent().as(A6a.class);
114 		client().applyAnnotations(A6d.class.getMethod("foo")).build().post("/echoBody",A6a.get()).run().cacheContent().assertContent("{bar:2,baz:3,foo:1}").getContent().as(A6a.class);
115 		var al = AnnotationWorkList.of(ClassInfo.of(A6c.class).getAnnotationList(CONTEXT_APPLY_FILTER));
116 		client().apply(al).build().post("/echoBody",A6a.get()).run().cacheContent().assertContent("{bar:2,baz:3,foo:1}").getContent().as(A6a.class);
117 	}
118 
119 	@Test void a09_annotations() throws Exception {
120 		client().annotations(BeanAnnotation.create(A6a.class).sort(true).build()).build().post("/echoBody",A6a.get()).run().cacheContent().assertContent("{bar:2,baz:3,foo:1}").getContent().as(A6a.class);
121 	}
122 
123 	//------------------------------------------------------------------------------------------------------------------
124 	// Helper methods.
125 	//------------------------------------------------------------------------------------------------------------------
126 
127 	private static RestClient.Builder client() {
128 		return MockRestClient.create(A.class).json5();
129 	}
130 }