Search in sources :

Example 1 with TransportInterceptor

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());
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) NetworkPlugin(org.opensearch.plugins.NetworkPlugin) TransportInterceptor(org.opensearch.transport.TransportInterceptor) TransportRequest(org.opensearch.transport.TransportRequest) TransportRequestHandler(org.opensearch.transport.TransportRequestHandler) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Aggregations

List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)1 ClusterSettings (org.opensearch.common.settings.ClusterSettings)1 Settings (org.opensearch.common.settings.Settings)1 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)1 NetworkPlugin (org.opensearch.plugins.NetworkPlugin)1 TransportInterceptor (org.opensearch.transport.TransportInterceptor)1 TransportRequest (org.opensearch.transport.TransportRequest)1 TransportRequestHandler (org.opensearch.transport.TransportRequestHandler)1