From 8fda74aee15723d69e106aa4c22b29195210c1d2 Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Mon, 7 Aug 2017 10:09:41 +0100 Subject: [PATCH] Adds mutate function for all pipeline aggregation tests (#26058) * Adds mutate function for all metric aggregation tests Relates to #25929 * Fixes review comments --- .../pipeline/InternalSimpleValue.java | 4 ++ .../InternalBucketMetricValue.java | 4 ++ .../percentile/InternalPercentilesBucket.java | 4 ++ .../derivative/InternalDerivative.java | 8 +++ .../pipeline/InternalSimpleValueTests.java | 32 +++++++++- .../InternalBucketMetricValueTests.java | 36 ++++++++++++ .../InternalPercentilesBucketTests.java | 58 +++++++++++++++++++ .../derivative/InternalDerivativeTests.java | 33 +++++++++++ 8 files changed, 178 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/InternalSimpleValue.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/InternalSimpleValue.java index a3c7012f7cd..4fa4f1f6c86 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/InternalSimpleValue.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/InternalSimpleValue.java @@ -71,6 +71,10 @@ public class InternalSimpleValue extends InternalNumericMetricsAggregation.Singl return value; } + DocValueFormat formatter() { + return format; + } + @Override public InternalSimpleValue doReduce(List aggregations, ReduceContext reduceContext) { throw new UnsupportedOperationException("Not supported"); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/InternalBucketMetricValue.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/InternalBucketMetricValue.java index 95b3782bd54..76a240d3178 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/InternalBucketMetricValue.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/InternalBucketMetricValue.java @@ -81,6 +81,10 @@ public class InternalBucketMetricValue extends InternalNumericMetricsAggregation return keys; } + DocValueFormat formatter() { + return format; + } + @Override public InternalAggregation doReduce(List aggregations, ReduceContext reduceContext) { throw new UnsupportedOperationException("Not supported"); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/InternalPercentilesBucket.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/InternalPercentilesBucket.java index a250769f685..42a8b28a51a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/InternalPercentilesBucket.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/InternalPercentilesBucket.java @@ -100,6 +100,10 @@ public class InternalPercentilesBucket extends InternalNumericMetricsAggregation return format.format(percentile(percent)); } + DocValueFormat formatter() { + return format; + } + @Override public Iterator iterator() { return new Iter(percents, percentiles); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/InternalDerivative.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/InternalDerivative.java index db56f0f7c6f..5f6c5793016 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/InternalDerivative.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/InternalDerivative.java @@ -64,6 +64,14 @@ public class InternalDerivative extends InternalSimpleValue implements Derivativ return normalizationFactor > 0 ? (value() / normalizationFactor) : value(); } + DocValueFormat formatter() { + return format; + } + + double getNormalizationFactor() { + return normalizationFactor; + } + @Override public Object getProperty(List path) { if (path.isEmpty()) { diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/InternalSimpleValueTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/InternalSimpleValueTests.java index a996bde0596..09b86c2c98e 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/InternalSimpleValueTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/InternalSimpleValueTests.java @@ -21,10 +21,11 @@ package org.elasticsearch.search.aggregations.pipeline; import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.search.DocValueFormat; -import org.elasticsearch.test.InternalAggregationTestCase; import org.elasticsearch.search.aggregations.ParsedAggregation; +import org.elasticsearch.test.InternalAggregationTestCase; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -67,4 +68,33 @@ public class InternalSimpleValueTests extends InternalAggregationTestCase pipelineAggregators = instance.pipelineAggregators(); + Map metaData = instance.getMetaData(); + switch (between(0, 2)) { + case 0: + name += randomAlphaOfLength(5); + break; + case 1: + value += between(1, 100); + break; + case 2: + if (metaData == null) { + metaData = new HashMap<>(1); + } else { + metaData = new HashMap<>(instance.getMetaData()); + } + metaData.put(randomAlphaOfLength(15), randomInt()); + break; + default: + throw new AssertionError("Illegal randomisation branch"); + } + return new InternalSimpleValue(name, value, formatter, pipelineAggregators, metaData); + } + } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/InternalBucketMetricValueTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/InternalBucketMetricValueTests.java index e58538864a8..b9f7f6d7774 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/InternalBucketMetricValueTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/InternalBucketMetricValueTests.java @@ -20,11 +20,14 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.ParsedAggregation; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.test.InternalAggregationTestCase; +import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -72,4 +75,37 @@ public class InternalBucketMetricValueTests extends InternalAggregationTestCase< assertEquals(parsed.value(), Double.NEGATIVE_INFINITY, 0); } } + + @Override + protected InternalBucketMetricValue mutateInstance(InternalBucketMetricValue instance) { + String name = instance.getName(); + String[] keys = instance.keys(); + double value = instance.value(); + DocValueFormat formatter = instance.formatter(); + List pipelineAggregators = instance.pipelineAggregators(); + Map metaData = instance.getMetaData(); + switch (between(0, 3)) { + case 0: + name += randomAlphaOfLength(5); + break; + case 1: + value += between(1, 100); + break; + case 2: + keys = Arrays.copyOf(keys, keys.length + 1); + keys[keys.length - 1] = randomAlphaOfLengthBetween(1, 20); + break; + case 3: + if (metaData == null) { + metaData = new HashMap<>(1); + } else { + metaData = new HashMap<>(instance.getMetaData()); + } + metaData.put(randomAlphaOfLength(15), randomInt()); + break; + default: + throw new AssertionError("Illegal randomisation branch"); + } + return new InternalBucketMetricValue(name, keys, value, formatter, pipelineAggregators, metaData); + } } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/InternalPercentilesBucketTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/InternalPercentilesBucketTests.java index 0adbafb843f..e3ebd9dc77a 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/InternalPercentilesBucketTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/InternalPercentilesBucketTests.java @@ -28,7 +28,10 @@ import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.test.InternalAggregationTestCase; import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -124,4 +127,59 @@ public class InternalPercentilesBucketTests extends InternalAggregationTestCase< protected Predicate excludePathsFromXContentInsertion() { return path -> path.endsWith(CommonFields.VALUES.getPreferredName()); } + + @Override + protected InternalPercentilesBucket mutateInstance(InternalPercentilesBucket instance) { + String name = instance.getName(); + double[] percents = extractPercents(instance); + double[] percentiles = extractPercentiles(instance); + ; + DocValueFormat formatter = instance.formatter(); + List pipelineAggregators = instance.pipelineAggregators(); + Map metaData = instance.getMetaData(); + switch (between(0, 3)) { + case 0: + name += randomAlphaOfLength(5); + break; + case 1: + percents = Arrays.copyOf(percents, percents.length); + percents[percents.length - 1] = randomDouble(); + break; + case 2: + percentiles = Arrays.copyOf(percentiles, percentiles.length); + percentiles[percentiles.length - 1] = randomDouble(); + break; + case 3: + if (metaData == null) { + metaData = new HashMap<>(1); + } else { + metaData = new HashMap<>(instance.getMetaData()); + } + metaData.put(randomAlphaOfLength(15), randomInt()); + break; + default: + throw new AssertionError("Illegal randomisation branch"); + } + return new InternalPercentilesBucket(name, percents, percentiles, formatter, pipelineAggregators, metaData); + } + + private double[] extractPercentiles(InternalPercentilesBucket instance) { + List values = new ArrayList<>(); + instance.iterator().forEachRemaining(percentile -> values.add(percentile.getValue())); + double[] valuesArray = new double[values.size()]; + for (int i = 0; i < values.size(); i++) { + valuesArray[i] = values.get(i); + } + return valuesArray; + } + + private double[] extractPercents(InternalPercentilesBucket instance) { + List percents = new ArrayList<>(); + instance.iterator().forEachRemaining(percentile -> percents.add(percentile.getPercent())); + double[] percentArray = new double[percents.size()]; + for (int i = 0; i < percents.size(); i++) { + percentArray[i] = percents.get(i); + } + return percentArray; + } } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/derivative/InternalDerivativeTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/derivative/InternalDerivativeTests.java index 58115a7fbb8..76492449eb9 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/derivative/InternalDerivativeTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/derivative/InternalDerivativeTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.test.InternalAggregationTestCase; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -69,4 +70,36 @@ public class InternalDerivativeTests extends InternalAggregationTestCase pipelineAggregators = instance.pipelineAggregators(); + Map metaData = instance.getMetaData(); + switch (between(0, 2)) { + case 0: + name += randomAlphaOfLength(5); + break; + case 1: + value += between(1, 100); + break; + case 2: + normalizationFactor += between(1, 100); + break; + case 3: + if (metaData == null) { + metaData = new HashMap<>(1); + } else { + metaData = new HashMap<>(instance.getMetaData()); + } + metaData.put(randomAlphaOfLength(15), randomInt()); + break; + default: + throw new AssertionError("Illegal randomisation branch"); + } + return new InternalDerivative(name, value, normalizationFactor, formatter, pipelineAggregators, metaData); + } }