summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/common/network/NetworkModuleTests.java
blob: 0fb9b1a92fb9d9bc029a3424ef721906de71fbff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.common.network;

import org.elasticsearch.client.Client;
import org.elasticsearch.common.Table;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.ModuleTestCase;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.BoundTransportAddress;
import org.elasticsearch.http.HttpInfo;
import org.elasticsearch.http.HttpServerAdapter;
import org.elasticsearch.http.HttpServerTransport;
import org.elasticsearch.http.HttpStats;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.cat.AbstractCatAction;
import org.elasticsearch.rest.action.cat.RestNodesAction;
import org.elasticsearch.rest.action.main.RestMainAction;
import org.elasticsearch.test.transport.AssertingLocalTransport;
import org.elasticsearch.transport.Transport;
import org.elasticsearch.transport.TransportService;

public class NetworkModuleTests extends ModuleTestCase {

    static class FakeTransportService extends TransportService {
        public FakeTransportService() {
            super(null, null);
        }
    }

    static class FakeTransport extends AssertingLocalTransport {
        public FakeTransport() {
            super(null, null, null, null);
        }
    }

    static class FakeHttpTransport extends AbstractLifecycleComponent<HttpServerTransport> implements HttpServerTransport {
        public FakeHttpTransport() {
            super(null);
        }
        @Override
        protected void doStart() {}
        @Override
        protected void doStop() {}
        @Override
        protected void doClose() {}
        @Override
        public BoundTransportAddress boundAddress() {
            return null;
        }
        @Override
        public HttpInfo info() {
            return null;
        }
        @Override
        public HttpStats stats() {
            return null;
        }
        @Override
        public void httpServerAdapter(HttpServerAdapter httpServerAdapter) {}
    }

    static class FakeRestHandler extends BaseRestHandler {
        public FakeRestHandler() {
            super(null, null);
        }
        @Override
        protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception {}
    }

    static class FakeCatRestHandler extends AbstractCatAction {
        public FakeCatRestHandler() {
            super(null, null, null);
        }
        @Override
        protected void doRequest(RestRequest request, RestChannel channel, Client client) {}
        @Override
        protected void documentation(StringBuilder sb) {}
        @Override
        protected Table getTableWithHeader(RestRequest request) {
            return null;
        }
    }

    public void testRegisterTransportService() {
        Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_SERVICE_TYPE_KEY, "custom").build();
        NetworkModule module = new NetworkModule(new NetworkService(settings), settings, false, null);
        module.registerTransportService("custom", FakeTransportService.class);
        assertBinding(module, TransportService.class, FakeTransportService.class);

        // check it works with transport only as well
        module = new NetworkModule(new NetworkService(settings), settings, true, null);
        module.registerTransportService("custom", FakeTransportService.class);
        assertBinding(module, TransportService.class, FakeTransportService.class);
    }

    public void testRegisterTransport() {
        Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "custom").build();
        NetworkModule module = new NetworkModule(new NetworkService(settings), settings, false, null);
        module.registerTransport("custom", FakeTransport.class);
        assertBinding(module, Transport.class, FakeTransport.class);

        // check it works with transport only as well
        module = new NetworkModule(new NetworkService(settings), settings, true, null);
        module.registerTransport("custom", FakeTransport.class);
        assertBinding(module, Transport.class, FakeTransport.class);
    }

    public void testRegisterHttpTransport() {
        Settings settings = Settings.builder().put(NetworkModule.HTTP_TYPE_KEY, "custom").build();
        NetworkModule module = new NetworkModule(new NetworkService(settings), settings, false, null);
        module.registerHttpTransport("custom", FakeHttpTransport.class);
        assertBinding(module, HttpServerTransport.class, FakeHttpTransport.class);

        // check registration not allowed for transport only
        module = new NetworkModule(new NetworkService(settings), settings, true, null);
        try {
            module.registerHttpTransport("custom", FakeHttpTransport.class);
            fail();
        } catch (IllegalArgumentException e) {
            assertTrue(e.getMessage().contains("Cannot register http transport"));
            assertTrue(e.getMessage().contains("for transport client"));
        }

        // not added if http is disabled
        settings = Settings.builder().put(NetworkModule.HTTP_ENABLED.getKey(), false).build();
        module = new NetworkModule(new NetworkService(settings), settings, false, null);
        assertNotBound(module, HttpServerTransport.class);
    }

    public void testRegisterRestHandler() {
        Settings settings = Settings.EMPTY;
        NetworkModule module = new NetworkModule(new NetworkService(settings), settings, false, null);
        module.registerRestHandler(FakeRestHandler.class);
        // also check a builtin is bound
        assertSetMultiBinding(module, RestHandler.class, FakeRestHandler.class, RestMainAction.class);

        // check registration not allowed for transport only
        module = new NetworkModule(new NetworkService(settings), settings, true, null);
        try {
            module.registerRestHandler(FakeRestHandler.class);
            fail();
        } catch (IllegalArgumentException e) {
            assertTrue(e.getMessage().contains("Cannot register rest handler"));
            assertTrue(e.getMessage().contains("for transport client"));
        }
    }

    public void testRegisterCatRestHandler() {
        Settings settings = Settings.EMPTY;
        NetworkModule module = new NetworkModule(new NetworkService(settings), settings, false, null);
        module.registerRestHandler(FakeCatRestHandler.class);
        // also check a builtin is bound
        assertSetMultiBinding(module, AbstractCatAction.class, FakeCatRestHandler.class, RestNodesAction.class);
    }
}