Search in sources :

Example 11 with FakeRestRequest

use of org.elasticsearch.test.rest.FakeRestRequest 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 12 with FakeRestRequest

use of org.elasticsearch.test.rest.FakeRestRequest 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 13 with FakeRestRequest

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

the class SettingsFilterTests method testFiltering.

private void testFiltering(Settings source, Settings filtered, String... patterns) throws IOException {
    SettingsFilter settingsFilter = new SettingsFilter(Settings.EMPTY, Arrays.asList(patterns));
    // Test using direct filtering
    Settings filteredSettings = settingsFilter.filter(source);
    assertThat(filteredSettings.getAsMap().entrySet(), equalTo(filtered.getAsMap().entrySet()));
    // Test using toXContent filtering
    RestRequest request = new FakeRestRequest();
    settingsFilter.addFilterSettingParams(request);
    XContentBuilder xContentBuilder = XContentBuilder.builder(JsonXContent.jsonXContent);
    xContentBuilder.startObject();
    source.toXContent(xContentBuilder, request);
    xContentBuilder.endObject();
    String filteredSettingsString = xContentBuilder.string();
    filteredSettings = Settings.builder().loadFromSource(filteredSettingsString, xContentBuilder.contentType()).build();
    assertThat(filteredSettings.getAsMap().entrySet(), equalTo(filtered.getAsMap().entrySet()));
}
Also used : RestRequest(org.elasticsearch.rest.RestRequest) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 14 with FakeRestRequest

use of org.elasticsearch.test.rest.FakeRestRequest 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)

Example 15 with FakeRestRequest

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

the class RestMainActionTests method testHeadResponse.

public void testHeadResponse() throws Exception {
    final String nodeName = "node1";
    final ClusterName clusterName = new ClusterName("cluster1");
    final String clusterUUID = randomAsciiOfLengthBetween(10, 20);
    final boolean available = randomBoolean();
    final RestStatus expectedStatus = available ? RestStatus.OK : RestStatus.SERVICE_UNAVAILABLE;
    final Version version = Version.CURRENT;
    final Build build = Build.CURRENT;
    final MainResponse mainResponse = new MainResponse(nodeName, version, clusterName, clusterUUID, build, available);
    XContentBuilder builder = JsonXContent.contentBuilder();
    RestRequest restRequest = new FakeRestRequest() {

        @Override
        public Method method() {
            return Method.HEAD;
        }
    };
    BytesRestResponse response = RestMainAction.convertMainResponse(mainResponse, restRequest, builder);
    assertNotNull(response);
    assertEquals(expectedStatus, response.status());
// the empty responses are handled in the HTTP layer so we do
// not assert on them here
}
Also used : FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) RestRequest(org.elasticsearch.rest.RestRequest) RestStatus(org.elasticsearch.rest.RestStatus) Version(org.elasticsearch.Version) MainResponse(org.elasticsearch.action.main.MainResponse) Build(org.elasticsearch.Build) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ClusterName(org.elasticsearch.cluster.ClusterName) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

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