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 static org.junit.jupiter.api.Assertions.*;
20  
21  import org.apache.juneau.*;
22  import org.apache.juneau.annotation.Named;
23  import org.apache.juneau.cp.*;
24  import org.apache.juneau.rest.annotation.*;
25  import org.apache.juneau.rest.client.*;
26  import org.apache.juneau.rest.config.*;
27  import org.apache.juneau.rest.mock.*;
28  import org.junit.jupiter.api.*;
29  
30  class RestContext_Builder_Test extends TestBase {
31  
32  	//-----------------------------------------------------------------------------------------------------------------
33  	// beanStore
34  	//-----------------------------------------------------------------------------------------------------------------
35  
36  	public static class A {}
37  
38  	@Rest
39  	public static class A1 {
40  		@RestInject static BeanStore beanStore;
41  	}
42  
43  	@Test void a01_createBeanStore_default() {
44  		MockRestClient.buildLax(A1.class);
45  		assertEquals("BeanStore", A1.beanStore.getClass().getSimpleName());
46  	}
47  
48  	public static class MyBeanStore extends BeanStore {
49  		protected MyBeanStore(Builder builder) {
50  			super(builder.parent(BeanStore.create().build().addBean(A.class, new A())));
51  		}
52  	}
53  
54  	@Rest(beanStore=MyBeanStore.class)
55  	public static class A2 {
56  		@RestInject static BeanStore beanStore;
57  	}
58  
59  	@Test void a02_createBeanStore_annotation() {
60  		MockRestClient.buildLax(A2.class);
61  		assertNotNull(A2.beanStore.getBean(A.class));
62  	}
63  
64  	@Rest
65  	public static class A3 {
66  		@RestInject static BeanStore beanStore;
67  
68  		@RestInject BeanStore.Builder beanStore(BeanStore.Builder b) {
69  			return b.type(MyBeanStore.class);
70  		}
71  	}
72  
73  	@Test void a03_createBeanStore_restBean1() {
74  		MockRestClient.buildLax(A3.class);
75  		assertNotNull(A3.beanStore.getBean(A.class));
76  	}
77  
78  	@Rest
79  	public static class A4 {
80  		@RestInject static BeanStore beanStore;
81  
82  		@RestInject BeanStore beanStore() {
83  			return BeanStore.create().type(MyBeanStore.class).build();
84  		}
85  	}
86  
87  	@Test void a04_createBeanStore_restBean2() {
88  		MockRestClient.buildLax(A4.class);
89  		assertNotNull(A4.beanStore.getBean(A.class));
90  	}
91  
92  	//-----------------------------------------------------------------------------------------------------------------
93  	// @RestInject on fields.
94  	//-----------------------------------------------------------------------------------------------------------------
95  
96  	public static class B {
97  		public int id;
98  
99  		public B(int id) {
100 			this.id = id;
101 		}
102 	}
103 
104 	@Rest
105 	public static class B1a implements BasicJsonConfig {
106 		@RestInject static B b1 = new B(1);
107 		@RestInject(name="b2") B b2 = new B(2);
108 
109 		@RestInject static B b3;
110 		@RestInject(name="b2") B b4;
111 
112 		@RestGet("/a1") public B a1(B b) { return b; }
113 		@RestGet("/a2") public B a2(@Named("b2") B b) { return b; }
114 		@RestGet("/a3") public B a3() { return b3; }
115 		@RestGet("/a4") public B a4() { return b4; }
116 	}
117 
118 	@Rest
119 	public static class B1b extends B1a {
120 		@RestGet("/a5") public B a5(B b) { return b; }
121 		@RestGet("/a6") public B a6(@Named("b2") B b) { return b; }
122 		@RestGet("/a7") public B a7() { return b3; }
123 		@RestGet("/a8") public B a8() { return b4; }
124 	}
125 
126 	static RestClient b1b = MockRestClient.createLax(B1b.class).json5().build();
127 
128 	@Test void b01_RestBean_fields() throws Exception {
129 		b1b.get("/a1").run().assertContent("{id:1}");
130 		b1b.get("/a2").run().assertContent("{id:2}");
131 		b1b.get("/a3").run().assertContent("{id:1}");
132 		b1b.get("/a4").run().assertContent("{id:2}");
133 		b1b.get("/a5").run().assertContent("{id:1}");
134 		b1b.get("/a6").run().assertContent("{id:2}");
135 		b1b.get("/a7").run().assertContent("{id:1}");
136 		b1b.get("/a8").run().assertContent("{id:2}");
137 	}
138 }