Skip to content

Commit

Permalink
Ensure RestHandler.Wrapper delegates all implementations to the wrapp…
Browse files Browse the repository at this point in the history
…ed handler (opensearch-project#16154)

* Ensure RestHandler.Wrapper delegates all implementations to the wrapper handler

Signed-off-by: Craig Perkins <[email protected]>

* Add to CHANGELOG

Signed-off-by: Craig Perkins <[email protected]>

* Fix typo

Signed-off-by: Craig Perkins <[email protected]>

---------

Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks authored Oct 1, 2024
1 parent 908fefe commit 0b1650d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Add support for docker compose v2 in TestFixturesPlugin ([#16049](https://github.com/opensearch-project/OpenSearch/pull/16049))
- Remove identity-related feature flagged code from the RestController ([#15430](https://github.com/opensearch-project/OpenSearch/pull/15430))
- Remove Identity FeatureFlag ([#16024](https://github.com/opensearch-project/OpenSearch/pull/16024))
- Ensure RestHandler.Wrapper delegates all implementations to the wrapped handler ([#16154](https://github.com/opensearch-project/OpenSearch/pull/16154))


### Deprecated
Expand Down
10 changes: 10 additions & 0 deletions server/src/main/java/org/opensearch/rest/RestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ public List<ReplacedRoute> replacedRoutes() {
public boolean allowSystemIndexAccessByDefault() {
return delegate.allowSystemIndexAccessByDefault();
}

@Override
public boolean isActionPaginated() {
return delegate.isActionPaginated();
}

@Override
public boolean supportsStreaming() {
return delegate.supportsStreaming();
}
}

/**
Expand Down
41 changes: 41 additions & 0 deletions server/src/test/java/org/opensearch/rest/BaseRestHandlerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.opensearch.client.node.NodeClient;
import org.opensearch.common.Table;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.rest.RestHandler.ReplacedRoute;
import org.opensearch.rest.RestHandler.Route;
import org.opensearch.rest.RestRequest.Method;
Expand All @@ -46,15 +48,22 @@
import org.opensearch.threadpool.ThreadPool;

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import static org.hamcrest.core.StringContains.containsString;
import static org.hamcrest.object.HasToString.hasToString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

public class BaseRestHandlerTests extends OpenSearchTestCase {
private NodeClient mockClient;
Expand Down Expand Up @@ -288,4 +297,36 @@ public void testReplaceRoutesMethod() throws Exception {
}
}

public void testRestHandlerWrapper() throws Exception {
RestHandler rh = new RestHandler() {
@Override
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
}
};
RestHandler handlerSpy = spy(rh);
RestHandler.Wrapper rhWrapper = new RestHandler.Wrapper(handlerSpy);

List<java.lang.reflect.Method> overridableMethods = Arrays.stream(RestHandler.class.getMethods())
.filter(
m -> !(Modifier.isPrivate(m.getModifiers()) || Modifier.isStatic(m.getModifiers()) || Modifier.isFinal(m.getModifiers()))
)
.collect(Collectors.toList());

for (java.lang.reflect.Method method : overridableMethods) {
int argCount = method.getParameterCount();
Object[] args = new Object[argCount];
for (int i = 0; i < argCount; i++) {
args[i] = any();
}
if (args.length > 0) {
method.invoke(rhWrapper, args);
} else {
method.invoke(rhWrapper);
}
method.invoke(verify(handlerSpy, times(1)), args);
}
verifyNoMoreInteractions(handlerSpy);
}

}

0 comments on commit 0b1650d

Please sign in to comment.