From 9e25962f9145b092a888a685dcd10bda2175cb4b Mon Sep 17 00:00:00 2001 From: olcbean Date: Tue, 28 Nov 2017 15:19:29 +0100 Subject: [PATCH 1/4] Add open index api to the high level rest client --- .../elasticsearch/client/IndicesClient.java | 24 ++++++++ .../org/elasticsearch/client/Request.java | 14 +++++ .../client/RestHighLevelClient.java | 24 ++++++++ .../elasticsearch/client/IndicesClientIT.java | 25 ++++++++- .../elasticsearch/client/RequestTests.java | 25 +++++++++ .../admin/indices/open/OpenIndexResponse.java | 34 +++++++++++- .../indices/open/OpenIndexResponseTests.java | 55 +++++++++++++++++++ 7 files changed, 198 insertions(+), 3 deletions(-) create mode 100644 core/src/test/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponseTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java index 2cc1d4849d5a8..bd0b4217a4886 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java @@ -23,6 +23,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; +import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; +import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; import java.io.IOException; import java.util.Collections; @@ -60,4 +62,26 @@ public void deleteIndexAsync(DeleteIndexRequest deleteIndexRequest, ActionListen restHighLevelClient.performRequestAsyncAndParseEntity(deleteIndexRequest, Request::deleteIndex, DeleteIndexResponse::fromXContent, listener, Collections.emptySet(), headers); } + + /** + * Opens an index using the Open Index API + *

+ * See + * Open Index API on elastic.co + */ + public OpenIndexResponse openIndex(OpenIndexRequest openIndexRequest, Header... headers) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent, + Collections.emptySet(), headers); + } + + /** + * Asynchronously opens an index using the Open Index API + *

+ * See + * Open Index API on elastic.co + */ + public void openIndexAsync(OpenIndexRequest openIndexRequest, ActionListener listener, Header... headers) { + restHighLevelClient.performRequestAsyncAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent, + listener, Collections.emptySet(), headers); + } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java index e2a6dcac20b06..3e639aa967fe3 100755 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java @@ -30,6 +30,7 @@ import org.apache.lucene.util.BytesRef; import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.get.GetRequest; @@ -135,6 +136,19 @@ static Request deleteIndex(DeleteIndexRequest deleteIndexRequest) { return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null); } + static Request openIndex(OpenIndexRequest openIndexRequest) { + String endpoint = endpoint(openIndexRequest.indices(), Strings.EMPTY_ARRAY, "_open"); + + Params parameters = Params.builder(); + + parameters.withTimeout(openIndexRequest.timeout()); + parameters.withMasterTimeout(openIndexRequest.masterNodeTimeout()); + parameters.withWaitForActiveShards(openIndexRequest.waitForActiveShards()); + parameters.withIndicesOptions(openIndexRequest.indicesOptions()); + + return new Request(HttpPost.METHOD_NAME, endpoint, parameters.getParams(), null); + } + static Request info() { return new Request(HttpGet.METHOD_NAME, "/", Collections.emptyMap(), null); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java index 2ebaf2cf342ff..cf91fb083c055 100755 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java @@ -26,6 +26,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; +import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.delete.DeleteRequest; @@ -359,6 +361,28 @@ public final void deleteAsync(DeleteRequest deleteRequest, ActionListener + * Open Index API on elastic.co + */ + public final OpenIndexResponse openIndex(OpenIndexRequest openIndexRequest, Header... headers) throws IOException { + return performRequestAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent, + Collections.singleton(404), headers); + } + + /** + * Asynchronously opens an index using the Open Index API + * + * See + * Open Index API on elastic.co + */ + public final void openIndexAsync(OpenIndexRequest openIndexRequest, ActionListener listener, Header... headers) { + performRequestAsyncAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent, listener, + Collections.singleton(404), headers); + } + /** * Executes a search using the Search API * diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 4045e565288e5..99205a75a7bea 100755 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -22,6 +22,8 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; +import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; +import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; import org.elasticsearch.rest.RestStatus; import java.io.IOException; @@ -54,15 +56,34 @@ public void testDeleteIndex() throws IOException { } } + public void testOpenExistingIndex() throws IOException { + String indexName = "test_index"; + createIndex(indexName); + + OpenIndexRequest openIndexRequest = new OpenIndexRequest(indexName); + OpenIndexResponse openIndexResponse = execute(openIndexRequest, highLevelClient().indices()::openIndex, + highLevelClient().indices()::openIndexAsync); + assertTrue(openIndexResponse.isAcknowledged()); + } + + public void testOpenNonExistentIndex() throws IOException { + String nonExistentIndex = "non_existent_index"; + assertFalse(indexExists(nonExistentIndex)); + + OpenIndexRequest openIndexRequest = new OpenIndexRequest(nonExistentIndex); + + ElasticsearchException exception = expectThrows(ElasticsearchException.class, + () -> execute(openIndexRequest, highLevelClient().indices()::openIndex, highLevelClient().indices()::openIndexAsync)); + assertEquals(RestStatus.NOT_FOUND, exception.status()); + } + private static void createIndex(String index) throws IOException { Response response = client().performRequest("PUT", index); - assertEquals(200, response.getStatusLine().getStatusCode()); } private static boolean indexExists(String index) throws IOException { Response response = client().performRequest("HEAD", index); - return response.getStatusLine().getStatusCode() == 200; } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java index 3be250d513d21..0c3c5f15b5ecc 100755 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java @@ -26,6 +26,7 @@ import org.apache.http.util.EntityUtils; import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkShardRequest; import org.elasticsearch.action.delete.DeleteRequest; @@ -83,6 +84,8 @@ import static java.util.Collections.singletonMap; import static org.elasticsearch.client.Request.enforceSameContentType; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.Matchers.nullValue; public class RequestTests extends ESTestCase { @@ -269,6 +272,28 @@ public void testDeleteIndex() throws IOException { assertNull(request.getEntity()); } + public void testOpenIndex() throws IOException { + OpenIndexRequest openIndexRequest = new OpenIndexRequest(); + int numIndices = randomIntBetween(1, 5); + String[] indices = new String[numIndices]; + for (int i = 0; i < numIndices; i++) { + indices[i] = "index-" + randomAlphaOfLengthBetween(2, 5); + } + openIndexRequest.indices(indices); + + Map expectedParams = new HashMap<>(); + setRandomTimeout(openIndexRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); + setRandomMasterTimeout(openIndexRequest, expectedParams); + setRandomIndicesOptions(openIndexRequest::indicesOptions, openIndexRequest::indicesOptions, expectedParams); + + Request request = Request.openIndex(openIndexRequest); + StringJoiner endpoint = new StringJoiner("/", "/", "").add(String.join(",", indices)).add("_open"); + assertThat(endpoint.toString(), equalTo(request.getEndpoint())); + assertThat(expectedParams, equalTo(request.getParameters())); + assertThat(request.getMethod(), equalTo("POST")); + assertThat(request.getEntity(), nullValue()); + } + public void testIndex() throws IOException { String index = randomAlphaOfLengthBetween(3, 10); String type = randomAlphaOfLengthBetween(3, 10); diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponse.java b/core/src/main/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponse.java index fe9343f363ff3..95fef9fc65344 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponse.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponse.java @@ -21,15 +21,34 @@ import org.elasticsearch.Version; import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; + /** * A response for a open index action. */ -public class OpenIndexResponse extends AcknowledgedResponse { +public class OpenIndexResponse extends AcknowledgedResponse implements ToXContentObject { + private static final String SHARDS_ACKNOWLEDGED = "shards_acknowledged"; + private static final ParseField SHARDS_ACKNOWLEDGED_PARSER = new ParseField(SHARDS_ACKNOWLEDGED); + + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("open_index", true, + args -> new OpenIndexResponse((boolean) args[0], (boolean) args[1])); + + static { + declareAcknowledgedField(PARSER); + PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), SHARDS_ACKNOWLEDGED_PARSER, + ObjectParser.ValueType.BOOLEAN); + } private boolean shardsAcknowledged; @@ -68,4 +87,17 @@ public void writeTo(StreamOutput out) throws IOException { out.writeBoolean(shardsAcknowledged); } } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + addAcknowledgedField(builder); + builder.field(SHARDS_ACKNOWLEDGED, isShardsAcknowledged()); + builder.endObject(); + return builder; + } + + public static OpenIndexResponse fromXContent(XContentParser parser) throws IOException { + return PARSER.apply(parser, null); + } } diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponseTests.java b/core/src/test/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponseTests.java new file mode 100644 index 0000000000000..fc8457a3f858f --- /dev/null +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponseTests.java @@ -0,0 +1,55 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.action.admin.indices.open; + +import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.test.ESTestCase; + +import java.io.IOException; + +import static org.hamcrest.CoreMatchers.equalTo; + +public class OpenIndexResponseTests extends ESTestCase { + + public void testFromToXContent() throws IOException { + final OpenIndexResponse openIndexResponse = createTestItem(); + + boolean humanReadable = randomBoolean(); + final XContentType xContentType = randomFrom(XContentType.values()); + BytesReference originalBytes = toShuffledXContent(openIndexResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); + + OpenIndexResponse parsedCreateIndexResponse; + try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { + parsedCreateIndexResponse = OpenIndexResponse.fromXContent(parser); + assertNull(parser.nextToken()); + } + assertThat(parsedCreateIndexResponse.isShardsAcknowledged(), equalTo(openIndexResponse.isShardsAcknowledged())); + assertThat(parsedCreateIndexResponse.isAcknowledged(), equalTo(openIndexResponse.isAcknowledged())); + } + + private static OpenIndexResponse createTestItem() { + boolean acknowledged = randomBoolean(); + boolean shardsAcked = acknowledged && randomBoolean(); + return new OpenIndexResponse(acknowledged, shardsAcked); + } +} From cbfaf3410e8d7c23fb4db051a3fc8879c993d15d Mon Sep 17 00:00:00 2001 From: olcbean Date: Sun, 3 Dec 2017 20:31:39 +0100 Subject: [PATCH 2/4] Addressing reviewers remarks --- .../client/RestHighLevelClient.java | 7 ++- .../elasticsearch/client/IndicesClientIT.java | 50 +++++++++++++++---- .../elasticsearch/client/RequestTests.java | 6 +++ .../indices/open/OpenIndexResponseTests.java | 18 +++++-- 4 files changed, 63 insertions(+), 18 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java index cf91fb083c055..da0aecdafb366 100755 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java @@ -368,8 +368,7 @@ public final void deleteAsync(DeleteRequest deleteRequest, ActionListener */ public final OpenIndexResponse openIndex(OpenIndexRequest openIndexRequest, Header... headers) throws IOException { - return performRequestAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent, - Collections.singleton(404), headers); + return performRequestAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent, emptySet(), headers); } /** @@ -379,8 +378,8 @@ public final OpenIndexResponse openIndex(OpenIndexRequest openIndexRequest, Head * Open Index API on elastic.co */ public final void openIndexAsync(OpenIndexRequest openIndexRequest, ActionListener listener, Header... headers) { - performRequestAsyncAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent, listener, - Collections.singleton(404), headers); + performRequestAsyncAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent, listener, emptySet(), + headers); } /** diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 99205a75a7bea..7f7612ce353f1 100755 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -27,6 +27,9 @@ import org.elasticsearch.rest.RestStatus; import java.io.IOException; +import java.util.Locale; + +import static org.hamcrest.Matchers.equalTo; public class IndicesClientIT extends ESRestHighLevelClientTestCase { @@ -57,33 +60,62 @@ public void testDeleteIndex() throws IOException { } public void testOpenExistingIndex() throws IOException { - String indexName = "test_index"; - createIndex(indexName); + String[] indices = randomIndices(1, 5); + for (String index : indices) { + createIndex(index); + closeIndex(index); + ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest("GET", index + "/_search")); + assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus())); + assertThat(exception.getMessage().contains(index), equalTo(true)); + } - OpenIndexRequest openIndexRequest = new OpenIndexRequest(indexName); + OpenIndexRequest openIndexRequest = new OpenIndexRequest(indices); OpenIndexResponse openIndexResponse = execute(openIndexRequest, highLevelClient().indices()::openIndex, highLevelClient().indices()::openIndexAsync); assertTrue(openIndexResponse.isAcknowledged()); + + for (String index : indices) { + client().performRequest("GET", index + "/_search"); + } } public void testOpenNonExistentIndex() throws IOException { - String nonExistentIndex = "non_existent_index"; - assertFalse(indexExists(nonExistentIndex)); - - OpenIndexRequest openIndexRequest = new OpenIndexRequest(nonExistentIndex); + String[] nonExistentIndices = randomIndices(); + for (String nonExistentIndex : nonExistentIndices) { + assertFalse(indexExists(nonExistentIndex)); + } + OpenIndexRequest openIndexRequest = new OpenIndexRequest(nonExistentIndices); ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(openIndexRequest, highLevelClient().indices()::openIndex, highLevelClient().indices()::openIndexAsync)); assertEquals(RestStatus.NOT_FOUND, exception.status()); } + private String[] randomIndices() { + return randomIndices(0, 5); + } + + private String[] randomIndices(int minIndicesNum, int maxIndicesNum) { + int numIndices = randomIntBetween(minIndicesNum, maxIndicesNum); + String[] indices = new String[numIndices]; + for (int i = 0; i < numIndices; i++) { + indices[i] = "index-" + randomAlphaOfLengthBetween(2, 5).toLowerCase(Locale.ROOT); + } + return indices; + } + private static void createIndex(String index) throws IOException { Response response = client().performRequest("PUT", index); - assertEquals(200, response.getStatusLine().getStatusCode()); + assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); } private static boolean indexExists(String index) throws IOException { Response response = client().performRequest("HEAD", index); - return response.getStatusLine().getStatusCode() == 200; + return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode(); + } + + private static void closeIndex(String index) throws IOException { + Response response = client().performRequest("POST", index + "/_close"); + assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java index 0c3c5f15b5ecc..9874b7e890f60 100755 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java @@ -286,6 +286,12 @@ public void testOpenIndex() throws IOException { setRandomMasterTimeout(openIndexRequest, expectedParams); setRandomIndicesOptions(openIndexRequest::indicesOptions, openIndexRequest::indicesOptions, expectedParams); + if (randomBoolean()) { + int waitForActiveShards = randomIntBetween(0, 10); + openIndexRequest.waitForActiveShards(waitForActiveShards); + expectedParams.put("wait_for_active_shards", String.valueOf(waitForActiveShards)); + } + Request request = Request.openIndex(openIndexRequest); StringJoiner endpoint = new StringJoiner("/", "/", "").add(String.join(",", indices)).add("_open"); assertThat(endpoint.toString(), equalTo(request.getEndpoint())); diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponseTests.java b/core/src/test/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponseTests.java index fc8457a3f858f..09ceb7960347b 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponseTests.java @@ -27,6 +27,7 @@ import java.io.IOException; +import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; import static org.hamcrest.CoreMatchers.equalTo; public class OpenIndexResponseTests extends ESTestCase { @@ -37,14 +38,21 @@ public void testFromToXContent() throws IOException { boolean humanReadable = randomBoolean(); final XContentType xContentType = randomFrom(XContentType.values()); BytesReference originalBytes = toShuffledXContent(openIndexResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); + BytesReference mutated; + if (randomBoolean()) { + mutated = insertRandomFields(xContentType, originalBytes, null, random()); + } else { + mutated = originalBytes; + } - OpenIndexResponse parsedCreateIndexResponse; - try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { - parsedCreateIndexResponse = OpenIndexResponse.fromXContent(parser); + OpenIndexResponse parsedOpenIndexResponse; + try (XContentParser parser = createParser(xContentType.xContent(), mutated)) { + parsedOpenIndexResponse = OpenIndexResponse.fromXContent(parser); assertNull(parser.nextToken()); } - assertThat(parsedCreateIndexResponse.isShardsAcknowledged(), equalTo(openIndexResponse.isShardsAcknowledged())); - assertThat(parsedCreateIndexResponse.isAcknowledged(), equalTo(openIndexResponse.isAcknowledged())); + + assertThat(parsedOpenIndexResponse.isShardsAcknowledged(), equalTo(openIndexResponse.isShardsAcknowledged())); + assertThat(parsedOpenIndexResponse.isAcknowledged(), equalTo(openIndexResponse.isAcknowledged())); } private static OpenIndexResponse createTestItem() { From ae7ede9bfa4ac85684fd6fb3c66a50c184752d9a Mon Sep 17 00:00:00 2001 From: olcbean Date: Sun, 3 Dec 2017 20:46:02 +0100 Subject: [PATCH 3/4] Setting getAsync to `final` --- .../main/java/org/elasticsearch/client/RestHighLevelClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java index da0aecdafb366..baa9f7bbea5ad 100755 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java @@ -283,7 +283,7 @@ public final GetResponse get(GetRequest getRequest, Header... headers) throws IO * * See Get API on elastic.co */ - public void getAsync(GetRequest getRequest, ActionListener listener, Header... headers) { + public final void getAsync(GetRequest getRequest, ActionListener listener, Header... headers) { performRequestAsyncAndParseEntity(getRequest, Request::get, GetResponse::fromXContent, listener, singleton(404), headers); } From c3f85ee4c29bc4b2caff6189461cc5581d5107de Mon Sep 17 00:00:00 2001 From: olcbean Date: Mon, 4 Dec 2017 13:45:02 +0100 Subject: [PATCH 4/4] Added tests for strictExpandOpen and lenientExpandOpen Clean up --- .../client/RestHighLevelClient.java | 21 ------------------ .../elasticsearch/client/IndicesClientIT.java | 22 ++++++++++++++----- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java index baa9f7bbea5ad..0a1fd95a94d7a 100755 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java @@ -361,27 +361,6 @@ public final void deleteAsync(DeleteRequest deleteRequest, ActionListener - * Open Index API on elastic.co - */ - public final OpenIndexResponse openIndex(OpenIndexRequest openIndexRequest, Header... headers) throws IOException { - return performRequestAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent, emptySet(), headers); - } - - /** - * Asynchronously opens an index using the Open Index API - * - * See - * Open Index API on elastic.co - */ - public final void openIndexAsync(OpenIndexRequest openIndexRequest, ActionListener listener, Header... headers) { - performRequestAsyncAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent, listener, emptySet(), - headers); - } - /** * Executes a search using the Search API * diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 7f7612ce353f1..3aa4791cee215 100755 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -24,6 +24,7 @@ import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; +import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.rest.RestStatus; import java.io.IOException; @@ -75,12 +76,13 @@ public void testOpenExistingIndex() throws IOException { assertTrue(openIndexResponse.isAcknowledged()); for (String index : indices) { - client().performRequest("GET", index + "/_search"); + Response response = client().performRequest("GET", index + "/_search"); + assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); } } public void testOpenNonExistentIndex() throws IOException { - String[] nonExistentIndices = randomIndices(); + String[] nonExistentIndices = randomIndices(1, 5); for (String nonExistentIndex : nonExistentIndices) { assertFalse(indexExists(nonExistentIndex)); } @@ -89,13 +91,21 @@ public void testOpenNonExistentIndex() throws IOException { ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(openIndexRequest, highLevelClient().indices()::openIndex, highLevelClient().indices()::openIndexAsync)); assertEquals(RestStatus.NOT_FOUND, exception.status()); - } - private String[] randomIndices() { - return randomIndices(0, 5); + OpenIndexRequest lenientOpenIndexRequest = new OpenIndexRequest(nonExistentIndices); + lenientOpenIndexRequest.indicesOptions(IndicesOptions.lenientExpandOpen()); + OpenIndexResponse lenientOpenIndexResponse = execute(lenientOpenIndexRequest, highLevelClient().indices()::openIndex, + highLevelClient().indices()::openIndexAsync); + assertThat(lenientOpenIndexResponse.isAcknowledged(), equalTo(true)); + + OpenIndexRequest strictOpenIndexRequest = new OpenIndexRequest(nonExistentIndices); + strictOpenIndexRequest.indicesOptions(IndicesOptions.strictExpandOpen()); + ElasticsearchException strictException = expectThrows(ElasticsearchException.class, + () -> execute(openIndexRequest, highLevelClient().indices()::openIndex, highLevelClient().indices()::openIndexAsync)); + assertEquals(RestStatus.NOT_FOUND, strictException.status()); } - private String[] randomIndices(int minIndicesNum, int maxIndicesNum) { + private static String[] randomIndices(int minIndicesNum, int maxIndicesNum) { int numIndices = randomIntBetween(minIndicesNum, maxIndicesNum); String[] indices = new String[numIndices]; for (int i = 0; i < numIndices; i++) {