use of org.opensearch.transport.TransportInterceptor in project OpenSearch by opensearch-project.
the class NetworkModuleTests method testRegisterInterceptor.
public void testRegisterInterceptor() {
Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "local").build();
AtomicInteger called = new AtomicInteger(0);
TransportInterceptor interceptor = new TransportInterceptor() {
@Override
public <T extends TransportRequest> TransportRequestHandler<T> interceptHandler(String action, String executor, boolean forceExecution, TransportRequestHandler<T> actualHandler) {
called.incrementAndGet();
if ("foo/bar/boom".equals(action)) {
assertTrue(forceExecution);
} else {
assertFalse(forceExecution);
}
return actualHandler;
}
};
NetworkModule module = newNetworkModule(settings, new NetworkPlugin() {
@Override
public List<TransportInterceptor> getTransportInterceptors(NamedWriteableRegistry namedWriteableRegistry, ThreadContext threadContext) {
assertNotNull(threadContext);
return Collections.singletonList(interceptor);
}
});
TransportInterceptor transportInterceptor = module.getTransportInterceptor();
assertEquals(0, called.get());
transportInterceptor.interceptHandler("foo/bar/boom", null, true, null);
assertEquals(1, called.get());
transportInterceptor.interceptHandler("foo/baz/boom", null, false, null);
assertEquals(2, called.get());
assertTrue(transportInterceptor instanceof NetworkModule.CompositeTransportInterceptor);
assertEquals(((NetworkModule.CompositeTransportInterceptor) transportInterceptor).transportInterceptors.size(), 1);
assertSame(((NetworkModule.CompositeTransportInterceptor) transportInterceptor).transportInterceptors.get(0), interceptor);
NullPointerException nullPointerException = expectThrows(NullPointerException.class, () -> {
newNetworkModule(settings, new NetworkPlugin() {
@Override
public List<TransportInterceptor> getTransportInterceptors(NamedWriteableRegistry namedWriteableRegistry, ThreadContext threadContext) {
assertNotNull(threadContext);
return Collections.singletonList(null);
}
});
});
assertEquals("interceptor must not be null", nullPointerException.getMessage());
}
Aggregations