Search in sources :

Example 46 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class DanglingIndicesRestIT method createIndices.

/**
 * Helper that creates one or more indices, and importantly,
 * checks that they are green before proceeding. This is important
 * because the tests in this class stop and restart nodes, assuming
 * that each index has a primary or replica shard on every node, and if
 * a node is stopped prematurely, this assumption is broken.
 *
 * @return a mapping from each created index name to its UUID
 */
private Map<String, String> createIndices(String... indices) throws IOException {
    assert indices.length > 0;
    for (String index : indices) {
        String indexSettings = "{" + "  \"settings\": {" + "    \"index\": {" + "      \"number_of_shards\": 1," + "      \"number_of_replicas\": 2," + "      \"routing\": {" + "        \"allocation\": {" + "          \"total_shards_per_node\": 1" + "        }" + "      }" + "    }" + "  }" + "}";
        Request request = new Request("PUT", "/" + index);
        request.setJsonEntity(indexSettings);
        assertOK(getRestClient().performRequest(request));
    }
    ensureGreen(indices);
    final Response catResponse = getRestClient().performRequest(new Request("GET", "/_cat/indices?h=index,uuid"));
    assertOK(catResponse);
    final Map<String, String> createdIndexIDs = new HashMap<>();
    final List<String> indicesAsList = Arrays.asList(indices);
    for (String indexLine : Streams.readAllLines(catResponse.getEntity().getContent())) {
        String[] elements = indexLine.split(" +");
        if (indicesAsList.contains(elements[0])) {
            createdIndexIDs.put(elements[0], elements[1]);
        }
    }
    assertThat("Expected to find as many index UUIDs as created indices", createdIndexIDs.size(), equalTo(indices.length));
    return createdIndexIDs;
}
Also used : Response(org.opensearch.client.Response) HashMap(java.util.HashMap) Request(org.opensearch.client.Request)

Example 47 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class DanglingIndicesRestIT method listDanglingIndexIds.

private List<String> listDanglingIndexIds() throws IOException {
    final Response response = getRestClient().performRequest(new Request("GET", "/_dangling"));
    assertOK(response);
    final XContentTestUtils.JsonMapView mapView = createJsonMapView(response.getEntity().getContent());
    assertThat(mapView.get("_nodes.total"), equalTo(3));
    assertThat(mapView.get("_nodes.successful"), equalTo(3));
    assertThat(mapView.get("_nodes.failed"), equalTo(0));
    List<Object> indices = mapView.get("dangling_indices");
    List<String> danglingIndexIds = new ArrayList<>();
    for (int i = 0; i < indices.size(); i++) {
        danglingIndexIds.add(mapView.get("dangling_indices." + i + ".index_uuid"));
    }
    return danglingIndexIds;
}
Also used : Response(org.opensearch.client.Response) Request(org.opensearch.client.Request) ArrayList(java.util.ArrayList) XContentTestUtils(org.opensearch.test.XContentTestUtils)

Example 48 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class NoHandlerIT method runTestNoHandlerRespectsAcceptHeader.

private void runTestNoHandlerRespectsAcceptHeader(final String accept, final String contentType, final String expect) throws IOException {
    Request request = new Request("GET", "/foo/bar/baz/qux/quux");
    RequestOptions.Builder options = request.getOptions().toBuilder();
    options.addHeader("Accept", accept);
    request.setOptions(options);
    final ResponseException e = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request));
    final Response response = e.getResponse();
    assertThat(response.getHeader("Content-Type"), equalTo(contentType));
    assertThat(EntityUtils.toString(e.getResponse().getEntity()), containsString(expect));
    assertThat(response.getStatusLine().getStatusCode(), is(400));
}
Also used : Response(org.opensearch.client.Response) RequestOptions(org.opensearch.client.RequestOptions) ResponseException(org.opensearch.client.ResponseException) Request(org.opensearch.client.Request)

Example 49 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class RestHttpResponseHeadersIT method testUnsupportedMethodResponseHttpHeader.

/**
 * For requests to a valid REST endpoint using an unsupported HTTP method,
 * verify that a 405 HTTP response code is returned, and that the response
 * 'Allow' header includes a list of valid HTTP methods for the endpoint
 * (see
 * <a href="https://tools.ietf.org/html/rfc2616#section-10.4.6">HTTP/1.1 -
 * 10.4.6 - 405 Method Not Allowed</a>).
 */
public void testUnsupportedMethodResponseHttpHeader() throws Exception {
    try {
        client().performRequest(new Request("DELETE", "/_tasks"));
        fail("Request should have failed with 405 error");
    } catch (ResponseException e) {
        Response response = e.getResponse();
        assertThat(response.getStatusLine().getStatusCode(), is(405));
        assertThat(response.getHeader("Allow"), notNullValue());
        List<String> responseAllowHeaderStringArray = Arrays.asList(response.getHeader("Allow").split(","));
        assertThat(responseAllowHeaderStringArray, containsInAnyOrder("GET"));
        assertThat(EntityUtils.toString(response.getEntity()), containsString("Incorrect HTTP method for uri [/_tasks] and method [DELETE], allowed: [GET]"));
    }
}
Also used : Response(org.opensearch.client.Response) ResponseException(org.opensearch.client.ResponseException) Request(org.opensearch.client.Request) List(java.util.List)

Example 50 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class RestHttpResponseHeadersIT method testIndexSettingsPostRequest.

/**
 * Test if a POST request to /{index}/_settings matches the update settings
 * handler for /{index}/_settings, and returns a 405 error (see
 * <a href="https://github.com/elastic/elasticsearch/issues/17853">Issue
 * 17853</a> for more information).
 */
public void testIndexSettingsPostRequest() throws Exception {
    client().performRequest(new Request("PUT", "/testindex"));
    try {
        client().performRequest(new Request("POST", "/testindex/_settings"));
        fail("Request should have failed with 405 error");
    } catch (ResponseException e) {
        Response response = e.getResponse();
        assertThat(response.getStatusLine().getStatusCode(), is(405));
        assertThat(response.getHeader("Allow"), notNullValue());
        List<String> responseAllowHeaderStringArray = Arrays.asList(response.getHeader("Allow").split(","));
        assertThat(responseAllowHeaderStringArray, containsInAnyOrder("PUT", "GET"));
        assertThat(EntityUtils.toString(response.getEntity()), containsString("Incorrect HTTP method for uri [/testindex/_settings] and method [POST], allowed:"));
        assertThat(EntityUtils.toString(response.getEntity()), containsString("GET"));
        assertThat(EntityUtils.toString(response.getEntity()), containsString("PUT"));
    }
}
Also used : Response(org.opensearch.client.Response) ResponseException(org.opensearch.client.ResponseException) Request(org.opensearch.client.Request) List(java.util.List)

Aggregations

Response (org.opensearch.client.Response)134 Request (org.opensearch.client.Request)113 ResponseException (org.opensearch.client.ResponseException)24 Map (java.util.Map)23 Matchers.containsString (org.hamcrest.Matchers.containsString)20 ArrayList (java.util.ArrayList)17 PutRepositoryRequest (org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)16 RestClient (org.opensearch.client.RestClient)15 HashMap (java.util.HashMap)14 RequestOptions (org.opensearch.client.RequestOptions)12 ObjectPath (org.opensearch.test.rest.yaml.ObjectPath)12 IndexRequest (org.opensearch.action.index.IndexRequest)11 IOException (java.io.IOException)10 List (java.util.List)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 Version (org.opensearch.Version)8 WriteRequest (org.opensearch.action.support.WriteRequest)8 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)7 IndexResponse (org.opensearch.action.index.IndexResponse)7 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)7