From 4ebc6dd0d0eca40ff5c8881b924367f810512b05 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Fri, 24 Feb 2017 09:19:39 +0100 Subject: [PATCH] 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)); } } }