Search in sources :

Example 1 with FakeRestRequest

use of org.opensearch.test.rest.FakeRestRequest in project OpenSearch by opensearch-project.

the class MultiSearchRequestTests method testDefaultIndicesOptions.

public void testDefaultIndicesOptions() throws IOException {
    final String requestContent = "{\"index\":\"test\", \"expand_wildcards\" : \"open,closed\"}}\r\n" + "{\"query\" : {\"match_all\" :{}}}\r\n";
    FakeRestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(requestContent), XContentType.JSON).withParams(Collections.singletonMap("ignore_unavailable", "true")).build();
    MultiSearchRequest request = RestMultiSearchAction.parseRequest(restRequest, null, true);
    assertThat(request.requests().size(), equalTo(1));
    assertThat(request.requests().get(0).indices()[0], equalTo("test"));
    assertThat(request.requests().get(0).indicesOptions(), equalTo(IndicesOptions.fromOptions(true, true, true, true, SearchRequest.DEFAULT_INDICES_OPTIONS)));
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest)

Example 2 with FakeRestRequest

use of org.opensearch.test.rest.FakeRestRequest in project OpenSearch by opensearch-project.

the class MultiSearchRequestTests method testFailWithUnknownKey.

public void testFailWithUnknownKey() {
    final String requestContent = "{\"index\":\"test\", \"ignore_unavailable\" : true, \"unknown_key\" : \"open,closed\"}}\r\n" + "{\"query\" : {\"match_all\" :{}}}\r\n";
    FakeRestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(requestContent), XContentType.JSON).build();
    IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> RestMultiSearchAction.parseRequest(restRequest, null, true));
    assertEquals("key [unknown_key] is not supported in the metadata section", ex.getMessage());
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest)

Example 3 with FakeRestRequest

use of org.opensearch.test.rest.FakeRestRequest in project OpenSearch by opensearch-project.

the class SettingsFilterTests method testFiltering.

private void testFiltering(Settings source, Settings filtered, String... patterns) throws IOException {
    SettingsFilter settingsFilter = new SettingsFilter(Arrays.asList(patterns));
    // Test using direct filtering
    Settings filteredSettings = settingsFilter.filter(source);
    assertThat(filteredSettings, equalTo(filtered));
    // 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 = Strings.toString(xContentBuilder);
    filteredSettings = Settings.builder().loadFromSource(filteredSettingsString, xContentBuilder.contentType()).build();
    assertThat(filteredSettings, equalTo(filtered));
}
Also used : RestRequest(org.opensearch.rest.RestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 4 with FakeRestRequest

use of org.opensearch.test.rest.FakeRestRequest in project OpenSearch by opensearch-project.

the class BytesRestResponseTests method testErrorTrace.

public void testErrorTrace() throws Exception {
    RestRequest request = new FakeRestRequest();
    request.params().put("error_trace", "true");
    RestChannel channel = new DetailedExceptionRestChannel(request);
    Exception t = new UnknownException("an error occurred reading data", new FileNotFoundException("/foo/bar"));
    BytesRestResponse response = new BytesRestResponse(channel, t);
    String text = response.content().utf8ToString();
    assertThat(text, containsString("\"type\":\"unknown_exception\",\"reason\":\"an error occurred reading data\""));
    assertThat(text, containsString("{\"type\":\"file_not_found_exception\""));
    assertThat(text, containsString("\"stack_trace\":\"[an error occurred reading data]"));
}
Also used : FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) FileNotFoundException(java.io.FileNotFoundException) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) OpenSearchException(org.opensearch.OpenSearchException) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) ParsingException(org.opensearch.common.ParsingException) RemoteTransportException(org.opensearch.transport.RemoteTransportException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException)

Example 5 with FakeRestRequest

use of org.opensearch.test.rest.FakeRestRequest in project OpenSearch by opensearch-project.

the class RestNodesActionTests method testCatNodesWithLocalDeprecationWarning.

public void testCatNodesWithLocalDeprecationWarning() {
    TestThreadPool threadPool = new TestThreadPool(RestNodesActionTests.class.getName());
    NodeClient client = new NodeClient(Settings.EMPTY, threadPool);
    FakeRestRequest request = new FakeRestRequest();
    request.params().put("local", randomFrom("", "true", "false"));
    action.doCatRequest(request, client);
    assertWarnings(RestNodesAction.LOCAL_DEPRECATED_MESSAGE);
    terminate(threadPool);
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) TestThreadPool(org.opensearch.threadpool.TestThreadPool) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest)

Aggregations

FakeRestRequest (org.opensearch.test.rest.FakeRestRequest)53 BytesArray (org.opensearch.common.bytes.BytesArray)15 Matchers.containsString (org.hamcrest.Matchers.containsString)14 IOException (java.io.IOException)12 NodeClient (org.opensearch.client.node.NodeClient)11 NoOpNodeClient (org.opensearch.test.client.NoOpNodeClient)9 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)8 OpenSearchException (org.opensearch.OpenSearchException)7 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)6 ParsingException (org.opensearch.common.ParsingException)6 FakeRestChannel (org.opensearch.test.rest.FakeRestChannel)6 FileNotFoundException (java.io.FileNotFoundException)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 OpenSearchStatusException (org.opensearch.OpenSearchStatusException)5 ResourceAlreadyExistsException (org.opensearch.ResourceAlreadyExistsException)5 ResourceNotFoundException (org.opensearch.ResourceNotFoundException)5 RestRequest (org.opensearch.rest.RestRequest)5 RemoteTransportException (org.opensearch.transport.RemoteTransportException)5 HashMap (java.util.HashMap)4 BytesRestResponse (org.opensearch.rest.BytesRestResponse)4