Search in sources :

Example 1 with FakeRestChannel

use of org.elasticsearch.test.rest.FakeRestChannel in project elasticsearch by elastic.

the class BaseRestHandlerTests method testMultipleUnconsumedParameters.

public void testMultipleUnconsumedParameters() 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-first", randomAsciiOfLength(8));
    params.put("unconsumed-second", randomAsciiOfLength(8));
    RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).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 unrecognized parameters: [unconsumed-first], [unconsumed-second]")));
    assertFalse(executed.get());
}
Also used : FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) AbstractCatAction(org.elasticsearch.rest.action.cat.AbstractCatAction) Table(org.elasticsearch.common.Table) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Settings(org.elasticsearch.common.settings.Settings) NodeClient(org.elasticsearch.client.node.NodeClient) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.elasticsearch.client.node.NodeClient) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel)

Example 2 with FakeRestChannel

use of org.elasticsearch.test.rest.FakeRestChannel in project elasticsearch by elastic.

the class BaseRestHandlerTests method testCatResponseParameters.

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(xContentRegistry()).withParams(params).build();
    RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
    handler.handleRequest(request, channel, mock(NodeClient.class));
    assertTrue(executed.get());
}
Also used : FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) AbstractCatAction(org.elasticsearch.rest.action.cat.AbstractCatAction) Table(org.elasticsearch.common.Table) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Settings(org.elasticsearch.common.settings.Settings) NodeClient(org.elasticsearch.client.node.NodeClient) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AbstractCatAction(org.elasticsearch.rest.action.cat.AbstractCatAction) NodeClient(org.elasticsearch.client.node.NodeClient) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel)

Example 3 with FakeRestChannel

use of org.elasticsearch.test.rest.FakeRestChannel in project elasticsearch by elastic.

the class RestBuilderListenerTests method testXContentBuilderClosedInBuildResponse.

public void testXContentBuilderClosedInBuildResponse() throws Exception {
    AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>();
    RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {

        @Override
        public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
            builderAtomicReference.set(builder);
            builder.close();
            return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
        }
    };
    builderListener.buildResponse(Empty.INSTANCE);
    assertNotNull(builderAtomicReference.get());
    assertTrue(builderAtomicReference.get().generator().isClosed());
}
Also used : Empty(org.elasticsearch.transport.TransportResponse.Empty) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 4 with FakeRestChannel

use of org.elasticsearch.test.rest.FakeRestChannel in project elasticsearch by elastic.

the class RestBuilderListenerTests method testXContentBuilderNotClosedInBuildResponseAssertionsEnabled.

public void testXContentBuilderNotClosedInBuildResponseAssertionsEnabled() throws Exception {
    assumeTrue("tests are not being run with assertions", RestBuilderListener.class.desiredAssertionStatus());
    RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {

        @Override
        public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
            return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
        }
    };
    AssertionError error = expectThrows(AssertionError.class, () -> builderListener.buildResponse(Empty.INSTANCE));
    assertEquals("callers should ensure the XContentBuilder is closed themselves", error.getMessage());
}
Also used : Empty(org.elasticsearch.transport.TransportResponse.Empty) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 5 with FakeRestChannel

use of org.elasticsearch.test.rest.FakeRestChannel in project elasticsearch by elastic.

the class RestBuilderListenerTests method testXContentBuilderNotClosedInBuildResponseAssertionsDisabled.

public void testXContentBuilderNotClosedInBuildResponseAssertionsDisabled() throws Exception {
    AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>();
    RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {

        @Override
        public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
            builderAtomicReference.set(builder);
            return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
        }

        @Override
        boolean assertBuilderClosed(XContentBuilder xContentBuilder) {
            // don't check the actual builder being closed so we can test auto close
            return true;
        }
    };
    builderListener.buildResponse(Empty.INSTANCE);
    assertNotNull(builderAtomicReference.get());
    assertTrue(builderAtomicReference.get().generator().isClosed());
}
Also used : Empty(org.elasticsearch.transport.TransportResponse.Empty) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

FakeRestChannel (org.elasticsearch.test.rest.FakeRestChannel)9 FakeRestRequest (org.elasticsearch.test.rest.FakeRestRequest)9 IOException (java.io.IOException)6 Collections (java.util.Collections)6 HashMap (java.util.HashMap)6 Set (java.util.Set)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 NodeClient (org.elasticsearch.client.node.NodeClient)6 Table (org.elasticsearch.common.Table)6 Settings (org.elasticsearch.common.settings.Settings)6 AbstractCatAction (org.elasticsearch.rest.action.cat.AbstractCatAction)6 ESTestCase (org.elasticsearch.test.ESTestCase)6 StringContains.containsString (org.hamcrest.core.StringContains.containsString)6 HasToString.hasToString (org.hamcrest.object.HasToString.hasToString)6 Mockito.mock (org.mockito.Mockito.mock)6 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)3 Empty (org.elasticsearch.transport.TransportResponse.Empty)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2