|
35 | 35 | import org.opensearch.client.node.NodeClient; |
36 | 36 | import org.opensearch.common.Table; |
37 | 37 | import org.opensearch.common.settings.Settings; |
| 38 | +import org.opensearch.core.common.bytes.BytesArray; |
| 39 | +import org.opensearch.core.rest.RestStatus; |
38 | 40 | import org.opensearch.rest.RestHandler.ReplacedRoute; |
39 | 41 | import org.opensearch.rest.RestHandler.Route; |
40 | 42 | import org.opensearch.rest.RestRequest.Method; |
|
46 | 48 | import org.opensearch.threadpool.ThreadPool; |
47 | 49 |
|
48 | 50 | import java.io.IOException; |
| 51 | +import java.lang.reflect.Modifier; |
49 | 52 | import java.util.Arrays; |
50 | 53 | import java.util.Collections; |
51 | 54 | import java.util.HashMap; |
52 | 55 | import java.util.List; |
53 | 56 | import java.util.Set; |
54 | 57 | import java.util.concurrent.atomic.AtomicBoolean; |
| 58 | +import java.util.stream.Collectors; |
55 | 59 |
|
56 | 60 | import static org.hamcrest.core.StringContains.containsString; |
57 | 61 | import static org.hamcrest.object.HasToString.hasToString; |
| 62 | +import static org.mockito.ArgumentMatchers.any; |
| 63 | +import static org.mockito.Mockito.spy; |
| 64 | +import static org.mockito.Mockito.times; |
| 65 | +import static org.mockito.Mockito.verify; |
| 66 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
58 | 67 |
|
59 | 68 | public class BaseRestHandlerTests extends OpenSearchTestCase { |
60 | 69 | private NodeClient mockClient; |
@@ -288,4 +297,36 @@ public void testReplaceRoutesMethod() throws Exception { |
288 | 297 | } |
289 | 298 | } |
290 | 299 |
|
| 300 | + public void testRestHandlerWrapper() throws Exception { |
| 301 | + RestHandler rh = new RestHandler() { |
| 302 | + @Override |
| 303 | + public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception { |
| 304 | + new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY); |
| 305 | + } |
| 306 | + }; |
| 307 | + RestHandler handlerSpy = spy(rh); |
| 308 | + RestHandler.Wrapper rhWrapper = new RestHandler.Wrapper(handlerSpy); |
| 309 | + |
| 310 | + List<java.lang.reflect.Method> overridableMethods = Arrays.stream(RestHandler.class.getMethods()) |
| 311 | + .filter( |
| 312 | + m -> !(Modifier.isPrivate(m.getModifiers()) || Modifier.isStatic(m.getModifiers()) || Modifier.isFinal(m.getModifiers())) |
| 313 | + ) |
| 314 | + .collect(Collectors.toList()); |
| 315 | + |
| 316 | + for (java.lang.reflect.Method method : overridableMethods) { |
| 317 | + int argCount = method.getParameterCount(); |
| 318 | + Object[] args = new Object[argCount]; |
| 319 | + for (int i = 0; i < argCount; i++) { |
| 320 | + args[i] = any(); |
| 321 | + } |
| 322 | + if (args.length > 0) { |
| 323 | + method.invoke(rhWrapper, args); |
| 324 | + } else { |
| 325 | + method.invoke(rhWrapper); |
| 326 | + } |
| 327 | + method.invoke(verify(handlerSpy, times(1)), args); |
| 328 | + } |
| 329 | + verifyNoMoreInteractions(handlerSpy); |
| 330 | + } |
| 331 | + |
291 | 332 | } |
0 commit comments