diff options
author | Simon Willnauer <simonw@apache.org> | 2016-10-04 21:09:40 +0200 |
---|---|---|
committer | Simon Willnauer <simonw@apache.org> | 2016-10-04 21:09:40 +0200 |
commit | 80bf44f709409eca5082b91303cdabb553b42893 (patch) | |
tree | 988d5c1e4420e468c40c2475e84576babfa3f324 /core/src/test/java/org | |
parent | c3622271bf1f4196030a4697fef08c609bdfa95b (diff) | |
parent | 51d53791fe71c9cb62bc992588a57cdfb500383f (diff) |
Merge branch 'master' into dont_cache_scripts
Diffstat (limited to 'core/src/test/java/org')
3 files changed, 145 insertions, 6 deletions
diff --git a/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TransportTasksActionTests.java b/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TransportTasksActionTests.java index f964eeba9f..3952ab2046 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TransportTasksActionTests.java +++ b/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TransportTasksActionTests.java @@ -47,7 +47,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.rest.action.admin.cluster.RestListTasksAction; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskId; import org.elasticsearch.tasks.TaskInfo; @@ -66,7 +65,6 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; -import java.util.function.Consumer; import static org.elasticsearch.action.support.PlainActionFuture.newFuture; import static org.hamcrest.Matchers.containsString; @@ -536,7 +534,7 @@ public class TransportTasksActionTests extends TaskManagerTestCase { // Try to cancel main task using action name CancelTasksRequest request = new CancelTasksRequest(); - request.setNodesIds(testNodes[0].discoveryNode.getId()); + request.setNodes(testNodes[0].discoveryNode.getId()); request.setReason("Testing Cancellation"); request.setActions(actionName); CancelTasksResponse response = testNodes[randomIntBetween(0, testNodes.length - 1)].transportCancelTasksAction.execute(request) diff --git a/core/src/test/java/org/elasticsearch/common/network/NetworkModuleTests.java b/core/src/test/java/org/elasticsearch/common/network/NetworkModuleTests.java index 2daea85967..d86773d2d9 100644 --- a/core/src/test/java/org/elasticsearch/common/network/NetworkModuleTests.java +++ b/core/src/test/java/org/elasticsearch/common/network/NetworkModuleTests.java @@ -34,7 +34,6 @@ import org.elasticsearch.http.HttpStats; import org.elasticsearch.indices.breaker.CircuitBreakerService; import org.elasticsearch.plugins.NetworkPlugin; import org.elasticsearch.rest.BaseRestHandler; -import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.cat.AbstractCatAction; import org.elasticsearch.test.transport.AssertingLocalTransport; @@ -42,6 +41,7 @@ import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.Transport; import org.elasticsearch.transport.TransportInterceptor; +import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -89,7 +89,7 @@ public class NetworkModuleTests extends ModuleTestCase { super(null); } @Override - public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {} + public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { return channel -> {}; } } static class FakeCatRestHandler extends AbstractCatAction { @@ -97,7 +97,7 @@ public class NetworkModuleTests extends ModuleTestCase { super(null); } @Override - protected void doRequest(RestRequest request, RestChannel channel, NodeClient client) {} + protected RestChannelConsumer doCatRequest(RestRequest request, NodeClient client) { return channel -> {}; } @Override protected void documentation(StringBuilder sb) {} @Override diff --git a/core/src/test/java/org/elasticsearch/rest/BaseRestHandlerTests.java b/core/src/test/java/org/elasticsearch/rest/BaseRestHandlerTests.java new file mode 100644 index 0000000000..6019ab8aac --- /dev/null +++ b/core/src/test/java/org/elasticsearch/rest/BaseRestHandlerTests.java @@ -0,0 +1,141 @@ +/* + * 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.rest; + +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.Table; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.rest.action.cat.AbstractCatAction; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.rest.FakeRestChannel; +import org.elasticsearch.test.rest.FakeRestRequest; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.hamcrest.core.StringContains.containsString; +import static org.hamcrest.object.HasToString.hasToString; +import static org.mockito.Mockito.mock; + +public class BaseRestHandlerTests extends ESTestCase { + + public void testUnconsumedParameters() throws Exception { + final AtomicBoolean executed = new AtomicBoolean(); + BaseRestHandler handler = new BaseRestHandler(Settings.EMPTY) { + @Override + protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { + request.param("consumed"); + return channel -> executed.set(true); + } + }; + + final HashMap<String, String> params = new HashMap<>(); + params.put("consumed", randomAsciiOfLength(8)); + params.put("unconsumed", randomAsciiOfLength(8)); + RestRequest request = new FakeRestRequest.Builder().withParams(params).build(); + RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1); + final IllegalArgumentException e = + expectThrows(IllegalArgumentException.class, () -> handler.handleRequest(request, channel, mock(NodeClient.class))); + assertThat(e, hasToString(containsString("request [/] contains unused params: [unconsumed]"))); + assertFalse(executed.get()); + } + + public void testUnconsumedResponseParameters() throws Exception { + final AtomicBoolean executed = new AtomicBoolean(); + BaseRestHandler handler = new BaseRestHandler(Settings.EMPTY) { + @Override + protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { + request.param("consumed"); + return channel -> executed.set(true); + } + + @Override + protected Set<String> responseParams() { + return Collections.singleton("response_param"); + } + }; + + final HashMap<String, String> params = new HashMap<>(); + params.put("consumed", randomAsciiOfLength(8)); + params.put("response_param", randomAsciiOfLength(8)); + RestRequest request = new FakeRestRequest.Builder().withParams(params).build(); + RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1); + handler.handleRequest(request, channel, mock(NodeClient.class)); + assertTrue(executed.get()); + } + + public void testDefaultResponseParameters() throws Exception { + final AtomicBoolean executed = new AtomicBoolean(); + BaseRestHandler handler = new BaseRestHandler(Settings.EMPTY) { + @Override + protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { + return channel -> executed.set(true); + } + }; + + final HashMap<String, String> params = new HashMap<>(); + params.put("format", randomAsciiOfLength(8)); + params.put("filter_path", randomAsciiOfLength(8)); + params.put("pretty", randomAsciiOfLength(8)); + params.put("human", randomAsciiOfLength(8)); + RestRequest request = new FakeRestRequest.Builder().withParams(params).build(); + RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1); + handler.handleRequest(request, channel, mock(NodeClient.class)); + assertTrue(executed.get()); + } + + public void testCatResponseParameters() throws Exception { + final AtomicBoolean executed = new AtomicBoolean(); + AbstractCatAction handler = new AbstractCatAction(Settings.EMPTY) { + @Override + protected RestChannelConsumer doCatRequest(RestRequest request, NodeClient client) { + return channel -> executed.set(true); + } + + @Override + protected void documentation(StringBuilder sb) { + + } + + @Override + protected Table getTableWithHeader(RestRequest request) { + return null; + } + }; + + final HashMap<String, String> params = new HashMap<>(); + params.put("format", randomAsciiOfLength(8)); + params.put("h", randomAsciiOfLength(8)); + params.put("v", randomAsciiOfLength(8)); + params.put("ts", randomAsciiOfLength(8)); + params.put("pri", randomAsciiOfLength(8)); + params.put("bytes", randomAsciiOfLength(8)); + params.put("size", randomAsciiOfLength(8)); + params.put("time", randomAsciiOfLength(8)); + RestRequest request = new FakeRestRequest.Builder().withParams(params).build(); + RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1); + handler.handleRequest(request, channel, mock(NodeClient.class)); + assertTrue(executed.get()); + } + +} |