From 27e157f67c2ccaf9c3b8eda3980eaf26e0f1dd7b Mon Sep 17 00:00:00 2001 From: javanna Date: Fri, 8 Dec 2017 10:51:10 +0100 Subject: [PATCH] [TEST] remove code duplications in RequestTests --- .../elasticsearch/client/RequestTests.java | 104 ++++++------------ 1 file changed, 35 insertions(+), 69 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 f39a7c77b80..7d63ed1ed40 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 @@ -42,7 +42,6 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.action.support.master.MasterNodeRequest; -import org.elasticsearch.action.support.replication.ReplicatedWriteRequest; import org.elasticsearch.action.support.replication.ReplicationRequest; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.common.CheckedBiConsumer; @@ -156,9 +155,9 @@ public class RequestTests extends ESTestCase { Map expectedParams = new HashMap<>(); setRandomTimeout(deleteRequest::timeout, ReplicationRequest.DEFAULT_TIMEOUT, expectedParams); - setRandomRefreshPolicy(deleteRequest, expectedParams); + setRandomRefreshPolicy(deleteRequest::setRefreshPolicy, expectedParams); setRandomVersion(deleteRequest, expectedParams); - setRandomVersionType(deleteRequest, expectedParams); + setRandomVersionType(deleteRequest::versionType, expectedParams); if (frequently()) { if (randomBoolean()) { @@ -223,27 +222,13 @@ public class RequestTests extends ESTestCase { expectedParams.put("version", Long.toString(version)); } } - if (randomBoolean()) { - VersionType versionType = randomFrom(VersionType.values()); - getRequest.versionType(versionType); - if (versionType != VersionType.INTERNAL) { - expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT)); - } - } + setRandomVersionType(getRequest::versionType, expectedParams); if (randomBoolean()) { int numStoredFields = randomIntBetween(1, 10); String[] storedFields = new String[numStoredFields]; - StringBuilder storedFieldsParam = new StringBuilder(); - for (int i = 0; i < numStoredFields; i++) { - String storedField = randomAlphaOfLengthBetween(3, 10); - storedFields[i] = storedField; - storedFieldsParam.append(storedField); - if (i < numStoredFields - 1) { - storedFieldsParam.append(","); - } - } + String storedFieldsParam = randomFields(storedFields); getRequest.storedFields(storedFields); - expectedParams.put("stored_fields", storedFieldsParam.toString()); + expectedParams.put("stored_fields", storedFieldsParam); } if (randomBoolean()) { randomizeFetchSourceContextParams(getRequest::fetchSourceContext, expectedParams); @@ -350,7 +335,7 @@ public class RequestTests extends ESTestCase { } setRandomTimeout(indexRequest::timeout, ReplicationRequest.DEFAULT_TIMEOUT, expectedParams); - setRandomRefreshPolicy(indexRequest, expectedParams); + setRandomRefreshPolicy(indexRequest::setRefreshPolicy, expectedParams); // There is some logic around _create endpoint and version/version type if (indexRequest.opType() == DocWriteRequest.OpType.CREATE) { @@ -358,7 +343,7 @@ public class RequestTests extends ESTestCase { expectedParams.put("version", Long.toString(Versions.MATCH_DELETED)); } else { setRandomVersion(indexRequest, expectedParams); - setRandomVersionType(indexRequest, expectedParams); + setRandomVersionType(indexRequest::versionType, expectedParams); } if (frequently()) { @@ -462,20 +447,8 @@ public class RequestTests extends ESTestCase { } } setRandomWaitForActiveShards(updateRequest::waitForActiveShards, expectedParams); - if (randomBoolean()) { - long version = randomLong(); - updateRequest.version(version); - if (version != Versions.MATCH_ANY) { - expectedParams.put("version", Long.toString(version)); - } - } - if (randomBoolean()) { - VersionType versionType = randomFrom(VersionType.values()); - updateRequest.versionType(versionType); - if (versionType != VersionType.INTERNAL) { - expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT)); - } - } + setRandomVersion(updateRequest, expectedParams); + setRandomVersionType(updateRequest::versionType, expectedParams); if (randomBoolean()) { int retryOnConflict = randomIntBetween(0, 5); updateRequest.retryOnConflict(retryOnConflict); @@ -519,7 +492,7 @@ public class RequestTests extends ESTestCase { } } - public void testUpdateWithDifferentContentTypes() throws IOException { + public void testUpdateWithDifferentContentTypes() { IllegalStateException exception = expectThrows(IllegalStateException.class, () -> { UpdateRequest updateRequest = new UpdateRequest(); updateRequest.doc(new IndexRequest().source(singletonMap("field", "doc"), XContentType.JSON)); @@ -542,13 +515,7 @@ public class RequestTests extends ESTestCase { expectedParams.put("timeout", BulkShardRequest.DEFAULT_TIMEOUT.getStringRep()); } - if (randomBoolean()) { - WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values()); - bulkRequest.setRefreshPolicy(refreshPolicy); - if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) { - expectedParams.put("refresh", refreshPolicy.getValue()); - } - } + setRandomRefreshPolicy(bulkRequest::setRefreshPolicy, expectedParams); XContentType xContentType = randomFrom(XContentType.JSON, XContentType.SMILE); @@ -561,7 +528,7 @@ public class RequestTests extends ESTestCase { BytesReference source = RandomObjects.randomSource(random(), xContentType); DocWriteRequest.OpType opType = randomFrom(DocWriteRequest.OpType.values()); - DocWriteRequest docWriteRequest = null; + DocWriteRequest docWriteRequest; if (opType == DocWriteRequest.OpType.INDEX) { IndexRequest indexRequest = new IndexRequest(index, type, id).source(source, xContentType); docWriteRequest = indexRequest; @@ -591,6 +558,8 @@ public class RequestTests extends ESTestCase { } } else if (opType == DocWriteRequest.OpType.DELETE) { docWriteRequest = new DeleteRequest(index, type, id); + } else { + throw new UnsupportedOperationException("optype [" + opType + "] not supported"); } if (randomBoolean()) { @@ -995,31 +964,15 @@ public class RequestTests extends ESTestCase { } else { int numIncludes = randomIntBetween(0, 5); String[] includes = new String[numIncludes]; - StringBuilder includesParam = new StringBuilder(); - for (int i = 0; i < numIncludes; i++) { - String include = randomAlphaOfLengthBetween(3, 10); - includes[i] = include; - includesParam.append(include); - if (i < numIncludes - 1) { - includesParam.append(","); - } - } + String includesParam = randomFields(includes); if (numIncludes > 0) { - expectedParams.put("_source_include", includesParam.toString()); + expectedParams.put("_source_include", includesParam); } int numExcludes = randomIntBetween(0, 5); String[] excludes = new String[numExcludes]; - StringBuilder excludesParam = new StringBuilder(); - for (int i = 0; i < numExcludes; i++) { - String exclude = randomAlphaOfLengthBetween(3, 10); - excludes[i] = exclude; - excludesParam.append(exclude); - if (i < numExcludes - 1) { - excludesParam.append(","); - } - } + String excludesParam = randomFields(excludes); if (numExcludes > 0) { - expectedParams.put("_source_exclude", excludesParam.toString()); + expectedParams.put("_source_exclude", excludesParam); } consumer.accept(new FetchSourceContext(true, includes, excludes)); } @@ -1074,10 +1027,10 @@ public class RequestTests extends ESTestCase { } } - private static void setRandomRefreshPolicy(ReplicatedWriteRequest request, Map expectedParams) { + private static void setRandomRefreshPolicy(Consumer setter, Map expectedParams) { if (randomBoolean()) { WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values()); - request.setRefreshPolicy(refreshPolicy); + setter.accept(refreshPolicy); if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) { expectedParams.put("refresh", refreshPolicy.getValue()); } @@ -1094,13 +1047,26 @@ public class RequestTests extends ESTestCase { } } - private static void setRandomVersionType(DocWriteRequest request, Map expectedParams) { + private static void setRandomVersionType(Consumer setter, Map expectedParams) { if (randomBoolean()) { VersionType versionType = randomFrom(VersionType.values()); - request.versionType(versionType); + setter.accept(versionType); if (versionType != VersionType.INTERNAL) { expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT)); } } } + + private static String randomFields(String[] fields) { + StringBuilder excludesParam = new StringBuilder(); + for (int i = 0; i < fields.length; i++) { + String exclude = randomAlphaOfLengthBetween(3, 10); + fields[i] = exclude; + excludesParam.append(exclude); + if (i < fields.length - 1) { + excludesParam.append(","); + } + } + return excludesParam.toString(); + } }