From 87c6e63e73a579440578ee0922413d56966faa7e Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Wed, 2 Aug 2017 11:38:31 +0100 Subject: [PATCH] Adds mutate function to various tests (#25999) * Adds mutate function to various tests Relates to #25929 * fix test * implements mutate function for all single bucket aggs * review comments * convert getMutateFunction to mutateIInstance --- .../aggregations/InternalAggregation.java | 6 ++ .../fieldcaps/FieldCapabilitiesTests.java | 71 +++++++++++++++++++ .../index/refresh/RefreshStatsTests.java | 26 ++++++- .../script/ScriptMetaDataTests.java | 15 +++- .../script/StoredScriptSourceTests.java | 32 +++++++++ .../bucket/filter/InternalFiltersTests.java | 28 ++++++++ .../search/collapse/CollapseBuilderTests.java | 26 +++++++ ...epositoriesMetaDataSerializationTests.java | 12 ++++ ...SnapshotsInProgressSerializationTests.java | 12 ++++ .../stats/InternalMatrixStatsTests.java | 42 ++++++++++- ...ternalSingleBucketAggregationTestCase.java | 33 +++++++++ .../test/AbstractWireSerializingTestCase.java | 20 ++---- 12 files changed, 300 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregation.java b/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregation.java index 2143e4f14ab..b1652d60365 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregation.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregation.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.search.aggregations; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.NamedWriteable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -234,4 +235,9 @@ public abstract class InternalAggregation implements Aggregation, ToXContent, Na return this == obj; } + @Override + public String toString() { + return Strings.toString(this); + } + } diff --git a/core/src/test/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesTests.java b/core/src/test/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesTests.java index b485d51bd6a..53c27645bf2 100644 --- a/core/src/test/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesTests.java +++ b/core/src/test/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesTests.java @@ -22,6 +22,8 @@ package org.elasticsearch.action.fieldcaps; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import java.util.Arrays; + import static org.hamcrest.Matchers.equalTo; public class FieldCapabilitiesTests extends AbstractWireSerializingTestCase { @@ -106,4 +108,73 @@ public class FieldCapabilitiesTests extends AbstractWireSerializingTestCase { @Override @@ -34,4 +32,26 @@ public class RefreshStatsTests extends AbstractStreamableTestCase protected RefreshStats createBlankInstance() { return new RefreshStats(); } + + @Override + protected MutateFunction getMutateFunction() { + return instance -> { + long total = instance.getTotal(); + long totalInMillis = instance.getTotalTimeInMillis(); + int listeners = instance.getListeners(); + switch (randomInt(2)) { + case 0: + total += between(1, 2000); + break; + case 1: + totalInMillis += between(1, 2000); + break; + case 2: + default: + listeners += between(1, 2000); + break; + } + return new RefreshStats(total, totalInMillis, listeners); + }; + } } diff --git a/core/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java b/core/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java index 1c7ef2de075..2200c013a52 100644 --- a/core/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java +++ b/core/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java @@ -120,9 +120,9 @@ public class ScriptMetaDataTests extends AbstractSerializingTestCase options = instance.getOptions(); + XContentType newXContentType = randomFrom(XContentType.JSON, XContentType.YAML); + XContentBuilder newTemplate = XContentBuilder.builder(newXContentType.xContent()); + newTemplate.startObject(); + newTemplate.startObject("query"); + newTemplate.startObject("match"); + newTemplate.field("body", "{{query_string}}"); + newTemplate.endObject(); + newTemplate.endObject(); + newTemplate.endObject(); + + switch (between(0, 3)) { + case 0: + source = newTemplate.string(); + break; + case 1: + lang = randomAlphaOfLengthBetween(1, 20); + break; + case 2: + options = new HashMap<>(options); + options.put(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); + break; + case 3: + default: + return new StoredScriptSource(newTemplate.string()); + } + return new StoredScriptSource(lang, source, options); + } } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/InternalFiltersTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/InternalFiltersTests.java index 30755b29919..528cf54566b 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/InternalFiltersTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/InternalFiltersTests.java @@ -23,9 +23,11 @@ import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.aggregations.InternalMultiBucketAggregationTestCase; import org.elasticsearch.search.aggregations.ParsedMultiBucketAggregation; +import org.elasticsearch.search.aggregations.bucket.filter.InternalFilters.InternalBucket; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -90,4 +92,30 @@ public class InternalFiltersTests extends InternalMultiBucketAggregationTestCase return ParsedFilters.class; } + @Override + protected InternalFilters mutateInstance(InternalFilters instance) { + String name = instance.getName(); + List buckets = instance.getBuckets(); + List pipelineAggregators = instance.pipelineAggregators(); + Map metaData = instance.getMetaData(); + switch (between(0, 2)) { + case 0: + name += randomAlphaOfLength(5); + break; + case 1: + buckets = new ArrayList<>(buckets); + buckets.add(new InternalFilters.InternalBucket("test", randomIntBetween(0, 1000), InternalAggregations.EMPTY, keyed)); + break; + case 2: + default: + if (metaData == null) { + metaData = new HashMap<>(1); + } else { + metaData = new HashMap<>(instance.getMetaData()); + } + metaData.put(randomAlphaOfLength(15), randomInt()); + break; + } + return new InternalFilters(name, buckets, keyed, pipelineAggregators, metaData); + } } diff --git a/core/src/test/java/org/elasticsearch/search/collapse/CollapseBuilderTests.java b/core/src/test/java/org/elasticsearch/search/collapse/CollapseBuilderTests.java index 734db838bd2..f31eb0b490f 100644 --- a/core/src/test/java/org/elasticsearch/search/collapse/CollapseBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/collapse/CollapseBuilderTests.java @@ -102,6 +102,32 @@ public class CollapseBuilderTests extends AbstractSerializingTestCase innerHits = newBuilder.getInnerHits(); + for (int i = 0; i < between(1, 5); i++) { + innerHits.add(InnerHitBuilderTests.randomInnerHits()); + } + newBuilder.setInnerHits(innerHits); + break; + } + return newBuilder; + } + @Override protected NamedWriteableRegistry getNamedWriteableRegistry() { return namedWriteableRegistry; diff --git a/core/src/test/java/org/elasticsearch/snapshots/RepositoriesMetaDataSerializationTests.java b/core/src/test/java/org/elasticsearch/snapshots/RepositoriesMetaDataSerializationTests.java index 8f027b66950..dc0c7b55c6d 100644 --- a/core/src/test/java/org/elasticsearch/snapshots/RepositoriesMetaDataSerializationTests.java +++ b/core/src/test/java/org/elasticsearch/snapshots/RepositoriesMetaDataSerializationTests.java @@ -53,6 +53,18 @@ public class RepositoriesMetaDataSerializationTests extends AbstractDiffableSeri return RepositoriesMetaData::new; } + @Override + protected Custom mutateInstance(Custom instance) { + List entries = new ArrayList<>(((RepositoriesMetaData) instance).repositories()); + boolean addEntry = entries.isEmpty() ? true : randomBoolean(); + if (addEntry) { + entries.add(new RepositoryMetaData(randomAlphaOfLength(10), randomAlphaOfLength(10), randomSettings())); + } else { + entries.remove(randomIntBetween(0, entries.size() - 1)); + } + return new RepositoriesMetaData(entries.toArray(new RepositoryMetaData[entries.size()])); + } + public Settings randomSettings() { if (randomBoolean()) { return Settings.EMPTY; diff --git a/core/src/test/java/org/elasticsearch/snapshots/SnapshotsInProgressSerializationTests.java b/core/src/test/java/org/elasticsearch/snapshots/SnapshotsInProgressSerializationTests.java index f376e205d19..3f23c8f0a2d 100644 --- a/core/src/test/java/org/elasticsearch/snapshots/SnapshotsInProgressSerializationTests.java +++ b/core/src/test/java/org/elasticsearch/snapshots/SnapshotsInProgressSerializationTests.java @@ -115,4 +115,16 @@ public class SnapshotsInProgressSerializationTests extends AbstractDiffableWireS return new NamedWriteableRegistry(ClusterModule.getNamedWriteables()); } + @Override + protected Custom mutateInstance(Custom instance) { + List entries = new ArrayList<>(((SnapshotsInProgress) instance).entries()); + boolean addEntry = entries.isEmpty() ? true : randomBoolean(); + if (addEntry) { + entries.add(randomSnapshot()); + } else { + entries.remove(randomIntBetween(0, entries.size() - 1)); + } + return new SnapshotsInProgress(entries); + } + } diff --git a/modules/aggs-matrix-stats/src/test/java/org/elasticsearch/search/aggregations/matrix/stats/InternalMatrixStatsTests.java b/modules/aggs-matrix-stats/src/test/java/org/elasticsearch/search/aggregations/matrix/stats/InternalMatrixStatsTests.java index 497f3acb7b3..74238e69e57 100644 --- a/modules/aggs-matrix-stats/src/test/java/org/elasticsearch/search/aggregations/matrix/stats/InternalMatrixStatsTests.java +++ b/modules/aggs-matrix-stats/src/test/java/org/elasticsearch/search/aggregations/matrix/stats/InternalMatrixStatsTests.java @@ -37,6 +37,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Predicate; @@ -76,7 +77,7 @@ public class InternalMatrixStatsTests extends InternalAggregationTestCase metaData = instance.getMetaData(); + switch (between(0, 3)) { + case 0: + name += randomAlphaOfLength(5); + break; + case 1: + double[] values = new double[fields.length]; + for (int i = 0; i < fields.length; i++) { + values[i] = randomDouble() * 200; + } + runningStats = new RunningStats(); + runningStats.add(fields, values); + break; + case 2: + if (matrixStatsResults == null) { + matrixStatsResults = new MatrixStatsResults(runningStats); + } else { + matrixStatsResults = null; + } + break; + case 3: + default: + if (metaData == null) { + metaData = new HashMap<>(1); + } else { + metaData = new HashMap<>(instance.getMetaData()); + } + metaData.put(randomAlphaOfLength(15), randomInt()); + break; + } + return new InternalMatrixStats(name, docCount, runningStats, matrixStatsResults, Collections.emptyList(), metaData); + } + @Override public void testReduceRandom() { int numValues = 10000; diff --git a/test/framework/src/main/java/org/elasticsearch/search/aggregations/InternalSingleBucketAggregationTestCase.java b/test/framework/src/main/java/org/elasticsearch/search/aggregations/InternalSingleBucketAggregationTestCase.java index 477f942f6f1..56a4bc983ca 100644 --- a/test/framework/src/main/java/org/elasticsearch/search/aggregations/InternalSingleBucketAggregationTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/search/aggregations/InternalSingleBucketAggregationTestCase.java @@ -79,6 +79,39 @@ public abstract class InternalSingleBucketAggregationTestCase pipelineAggregators = instance.pipelineAggregators(); + Map metaData = instance.getMetaData(); + switch (between(0, 3)) { + case 0: + name += randomAlphaOfLength(5); + break; + case 1: + docCount += between(1, 2000); + break; + case 2: + List aggs = new ArrayList<>(); + aggs.add(new InternalMax("new_max", randomDouble(), randomNumericDocValueFormat(), emptyList(), emptyMap())); + aggs.add(new InternalMin("new_min", randomDouble(), randomNumericDocValueFormat(), emptyList(), emptyMap())); + aggregations = new InternalAggregations(aggs); + break; + case 3: + default: + if (metaData == null) { + metaData = new HashMap<>(1); + } else { + metaData = new HashMap<>(instance.getMetaData()); + } + metaData.put(randomAlphaOfLength(15), randomInt()); + break; + } + return createTestInstance(name, docCount, aggregations, pipelineAggregators, metaData); + } + @Override protected final void assertReduced(T reduced, List inputs) { assertEquals(inputs.stream().mapToLong(InternalSingleBucketAggregation::getDocCount).sum(), reduced.getDocCount()); diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractWireSerializingTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractWireSerializingTestCase.java index 037b58a3a58..1166ae170c2 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractWireSerializingTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractWireSerializingTestCase.java @@ -26,8 +26,6 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.test.EqualsHashCodeTestUtils.CopyFunction; -import org.elasticsearch.test.EqualsHashCodeTestUtils.MutateFunction; import java.io.IOException; import java.util.Collections; @@ -48,21 +46,11 @@ public abstract class AbstractWireSerializingTestCase exten protected abstract Reader instanceReader(); /** - * Returns a {@link CopyFunction} that can be used to make an exact copy of - * the given instance. This defaults to a function that uses - * {@link #copyInstance(Writeable)} to create the copy. - */ - protected CopyFunction getCopyFunction() { - return (original) -> copyInstance(original); - } - - /** - * Returns a {@link MutateFunction} that can be used to make create a copy - * of the given instance that is different to this instance. This defaults - * to null. + * Returns an instance which is mutated slightly so it should not be equal + * to the given instance. */ // TODO: Make this abstract when all sub-classes implement this (https://github.com/elastic/elasticsearch/issues/25929) - protected MutateFunction getMutateFunction() { + protected T mutateInstance(T instance) throws IOException { return null; } @@ -72,7 +60,7 @@ public abstract class AbstractWireSerializingTestCase exten */ public void testEqualsAndHashcode() throws IOException { for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) { - EqualsHashCodeTestUtils.checkEqualsAndHashCode(createTestInstance(), getCopyFunction(), getMutateFunction()); + EqualsHashCodeTestUtils.checkEqualsAndHashCode(createTestInstance(), this::copyInstance, this::mutateInstance); } }