Search in sources :

Example 21 with FakeRestRequest

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

the class RestControllerTests method testDispatchFailsWithPlainText.

public void testDispatchFailsWithPlainText() {
    String content = randomAsciiOfLengthBetween(1, BREAKER_LIMIT.bytesAsInt());
    FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(new BytesArray(content), null).withPath("/foo").withHeaders(Collections.singletonMap("Content-Type", Collections.singletonList("text/plain"))).build();
    AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.NOT_ACCEPTABLE);
    restController.registerHandler(RestRequest.Method.GET, "/foo", new RestHandler() {

        @Override
        public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
            channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
        }
    });
    assertFalse(channel.getSendResponseCalled());
    restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY));
    assertTrue(channel.getSendResponseCalled());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) NodeClient(org.elasticsearch.client.node.NodeClient) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Example 22 with FakeRestRequest

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

the class RestControllerTests method testUnknownContentWithContentStream.

public void testUnknownContentWithContentStream() {
    FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(new BytesArray("aaaabbbbb"), null).withPath("/foo").withHeaders(Collections.singletonMap("Content-Type", Collections.singletonList("foo/bar"))).build();
    AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.NOT_ACCEPTABLE);
    restController.registerHandler(RestRequest.Method.GET, "/foo", new RestHandler() {

        @Override
        public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
            channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
        }

        @Override
        public boolean supportsContentStream() {
            return true;
        }
    });
    assertFalse(channel.getSendResponseCalled());
    restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY));
    assertTrue(channel.getSendResponseCalled());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) NodeClient(org.elasticsearch.client.node.NodeClient) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException)

Example 23 with FakeRestRequest

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

the class RestIndicesActionTests method testBuildTable.

public void testBuildTable() {
    final Settings settings = Settings.EMPTY;
    final RestController restController = new RestController(settings, Collections.emptySet(), null, null, null);
    final RestIndicesAction action = new RestIndicesAction(settings, restController, new IndexNameExpressionResolver(settings));
    // build a (semi-)random table
    final int numIndices = randomIntBetween(0, 5);
    Index[] indices = new Index[numIndices];
    for (int i = 0; i < numIndices; i++) {
        indices[i] = new Index(randomAsciiOfLength(5), UUIDs.randomBase64UUID());
    }
    final MetaData.Builder metaDataBuilder = MetaData.builder();
    for (final Index index : indices) {
        metaDataBuilder.put(IndexMetaData.builder(index.getName()).settings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_INDEX_UUID, index.getUUID())).creationDate(System.currentTimeMillis()).numberOfShards(1).numberOfReplicas(1).state(IndexMetaData.State.OPEN));
    }
    final MetaData metaData = metaDataBuilder.build();
    final ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).build();
    final String[] indicesStr = new String[indices.length];
    for (int i = 0; i < indices.length; i++) {
        indicesStr[i] = indices[i].getName();
    }
    final ClusterHealthResponse clusterHealth = new ClusterHealthResponse(clusterState.getClusterName().value(), indicesStr, clusterState, 0, 0, 0, TimeValue.timeValueMillis(1000L));
    final Table table = action.buildTable(new FakeRestRequest(), indices, clusterHealth, randomIndicesStatsResponse(indices), metaData);
    // now, verify the table is correct
    int count = 0;
    List<Table.Cell> headers = table.getHeaders();
    assertThat(headers.get(count++).value, equalTo("health"));
    assertThat(headers.get(count++).value, equalTo("status"));
    assertThat(headers.get(count++).value, equalTo("index"));
    assertThat(headers.get(count++).value, equalTo("uuid"));
    List<List<Table.Cell>> rows = table.getRows();
    assertThat(rows.size(), equalTo(indices.length));
    // TODO: more to verify (e.g. randomize cluster health, num primaries, num replicas, etc)
    for (int i = 0; i < rows.size(); i++) {
        count = 0;
        final List<Table.Cell> row = rows.get(i);
        // all are red because cluster state doesn't have routing entries
        assertThat(row.get(count++).value, equalTo("red*"));
        // all are OPEN for now
        assertThat(row.get(count++).value, equalTo("open"));
        assertThat(row.get(count++).value, equalTo(indices[i].getName()));
        assertThat(row.get(count++).value, equalTo(indices[i].getUUID()));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) Table(org.elasticsearch.common.Table) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) RestController(org.elasticsearch.rest.RestController) Index(org.elasticsearch.index.Index) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Settings(org.elasticsearch.common.settings.Settings)

Example 24 with FakeRestRequest

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

the class RestControllerTests method testDispatchWorksWithNewlineDelimitedJson.

public void testDispatchWorksWithNewlineDelimitedJson() {
    final String mimeType = "application/x-ndjson";
    String content = randomAsciiOfLengthBetween(1, BREAKER_LIMIT.bytesAsInt());
    FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(new BytesArray(content), null).withPath("/foo").withHeaders(Collections.singletonMap("Content-Type", Collections.singletonList(mimeType))).build();
    AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.OK);
    restController.registerHandler(RestRequest.Method.GET, "/foo", new RestHandler() {

        @Override
        public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
            channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
        }

        @Override
        public boolean supportsContentStream() {
            return true;
        }
    });
    assertFalse(channel.getSendResponseCalled());
    restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY));
    assertTrue(channel.getSendResponseCalled());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) NodeClient(org.elasticsearch.client.node.NodeClient) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Example 25 with FakeRestRequest

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

the class RestControllerTests method testDispatchBadRequestUnknownCause.

public void testDispatchBadRequestUnknownCause() {
    final FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build();
    final AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.BAD_REQUEST);
    restController.dispatchBadRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY), null);
    assertTrue(channel.getSendResponseCalled());
    assertThat(channel.getRestResponse().content().utf8ToString(), containsString("unknown cause"));
}
Also used : ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Aggregations

FakeRestRequest (org.elasticsearch.test.rest.FakeRestRequest)30 IOException (java.io.IOException)11 Matchers.containsString (org.hamcrest.Matchers.containsString)11 ThreadContext (org.elasticsearch.common.util.concurrent.ThreadContext)10 NodeClient (org.elasticsearch.client.node.NodeClient)7 BytesArray (org.elasticsearch.common.bytes.BytesArray)7 ElasticsearchException (org.elasticsearch.ElasticsearchException)6 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)6 ParsingException (org.elasticsearch.common.ParsingException)6 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)6 RemoteTransportException (org.elasticsearch.transport.RemoteTransportException)6 FileNotFoundException (java.io.FileNotFoundException)5 ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)5 ResourceAlreadyExistsException (org.elasticsearch.ResourceAlreadyExistsException)5 ResourceNotFoundException (org.elasticsearch.ResourceNotFoundException)5 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)4 RestRequest (org.elasticsearch.rest.RestRequest)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Settings (org.elasticsearch.common.settings.Settings)3 Table (org.elasticsearch.common.Table)2