From 7df1df24e83ae3fc4916dd46d0936b73f006cc99 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Wed, 15 Feb 2017 16:00:50 +0100 Subject: [PATCH 1/9] Add delete API to the High Level Rest Client --- .../org/elasticsearch/client/Request.java | 23 +++++++++++ .../client/RestHighLevelClient.java | 18 +++++++++ .../java/org/elasticsearch/client/CrudIT.java | 39 +++++++++++++++++++ 3 files changed, 80 insertions(+) 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 4d6cbac7a5b..9b851574f73 100644 --- 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 @@ -20,6 +20,7 @@ package org.elasticsearch.client; import org.apache.http.HttpEntity; +import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.common.Strings; import org.elasticsearch.common.lucene.uid.Versions; @@ -67,6 +68,10 @@ final class Request { return new Request("GET", getEndpoint(getRequest), getParams(getRequest), null); } + static Request delete(DeleteRequest deleteRequest) { + return new Request("DELETE", deleteEndpoint(deleteRequest), deleteParams(deleteRequest), null); + } + private static Map getParams(GetRequest getRequest) { Map params = new HashMap<>(); putParam("preference", getRequest.preference(), params); @@ -102,11 +107,29 @@ final class Request { return Collections.unmodifiableMap(params); } + private static Map deleteParams(DeleteRequest deleteRequest) { + Map params = new HashMap<>(); + putParam("routing", deleteRequest.routing(), params); + putParam("parent", deleteRequest.parent(), params); + if (deleteRequest.version() != Versions.MATCH_ANY) { + params.put("version", Long.toString(deleteRequest.version())); + } + if (deleteRequest.versionType() != VersionType.INTERNAL) { + params.put("version_type", deleteRequest.versionType().name().toLowerCase(Locale.ROOT)); + } + return Collections.unmodifiableMap(params); + } + private static String getEndpoint(GetRequest getRequest) { StringJoiner pathJoiner = new StringJoiner("/", "/", ""); return pathJoiner.add(getRequest.index()).add(getRequest.type()).add(getRequest.id()).toString(); } + private static String deleteEndpoint(DeleteRequest deleteRequest) { + StringJoiner pathJoiner = new StringJoiner("/", "/", ""); + return pathJoiner.add(deleteRequest.index()).add(deleteRequest.type()).add(deleteRequest.id()).toString(); + } + private static void putParam(String key, String value, Map params) { if (Strings.hasLength(value)) { params.put(key, value); 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 3ec4b91965d..241676d257c 100644 --- 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.ElasticsearchStatusException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.main.MainRequest; @@ -92,6 +94,22 @@ public class RestHighLevelClient { Collections.emptySet(), headers); } + /** + * Deletes a document by id using the delete api + */ + public DeleteResponse delete(DeleteRequest deleteRequest, Header... headers) throws IOException { + return performRequestAndParseEntity(deleteRequest, Request::delete, DeleteResponse::fromXContent, Collections.singleton(404), + headers); + } + + /** + * Asynchronously deletes a document by id using the delete api + */ + public void deleteAsync(DeleteRequest deleteRequest, ActionListener listener, Header... headers) { + performRequestAsyncAndParseEntity(deleteRequest, Request::delete, DeleteResponse::fromXContent, listener, + Collections.singleton(404), headers); + } + private Resp performRequestAndParseEntity(Req request, Function requestConverter, CheckedFunction entityParser, Set ignores, Header... headers) throws IOException { return performRequest(request, requestConverter, (response) -> parseEntity(response.getEntity(), entityParser), ignores, headers); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java index 2a51e240662..07036c27e4a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java @@ -22,6 +22,9 @@ package org.elasticsearch.client; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.action.DocWriteResponse; +import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.common.Strings; @@ -141,4 +144,40 @@ public class CrudIT extends ESRestHighLevelClientTestCase { assertEquals("value1", sourceAsMap.get("field1")); } } + + public void testDelete() throws IOException { + { + DeleteRequest deleteRequest = new DeleteRequest("index", "type", "does_not_exist"); + DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); + assertEquals("index", deleteResponse.getIndex()); + assertEquals("type", deleteResponse.getType()); + assertEquals("does_not_exist", deleteResponse.getId()); + assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult()); + assertEquals(1, deleteResponse.getVersion()); + } + String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}"; + StringEntity stringEntity = new StringEntity(document, ContentType.APPLICATION_JSON); + Response response = client().performRequest("PUT", "/index/type/id", Collections.singletonMap("refresh", "wait_for"), stringEntity); + assertEquals(201, response.getStatusLine().getStatusCode()); + { + DeleteRequest deleteRequest = new DeleteRequest("index", "type", "id").version(2); + ElasticsearchException exception = expectThrows(ElasticsearchException.class, + () -> execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync)); + assertEquals(RestStatus.CONFLICT, exception.status()); + assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, " + "reason=[type][id]: " + + "version conflict, current version [1] is different than the one provided [2]]", exception.getMessage()); + assertEquals("index", exception.getMetadata("es.index").get(0)); + } + { + DeleteRequest deleteRequest = new DeleteRequest("index", "type", "id"); + if (randomBoolean()) { + deleteRequest.version(1L); + } + DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); + assertEquals("index", deleteResponse.getIndex()); + assertEquals("type", deleteResponse.getType()); + assertEquals("id", deleteResponse.getId()); + assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult()); + } + } } From 14f04905f84e1eae25fb45c23ce16cec3f19a38e Mon Sep 17 00:00:00 2001 From: David Pilato Date: Wed, 15 Feb 2017 16:14:33 +0100 Subject: [PATCH 2/9] Merge with master --- .../org/elasticsearch/client/Request.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) 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 2e6b67dbc19..2fe83e5eedb 100644 --- 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 @@ -20,6 +20,7 @@ package org.elasticsearch.client; import org.apache.http.HttpEntity; +import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpPost; @@ -120,19 +121,18 @@ final class Request { } static Request delete(DeleteRequest deleteRequest) { - return new Request("DELETE", deleteEndpoint(deleteRequest), deleteParams(deleteRequest), null); + String endpoint = endpoint(deleteRequest.index(), deleteRequest.type(), deleteRequest.id()); - Map params = new HashMap<>(); - putParam("routing", deleteRequest.routing(), params); - putParam("parent", deleteRequest.parent(), params); - if (deleteRequest.version() != Versions.MATCH_ANY) { - params.put("version", Long.toString(deleteRequest.version())); - } - if (deleteRequest.versionType() != VersionType.INTERNAL) { - params.put("version_type", deleteRequest.versionType().name().toLowerCase(Locale.ROOT)); - } - return Collections.unmodifiableMap(params); + Params parameters = Params.builder(); + parameters.withRouting(deleteRequest.routing()); + parameters.withParent(deleteRequest.parent()); + parameters.withTimeout(deleteRequest.timeout()); + parameters.withVersion(deleteRequest.version()); + parameters.withVersionType(deleteRequest.versionType()); + parameters.withRefreshPolicy(deleteRequest.getRefreshPolicy()); + parameters.withWaitForActiveShards(deleteRequest.waitForActiveShards()); + return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null); } /** @@ -141,6 +141,7 @@ final class Request { static String endpoint(String... parts) { if (parts == null || parts.length == 0) { return DELIMITER; + } StringJoiner joiner = new StringJoiner(DELIMITER, DELIMITER, ""); for (String part : parts) { From 5aa2ab3bbc2a0bc2dca1e1399e6dada5907d24c2 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Fri, 17 Feb 2017 11:46:02 +0100 Subject: [PATCH 3/9] Fix after 1st review * Sort alphabetically method names * Add javadoc link to the ref guide * Add more tests about routing, version and version type --- .../org/elasticsearch/client/Request.java | 32 +++-- .../client/RestHighLevelClient.java | 8 +- .../java/org/elasticsearch/client/CrudIT.java | 128 +++++++++++++----- 3 files changed, 117 insertions(+), 51 deletions(-) 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 2fe83e5eedb..02c11aa42d1 100644 --- 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 @@ -71,8 +71,21 @@ final class Request { '}'; } - static Request ping() { - return new Request("HEAD", "/", Collections.emptyMap(), null); + // For developers please add your new methods in the alphabetical order + + static Request delete(DeleteRequest deleteRequest) { + String endpoint = endpoint(deleteRequest.index(), deleteRequest.type(), deleteRequest.id()); + + Params parameters = Params.builder(); + parameters.withRouting(deleteRequest.routing()); + parameters.withParent(deleteRequest.parent()); + parameters.withTimeout(deleteRequest.timeout()); + parameters.withVersion(deleteRequest.version()); + parameters.withVersionType(deleteRequest.versionType()); + parameters.withRefreshPolicy(deleteRequest.getRefreshPolicy()); + parameters.withWaitForActiveShards(deleteRequest.waitForActiveShards()); + + return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null); } static Request exists(GetRequest getRequest) { @@ -120,19 +133,8 @@ final class Request { return new Request(method, endpoint, parameters.getParams(), entity); } - static Request delete(DeleteRequest deleteRequest) { - String endpoint = endpoint(deleteRequest.index(), deleteRequest.type(), deleteRequest.id()); - - Params parameters = Params.builder(); - parameters.withRouting(deleteRequest.routing()); - parameters.withParent(deleteRequest.parent()); - parameters.withTimeout(deleteRequest.timeout()); - parameters.withVersion(deleteRequest.version()); - parameters.withVersionType(deleteRequest.versionType()); - parameters.withRefreshPolicy(deleteRequest.getRefreshPolicy()); - parameters.withWaitForActiveShards(deleteRequest.waitForActiveShards()); - - return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null); + static Request ping() { + return new Request("HEAD", "/", 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 e60c47e8773..86a524d97ed 100644 --- 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 @@ -124,7 +124,9 @@ public class RestHighLevelClient { } /** - * Deletes a document by id using the delete api + * Deletes a document by id using the Delete api + * + * See Delete API on elastic.co */ public DeleteResponse delete(DeleteRequest deleteRequest, Header... headers) throws IOException { return performRequestAndParseEntity(deleteRequest, Request::delete, DeleteResponse::fromXContent, Collections.singleton(404), @@ -132,7 +134,9 @@ public class RestHighLevelClient { } /** - * Asynchronously deletes a document by id using the delete api + * Asynchronously deletes a document by id using the Delete api + * + * See Delete API on elastic.co */ public void deleteAsync(DeleteRequest deleteRequest, ActionListener listener, Header... headers) { performRequestAsyncAndParseEntity(deleteRequest, Request::delete, DeleteResponse::fromXContent, listener, diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java index 7f643cf7827..50aafc26635 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java @@ -46,6 +46,79 @@ import static org.hamcrest.CoreMatchers.containsString; public class CrudIT extends ESRestHighLevelClientTestCase { + public void testDelete() throws IOException { + { + // Testing non existing document + DeleteRequest deleteRequest = new DeleteRequest("index", "type", "does_not_exist"); + DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); + assertEquals("index", deleteResponse.getIndex()); + assertEquals("type", deleteResponse.getType()); + assertEquals("does_not_exist", deleteResponse.getId()); + assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult()); + } + { + // Testing deletion + String docId = createSampleDocument(createSampleIndexRequest()); + DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId); + if (randomBoolean()) { + deleteRequest.version(1L); + } + DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); + assertEquals("index", deleteResponse.getIndex()); + assertEquals("type", deleteResponse.getType()); + assertEquals(docId, deleteResponse.getId()); + assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult()); + } + { + // Testing version conflict + String docId = createSampleDocument(createSampleIndexRequest()); + DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).version(2); + ElasticsearchException exception = expectThrows(ElasticsearchException.class, + () -> execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync)); + assertEquals(RestStatus.CONFLICT, exception.status()); + assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, " + + "reason=[type][" + docId + "]: " + + "version conflict, current version [1] is different than the one provided [2]]", exception.getMessage()); + assertEquals("index", exception.getMetadata("es.index").get(0)); + } + { + // Testing version type + String docId = createSampleDocument(createSampleIndexRequest() + .id(randomAsciiOfLength(15)) + .versionType(VersionType.EXTERNAL).version(12)); + DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).versionType(VersionType.EXTERNAL).version(13); + DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); + assertEquals("index", deleteResponse.getIndex()); + assertEquals("type", deleteResponse.getType()); + assertEquals(docId, deleteResponse.getId()); + assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult()); + } + { + // Testing version type with a wrong version + String docId = createSampleDocument(createSampleIndexRequest() + .id(randomAsciiOfLength(15)) + .versionType(VersionType.EXTERNAL).version(12)); + ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> { + DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).versionType(VersionType.EXTERNAL).version(10); + execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); + }); + assertEquals(RestStatus.CONFLICT, exception.status()); + assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, reason=[type][" + + docId + "]: version conflict, current version [12] is higher or equal to the one provided [10]]", exception.getMessage()); + assertEquals("index", exception.getMetadata("es.index").get(0)); + } + { + // Testing routing + String docId = createSampleDocument(createSampleIndexRequest().routing("foo")); + DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).routing("foo"); + DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); + assertEquals("index", deleteResponse.getIndex()); + assertEquals("type", deleteResponse.getType()); + assertEquals(docId, deleteResponse.getId()); + assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult()); + } + } + public void testExists() throws IOException { { GetRequest getRequest = new GetRequest("index", "type", "id"); @@ -268,39 +341,26 @@ public class CrudIT extends ESRestHighLevelClientTestCase { } } - public void testDelete() throws IOException { - { - DeleteRequest deleteRequest = new DeleteRequest("index", "type", "does_not_exist"); - DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); - assertEquals("index", deleteResponse.getIndex()); - assertEquals("type", deleteResponse.getType()); - assertEquals("does_not_exist", deleteResponse.getId()); - assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult()); - assertEquals(1, deleteResponse.getVersion()); - } - String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}"; - StringEntity stringEntity = new StringEntity(document, ContentType.APPLICATION_JSON); - Response response = client().performRequest("PUT", "/index/type/id", Collections.singletonMap("refresh", "wait_for"), stringEntity); - assertEquals(201, response.getStatusLine().getStatusCode()); - { - DeleteRequest deleteRequest = new DeleteRequest("index", "type", "id").version(2); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync)); - assertEquals(RestStatus.CONFLICT, exception.status()); - assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, " + "reason=[type][id]: " + - "version conflict, current version [1] is different than the one provided [2]]", exception.getMessage()); - assertEquals("index", exception.getMetadata("es.index").get(0)); - } - { - DeleteRequest deleteRequest = new DeleteRequest("index", "type", "id"); - if (randomBoolean()) { - deleteRequest.version(1L); - } - DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); - assertEquals("index", deleteResponse.getIndex()); - assertEquals("type", deleteResponse.getType()); - assertEquals("id", deleteResponse.getId()); - assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult()); - } + /** + * @return A sample index request + * @throws IOException If problem when generating the content + */ + private IndexRequest createSampleIndexRequest() throws IOException { + final XContentType xContentType = randomFrom(XContentType.values()); + IndexRequest indexRequest = new IndexRequest("index", "type"); + indexRequest.source(XContentBuilder.builder(xContentType.xContent()).startObject().field("test", "test").endObject()); + return indexRequest; + } + + /** + * Executes an index request and returns the document _id + * @param request index request + * @return the document _id + * @throws IOException if something goes wrong while executing the index request + */ + private String createSampleDocument(IndexRequest request) throws IOException { + IndexResponse indexResponse = execute(request, highLevelClient()::index, highLevelClient()::indexAsync); + assertEquals(RestStatus.CREATED, indexResponse.status()); + return indexResponse.getId(); } } From b2ec4c1f17768a860e94f0346cdffeb40fe46c42 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Mon, 20 Feb 2017 09:36:47 +0100 Subject: [PATCH 4/9] Remove a non needed comment --- .../src/main/java/org/elasticsearch/client/Request.java | 2 -- 1 file changed, 2 deletions(-) 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 02c11aa42d1..aea33484011 100644 --- 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 @@ -71,8 +71,6 @@ final class Request { '}'; } - // For developers please add your new methods in the alphabetical order - static Request delete(DeleteRequest deleteRequest) { String endpoint = endpoint(deleteRequest.index(), deleteRequest.type(), deleteRequest.id()); From efa28e05f80ca500a95b61f856d9ddb42840a2c0 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Mon, 20 Feb 2017 09:56:42 +0100 Subject: [PATCH 5/9] Check that we correctly propagate delete request parameters --- .../elasticsearch/client/RequestTests.java | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) 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 f9bf4cc1a39..ec3977c4a20 100644 --- 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 @@ -22,6 +22,7 @@ package org.elasticsearch.client; import org.apache.http.HttpEntity; import org.apache.http.entity.ByteArrayEntity; import org.elasticsearch.action.DocWriteRequest; +import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.support.WriteRequest; @@ -55,6 +56,64 @@ public class RequestTests extends ESTestCase { getAndExistsTest(Request::get, "GET"); } + public void testDelete() throws IOException { + String index = randomAsciiOfLengthBetween(3, 10); + String type = randomAsciiOfLengthBetween(3, 10); + String id = randomAsciiOfLengthBetween(3, 10); + DeleteRequest deleteRequest = new DeleteRequest(index, type, id); + + Map expectedParams = new HashMap<>(); + + if (randomBoolean()) { + long version = randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED, Versions.NOT_FOUND, randomNonNegativeLong()); + deleteRequest.version(version); + if (version != Versions.MATCH_ANY) { + expectedParams.put("version", Long.toString(version)); + } + } + if (randomBoolean()) { + VersionType versionType = randomFrom(VersionType.values()); + deleteRequest.versionType(versionType); + if (versionType != VersionType.INTERNAL) { + expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT)); + } + } + + if (randomBoolean()) { + String timeout = randomTimeValue(); + deleteRequest.timeout(timeout); + expectedParams.put("timeout", timeout); + } else { + expectedParams.put("timeout", ReplicationRequest.DEFAULT_TIMEOUT.getStringRep()); + } + + if (frequently()) { + if (randomBoolean()) { + String routing = randomAsciiOfLengthBetween(3, 10); + deleteRequest.routing(routing); + expectedParams.put("routing", routing); + } + if (randomBoolean()) { + String parent = randomAsciiOfLengthBetween(3, 10); + deleteRequest.parent(parent); + expectedParams.put("parent", parent); + } + + if (randomBoolean()) { + WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values()); + deleteRequest.setRefreshPolicy(refreshPolicy); + if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) { + expectedParams.put("refresh", refreshPolicy.getValue()); + } + } + } + + Request request = Request.delete(deleteRequest); + assertEquals("/" + index + "/" + type + "/" + id, request.endpoint); + assertEquals(expectedParams, request.params); + assertEquals("DELETE", request.method); + } + public void testExists() { getAndExistsTest(Request::exists, "HEAD"); } @@ -307,4 +366,4 @@ public class RequestTests extends ESTestCase { assertEquals("/a/b/_create", Request.endpoint("a", "b", "_create")); assertEquals("/a/b/c/_create", Request.endpoint("a", "b", "c", "_create")); } -} \ No newline at end of file +} From 0ee74f9c2844c8bb6310079aa725dfe1c783fb94 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Mon, 20 Feb 2017 10:09:44 +0100 Subject: [PATCH 6/9] Remove createSampleIndexRequest method and provide ids --- .../java/org/elasticsearch/client/CrudIT.java | 39 +++++++------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java index 50aafc26635..b1c1ba8a66f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java @@ -49,16 +49,18 @@ public class CrudIT extends ESRestHighLevelClientTestCase { public void testDelete() throws IOException { { // Testing non existing document - DeleteRequest deleteRequest = new DeleteRequest("index", "type", "does_not_exist"); + String docId = "does_not_exist"; + DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId); DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); assertEquals("index", deleteResponse.getIndex()); assertEquals("type", deleteResponse.getType()); - assertEquals("does_not_exist", deleteResponse.getId()); + assertEquals(docId, deleteResponse.getId()); assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult()); } { // Testing deletion - String docId = createSampleDocument(createSampleIndexRequest()); + String docId = "id"; + createSampleDocument(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar"))); DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId); if (randomBoolean()) { deleteRequest.version(1L); @@ -71,7 +73,8 @@ public class CrudIT extends ESRestHighLevelClientTestCase { } { // Testing version conflict - String docId = createSampleDocument(createSampleIndexRequest()); + String docId = "version_conflict"; + createSampleDocument(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar"))); DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).version(2); ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync)); @@ -83,8 +86,8 @@ public class CrudIT extends ESRestHighLevelClientTestCase { } { // Testing version type - String docId = createSampleDocument(createSampleIndexRequest() - .id(randomAsciiOfLength(15)) + String docId = "version_type"; + createSampleDocument(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar")) .versionType(VersionType.EXTERNAL).version(12)); DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).versionType(VersionType.EXTERNAL).version(13); DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); @@ -95,8 +98,8 @@ public class CrudIT extends ESRestHighLevelClientTestCase { } { // Testing version type with a wrong version - String docId = createSampleDocument(createSampleIndexRequest() - .id(randomAsciiOfLength(15)) + String docId = "wrong_version"; + createSampleDocument(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar")) .versionType(VersionType.EXTERNAL).version(12)); ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> { DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).versionType(VersionType.EXTERNAL).version(10); @@ -109,7 +112,8 @@ public class CrudIT extends ESRestHighLevelClientTestCase { } { // Testing routing - String docId = createSampleDocument(createSampleIndexRequest().routing("foo")); + String docId = "routing"; + createSampleDocument(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar")).routing("foo")); DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).routing("foo"); DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); assertEquals("index", deleteResponse.getIndex()); @@ -342,25 +346,12 @@ public class CrudIT extends ESRestHighLevelClientTestCase { } /** - * @return A sample index request - * @throws IOException If problem when generating the content - */ - private IndexRequest createSampleIndexRequest() throws IOException { - final XContentType xContentType = randomFrom(XContentType.values()); - IndexRequest indexRequest = new IndexRequest("index", "type"); - indexRequest.source(XContentBuilder.builder(xContentType.xContent()).startObject().field("test", "test").endObject()); - return indexRequest; - } - - /** - * Executes an index request and returns the document _id + * Index a document * @param request index request - * @return the document _id * @throws IOException if something goes wrong while executing the index request */ - private String createSampleDocument(IndexRequest request) throws IOException { + private void createSampleDocument(IndexRequest request) throws IOException { IndexResponse indexResponse = execute(request, highLevelClient()::index, highLevelClient()::indexAsync); assertEquals(RestStatus.CREATED, indexResponse.status()); - return indexResponse.getId(); } } From bf8241eec5478c7dd6bf1ac3b5f33d8724056626 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Mon, 20 Feb 2017 10:33:19 +0100 Subject: [PATCH 7/9] Remove createSampleDocument method and use the sync'ed index method --- .../java/org/elasticsearch/client/CrudIT.java | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java index b1c1ba8a66f..1aa05c43017 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java @@ -60,7 +60,7 @@ public class CrudIT extends ESRestHighLevelClientTestCase { { // Testing deletion String docId = "id"; - createSampleDocument(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar"))); + highLevelClient().index(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar"))); DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId); if (randomBoolean()) { deleteRequest.version(1L); @@ -74,7 +74,7 @@ public class CrudIT extends ESRestHighLevelClientTestCase { { // Testing version conflict String docId = "version_conflict"; - createSampleDocument(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar"))); + highLevelClient().index(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar"))); DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).version(2); ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync)); @@ -87,7 +87,7 @@ public class CrudIT extends ESRestHighLevelClientTestCase { { // Testing version type String docId = "version_type"; - createSampleDocument(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar")) + highLevelClient().index(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar")) .versionType(VersionType.EXTERNAL).version(12)); DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).versionType(VersionType.EXTERNAL).version(13); DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); @@ -99,7 +99,7 @@ public class CrudIT extends ESRestHighLevelClientTestCase { { // Testing version type with a wrong version String docId = "wrong_version"; - createSampleDocument(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar")) + highLevelClient().index(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar")) .versionType(VersionType.EXTERNAL).version(12)); ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> { DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).versionType(VersionType.EXTERNAL).version(10); @@ -113,7 +113,7 @@ public class CrudIT extends ESRestHighLevelClientTestCase { { // Testing routing String docId = "routing"; - createSampleDocument(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar")).routing("foo")); + highLevelClient().index(new IndexRequest("index", "type", docId).source(Collections.singletonMap("foo", "bar")).routing("foo")); DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).routing("foo"); DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); assertEquals("index", deleteResponse.getIndex()); @@ -344,14 +344,4 @@ public class CrudIT extends ESRestHighLevelClientTestCase { "version conflict, document already exists (current version [1])]", exception.getMessage()); } } - - /** - * Index a document - * @param request index request - * @throws IOException if something goes wrong while executing the index request - */ - private void createSampleDocument(IndexRequest request) throws IOException { - IndexResponse indexResponse = execute(request, highLevelClient()::index, highLevelClient()::indexAsync); - assertEquals(RestStatus.CREATED, indexResponse.status()); - } } From 9bde68e4d6f17510339341c4a4dd76d62af35185 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Mon, 20 Feb 2017 10:43:28 +0100 Subject: [PATCH 8/9] delete and index tests can share some part of the code --- .../elasticsearch/client/RequestTests.java | 112 +++++++----------- 1 file changed, 46 insertions(+), 66 deletions(-) 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 ec3977c4a20..e7aee6a768d 100644 --- 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.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.support.WriteRequest; +import org.elasticsearch.action.support.replication.ReplicatedWriteRequest; import org.elasticsearch.action.support.replication.ReplicationRequest; import org.elasticsearch.common.Strings; import org.elasticsearch.common.lucene.uid.Versions; @@ -64,28 +65,8 @@ public class RequestTests extends ESTestCase { Map expectedParams = new HashMap<>(); - if (randomBoolean()) { - long version = randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED, Versions.NOT_FOUND, randomNonNegativeLong()); - deleteRequest.version(version); - if (version != Versions.MATCH_ANY) { - expectedParams.put("version", Long.toString(version)); - } - } - if (randomBoolean()) { - VersionType versionType = randomFrom(VersionType.values()); - deleteRequest.versionType(versionType); - if (versionType != VersionType.INTERNAL) { - expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT)); - } - } - - if (randomBoolean()) { - String timeout = randomTimeValue(); - deleteRequest.timeout(timeout); - expectedParams.put("timeout", timeout); - } else { - expectedParams.put("timeout", ReplicationRequest.DEFAULT_TIMEOUT.getStringRep()); - } + enrichDocWriteRequest(deleteRequest, expectedParams); + enrichReplicationRequest(deleteRequest, expectedParams); if (frequently()) { if (randomBoolean()) { @@ -98,14 +79,6 @@ public class RequestTests extends ESTestCase { deleteRequest.parent(parent); expectedParams.put("parent", parent); } - - if (randomBoolean()) { - WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values()); - deleteRequest.setRefreshPolicy(refreshPolicy); - if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) { - expectedParams.put("refresh", refreshPolicy.getValue()); - } - } } Request request = Request.delete(deleteRequest); @@ -244,34 +217,8 @@ public class RequestTests extends ESTestCase { } } - // There is some logic around _create endpoint and version/version type - if (indexRequest.opType() == DocWriteRequest.OpType.CREATE) { - indexRequest.version(randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED)); - expectedParams.put("version", Long.toString(Versions.MATCH_DELETED)); - } else { - if (randomBoolean()) { - long version = randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED, Versions.NOT_FOUND, randomNonNegativeLong()); - indexRequest.version(version); - if (version != Versions.MATCH_ANY) { - expectedParams.put("version", Long.toString(version)); - } - } - if (randomBoolean()) { - VersionType versionType = randomFrom(VersionType.values()); - indexRequest.versionType(versionType); - if (versionType != VersionType.INTERNAL) { - expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT)); - } - } - } - - if (randomBoolean()) { - String timeout = randomTimeValue(); - indexRequest.timeout(timeout); - expectedParams.put("timeout", timeout); - } else { - expectedParams.put("timeout", ReplicationRequest.DEFAULT_TIMEOUT.getStringRep()); - } + enrichDocWriteRequest(indexRequest, expectedParams); + enrichReplicationRequest(indexRequest, expectedParams); if (frequently()) { if (randomBoolean()) { @@ -289,14 +236,6 @@ public class RequestTests extends ESTestCase { indexRequest.setPipeline(pipeline); expectedParams.put("pipeline", pipeline); } - - if (randomBoolean()) { - WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values()); - indexRequest.setRefreshPolicy(refreshPolicy); - if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) { - expectedParams.put("refresh", refreshPolicy.getValue()); - } - } } XContentType xContentType = randomFrom(XContentType.values()); @@ -366,4 +305,45 @@ public class RequestTests extends ESTestCase { assertEquals("/a/b/_create", Request.endpoint("a", "b", "_create")); assertEquals("/a/b/c/_create", Request.endpoint("a", "b", "c", "_create")); } + + private void enrichReplicationRequest(ReplicatedWriteRequest request, Map expectedParams) { + if (randomBoolean()) { + String timeout = randomTimeValue(); + request.timeout(timeout); + expectedParams.put("timeout", timeout); + } else { + expectedParams.put("timeout", ReplicationRequest.DEFAULT_TIMEOUT.getStringRep()); + } + + if (randomBoolean()) { + WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values()); + request.setRefreshPolicy(refreshPolicy); + if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) { + expectedParams.put("refresh", refreshPolicy.getValue()); + } + } + } + + private void enrichDocWriteRequest(DocWriteRequest request, Map expectedParams) { + // There is some logic around _create endpoint and version/version type + if (request.opType() == DocWriteRequest.OpType.CREATE) { + request.version(randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED)); + expectedParams.put("version", Long.toString(Versions.MATCH_DELETED)); + } else { + if (randomBoolean()) { + long version = randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED, Versions.NOT_FOUND, randomNonNegativeLong()); + request.version(version); + if (version != Versions.MATCH_ANY) { + expectedParams.put("version", Long.toString(version)); + } + } + if (randomBoolean()) { + VersionType versionType = randomFrom(VersionType.values()); + request.versionType(versionType); + if (versionType != VersionType.INTERNAL) { + expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT)); + } + } + } + } } From 4ebc6dd0d0eca40ff5c8881b924367f810512b05 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Fri, 24 Feb 2017 09:19:39 +0100 Subject: [PATCH 9/9] Fix after last merge with master and apply last comments --- .../client/RestHighLevelClient.java | 15 +++-- .../java/org/elasticsearch/client/CrudIT.java | 8 +-- .../elasticsearch/client/RequestTests.java | 58 +++++++++++-------- 3 files changed, 44 insertions(+), 37 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 2bc675cdce2..913a1ae52d7 100644 --- 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,10 +26,10 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.action.delete.DeleteRequest; -import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; +import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; @@ -45,6 +45,7 @@ import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestStatus; import java.io.IOException; +import java.util.Collections; import java.util.Objects; import java.util.Set; @@ -161,10 +162,6 @@ public class RestHighLevelClient { performRequestAsyncAndParseEntity(updateRequest, Request::update, UpdateResponse::fromXContent, listener, emptySet(), headers); } - private Resp performRequestAndParseEntity(Req request, - CheckedFunction requestConverter, - CheckedFunction entityParser, - Set ignores, Header... headers) throws IOException { /** * Deletes a document by id using the Delete api * @@ -185,8 +182,10 @@ public class RestHighLevelClient { Collections.singleton(404), headers); } - private Resp performRequestAndParseEntity(Req request, Function requestConverter, - CheckedFunction entityParser, Set ignores, Header... headers) throws IOException { + private Resp performRequestAndParseEntity(Req request, + CheckedFunction requestConverter, + CheckedFunction entityParser, + Set ignores, Header... headers) throws IOException { return performRequest(request, requestConverter, (response) -> parseEntity(response.getEntity(), entityParser), ignores, headers); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java index dd1ad5902e0..346d7d7c756 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java @@ -22,15 +22,14 @@ package org.elasticsearch.client; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.action.DocWriteResponse; -import org.elasticsearch.action.delete.DeleteRequest; -import org.elasticsearch.action.delete.DeleteResponse; -import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; @@ -89,8 +88,7 @@ public class CrudIT extends ESRestHighLevelClientTestCase { ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync)); assertEquals(RestStatus.CONFLICT, exception.status()); - assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, " + - "reason=[type][" + docId + "]: " + + assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, reason=[type][" + docId + "]: " + "version conflict, current version [1] is different than the one provided [2]]", exception.getMessage()); assertEquals("index", exception.getMetadata("es.index").get(0)); } 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 9ede218639c..62bb6b551af 100644 --- 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 @@ -22,7 +22,6 @@ package org.elasticsearch.client; import org.apache.http.HttpEntity; import org.apache.http.entity.ByteArrayEntity; import org.elasticsearch.action.DocWriteRequest; -import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkShardRequest; import org.elasticsearch.action.delete.DeleteRequest; @@ -79,8 +78,10 @@ public class RequestTests extends ESTestCase { Map expectedParams = new HashMap<>(); - enrichDocWriteRequest(deleteRequest, expectedParams); - enrichReplicationRequest(deleteRequest, expectedParams); + setRandomTimeout(deleteRequest, expectedParams); + setRandomRefreshPolicy(deleteRequest, expectedParams); + setRandomVersion(deleteRequest, expectedParams); + setRandomVersionType(deleteRequest, expectedParams); if (frequently()) { if (randomBoolean()) { @@ -99,6 +100,7 @@ public class RequestTests extends ESTestCase { assertEquals("/" + index + "/" + type + "/" + id, request.endpoint); assertEquals(expectedParams, request.params); assertEquals("DELETE", request.method); + assertNull(request.entity); } public void testExists() { @@ -195,8 +197,17 @@ public class RequestTests extends ESTestCase { } } - enrichDocWriteRequest(indexRequest, expectedParams); - enrichReplicationRequest(indexRequest, expectedParams); + setRandomTimeout(indexRequest, expectedParams); + setRandomRefreshPolicy(indexRequest, expectedParams); + + // There is some logic around _create endpoint and version/version type + if (indexRequest.opType() == DocWriteRequest.OpType.CREATE) { + indexRequest.version(randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED)); + expectedParams.put("version", Long.toString(Versions.MATCH_DELETED)); + } else { + setRandomVersion(indexRequest, expectedParams); + setRandomVersionType(indexRequest, expectedParams); + } if (frequently()) { if (randomBoolean()) { @@ -675,7 +686,7 @@ public class RequestTests extends ESTestCase { } } - private void enrichReplicationRequest(ReplicatedWriteRequest request, Map expectedParams) { + private static void setRandomTimeout(ReplicationRequest request, Map expectedParams) { if (randomBoolean()) { String timeout = randomTimeValue(); request.timeout(timeout); @@ -683,7 +694,9 @@ public class RequestTests extends ESTestCase { } else { expectedParams.put("timeout", ReplicationRequest.DEFAULT_TIMEOUT.getStringRep()); } + } + private static void setRandomRefreshPolicy(ReplicatedWriteRequest request, Map expectedParams) { if (randomBoolean()) { WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values()); request.setRefreshPolicy(refreshPolicy); @@ -693,25 +706,22 @@ public class RequestTests extends ESTestCase { } } - private void enrichDocWriteRequest(DocWriteRequest request, Map expectedParams) { - // There is some logic around _create endpoint and version/version type - if (request.opType() == DocWriteRequest.OpType.CREATE) { - request.version(randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED)); - expectedParams.put("version", Long.toString(Versions.MATCH_DELETED)); - } else { - if (randomBoolean()) { - long version = randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED, Versions.NOT_FOUND, randomNonNegativeLong()); - request.version(version); - if (version != Versions.MATCH_ANY) { - expectedParams.put("version", Long.toString(version)); - } + private static void setRandomVersion(DocWriteRequest request, Map expectedParams) { + if (randomBoolean()) { + long version = randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED, Versions.NOT_FOUND, randomNonNegativeLong()); + request.version(version); + if (version != Versions.MATCH_ANY) { + expectedParams.put("version", Long.toString(version)); } - if (randomBoolean()) { - VersionType versionType = randomFrom(VersionType.values()); - request.versionType(versionType); - if (versionType != VersionType.INTERNAL) { - expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT)); - } + } + } + + private static void setRandomVersionType(DocWriteRequest request, Map expectedParams) { + if (randomBoolean()) { + VersionType versionType = randomFrom(VersionType.values()); + request.versionType(versionType); + if (versionType != VersionType.INTERNAL) { + expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT)); } } }