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.junit.jupiter.api.*;
24  
25  class RestPostInit_Test extends TestBase {
26  
27  	//------------------------------------------------------------------------------------------------------------------
28  	// @RestPostInit
29  	//------------------------------------------------------------------------------------------------------------------
30  	@Rest(children={A_Super.class,A_Sub.class})
31  	public static class A {}
32  
33  	@Rest(path="/super")
34  	public static class A_Super {
35  		protected JsonList events = new JsonList();
36  		@RestPostInit
37  		public void postInit1c(RestContext context) {
38  			events.add("super-1c");
39  		}
40  		@RestPostInit
41  		public void postInit1a(RestContext context) {
42  			events.add("super-1a");
43  		}
44  		@RestPostInit
45  		public void postInit1b() {
46  			events.add("super-1b");
47  		}
48  		@RestPostInit
49  		public void postInit2a() {
50  			events.add("super-2a");
51  		}
52  		@RestGet
53  		public JsonList getEvents() {
54  			return events;
55  		}
56  	}
57  
58  	@Rest(path="/sub",children={A_Child.class})
59  	public static class A_Sub extends A_Super {
60  		protected static String LAST_CALLED;
61  		@Override
62  		@RestPostInit
63  		public void postInit1c(RestContext context) {
64  			events.add("sub-1c");
65  		}
66  		@Override
67  		@RestPostInit
68  		public void postInit1a(RestContext context) {
69  			events.add("sub-1a");
70  		}
71  		@Override
72  		@RestPostInit
73  		public void postInit1b() {
74  			events.add("sub-1b");
75  		}
76  		@RestPostInit
77  		public void postInit2b() {
78  			events.add("sub-2b");
79  		}
80  		@RestPostInit
81  		public void postInitOrderTestSub() {
82  			LAST_CALLED = "PARENT";
83  		}
84  		@RestGet
85  		public String getLastCalled() {
86  			return LAST_CALLED;
87  		}
88  	}
89  
90  	@Rest(path="/child")
91  	public static class A_Child extends A_Super {
92  		@Override
93  		@RestPostInit
94  		public void postInit1c(RestContext context) {
95  			events.add("child-1c");
96  		}
97  		@RestPostInit
98  		public void postInit2b() {
99  			events.add("child-2b");
100 		}
101 		@RestPostInit
102 		public void postInitOrderTestSub() {
103 			A_Sub.LAST_CALLED = "CHILD";
104 		}
105 	}
106 
107 	@Test void a01_postInit() throws Exception {
108 		var a = MockRestClient.build(A.class);
109 		a.get("/super/events").run().assertContent("['super-1a','super-1b','super-1c','super-2a']");
110 		a.get("/sub/events").run().assertContent("['sub-1a','sub-1b','sub-1c','super-2a','sub-2b']");
111 		a.get("/sub/child/events").run().assertContent("['super-1a','super-1b','child-1c','super-2a','child-2b']");
112 		a.get("/sub/lastCalled").run().assertContent("CHILD");
113 	}
114 
115 	//------------------------------------------------------------------------------------------------------------------
116 	// @RestPostInit(childFirst=true)
117 	//------------------------------------------------------------------------------------------------------------------
118 
119 	@Rest(
120 		children={
121 			B_Super.class,
122 			B_Sub.class
123 		}
124 	)
125 	public static class B {}
126 
127 	@Rest(path="/super")
128 	public static class B_Super {
129 		protected JsonList events = new JsonList();
130 		@RestPostInit(childFirst=true)
131 		public void postInitChildFirst1c(RestContext context) {
132 			events.add("super-1c");
133 		}
134 		@RestPostInit(childFirst=true)
135 		public void postInitChildFirst1a(RestContext context) {
136 			events.add("super-1a");
137 		}
138 		@RestPostInit(childFirst=true)
139 		public void postInitChildFirst1b() {
140 			events.add("super-1b");
141 		}
142 		@RestPostInit(childFirst=true)
143 		public void postInitChildFirst2a() {
144 			events.add("super-2a");
145 		}
146 		@RestGet
147 		public JsonList getPostInitChildFirstEvents() {
148 			return events;
149 		}
150 	}
151 
152 	@Rest(path="/sub", children={B_Child.class})
153 	public static class B_Sub extends B_Super {
154 		protected static String LAST_CALLED;
155 		@Override
156 		@RestPostInit(childFirst=true)
157 		public void postInitChildFirst1c(RestContext context) {
158 			events.add("sub-1c");
159 		}
160 		@Override
161 		@RestPostInit(childFirst=true)
162 		public void postInitChildFirst1a(RestContext context) {
163 			events.add("sub-1a");
164 		}
165 		@Override
166 		@RestPostInit(childFirst=true)
167 		public void postInitChildFirst1b() {
168 			events.add("sub-1b");
169 		}
170 		@RestPostInit(childFirst=true)
171 		public void postInitChildFirst2b() {
172 			events.add("sub-2b");
173 		}
174 		@RestPostInit(childFirst=true)
175 		public void postInitChildFirstOrderTestSub() {
176 			LAST_CALLED = "PARENT";
177 		}
178 		@RestGet
179 		public String getLastCalled() {
180 			return LAST_CALLED;
181 		}
182 	}
183 
184 	@Rest(path="/child")
185 	public static class B_Child extends B_Super {
186 		@Override
187 		@RestPostInit(childFirst=true)
188 		public void postInitChildFirst1c(RestContext context) {
189 			events.add("child-1c");
190 		}
191 		@RestPostInit(childFirst=true)
192 		public void postInitChildFirst2b() {
193 			events.add("child-2b");
194 		}
195 		@RestPostInit(childFirst=true)
196 		public void postInitChildFirstOrderTestSub() {
197 			B_Sub.LAST_CALLED = "CHILD";
198 		}
199 	}
200 
201 	@Test void b01_postInitChildFirst() throws Exception {
202 		var b = MockRestClient.build(B.class);
203 		b.get("/super/postInitChildFirstEvents").run().assertContent("['super-1a','super-1b','super-1c','super-2a']");
204 		b.get("/sub/postInitChildFirstEvents").run().assertContent("['sub-1a','sub-1b','sub-1c','super-2a','sub-2b']");
205 		b.get("/sub/child/postInitChildFirstEvents").run().assertContent("['super-1a','super-1b','child-1c','super-2a','child-2b']");
206 		b.get("/sub/lastCalled").run().assertContent("PARENT");
207 	}
208 }