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;
}
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;
}
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));
}
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]"));
}
}
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"));
}
}
Aggregations