diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/compat/compat/RestCompatTestTransformTask.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/compat/compat/RestCompatTestTransformTask.java index a92e605490536..ef93dafa913cd 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/compat/compat/RestCompatTestTransformTask.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/compat/compat/RestCompatTestTransformTask.java @@ -26,6 +26,7 @@ import org.elasticsearch.gradle.VersionProperties; import org.elasticsearch.gradle.internal.test.rest.transform.RestTestTransform; import org.elasticsearch.gradle.internal.test.rest.transform.RestTestTransformer; +import org.elasticsearch.gradle.internal.test.rest.transform.close_to.ReplaceValueInCloseTo; import org.elasticsearch.gradle.internal.test.rest.transform.do_.ReplaceKeyInDo; import org.elasticsearch.gradle.internal.test.rest.transform.headers.InjectHeaders; import org.elasticsearch.gradle.internal.test.rest.transform.length.ReplaceKeyInLength; @@ -253,7 +254,30 @@ public void replaceKeyInMatch(String oldKeyName, String newKeyName) { } /** - * Replaces all the values of a is_true assertion for all project REST tests. + * Replaces the value of the `value` of a close_to assertion for a given REST tests. + * For example: close_to: { get.fields._routing: { value: 5.1, error: 0.00001 } } + * to close_to: { get.fields._routing: { value: 9.5, error: 0.00001 } } + * @param subKey the key name directly under close_to to replace. For example "get.fields._routing" + * @param newValue the value used in the replacement. For example 9.5 + * @param testName the testName to apply replacement + */ + public void replaceValueInCloseTo(String subKey, double newValue, String testName) { + getTransformations().add(new ReplaceValueInCloseTo(subKey, MAPPER.convertValue(newValue, NumericNode.class), testName)); + } + + /** + * Replaces the value of the `value` of a close_to assertion for all project REST tests. + * For example: close_to: { get.fields._routing: { value: 5.1, error: 0.00001 } } + * to close_to: { get.fields._routing: { value: 9.5, error: 0.00001 } } + * @param subKey the key name directly under close_to to replace. For example "get.fields._routing" + * @param newValue the value used in the replacement. For example 9.5 + */ + public void replaceValueInCloseTo(String subKey, double newValue) { + getTransformations().add(new ReplaceValueInCloseTo(subKey, MAPPER.convertValue(newValue, NumericNode.class))); + } + + /** + * Replaces all the values of is_true assertion for all project REST tests. * For example "is_true": "value_to_replace" to "is_true": "value_replaced" * * @param oldValue the value that has to match and will be replaced @@ -263,6 +287,18 @@ public void replaceIsTrue(String oldValue, Object newValue) { getTransformations().add(new ReplaceIsTrue(oldValue, MAPPER.convertValue(newValue, TextNode.class))); } + /** + * Replaces all the values of is_true assertion for given REST test. + * For example "is_true": "value_to_replace" to "is_true": "value_replaced" + * + * @param oldValue the value that has to match and will be replaced + * @param newValue the value used in the replacement + * @param testName the testName to apply replacement + */ + public void replaceIsTrue(String oldValue, Object newValue, String testName) { + getTransformations().add(new ReplaceIsTrue(oldValue, MAPPER.convertValue(newValue, TextNode.class), testName)); + } + /** * Replaces all the values of a is_false assertion for all project REST tests. * For example "is_false": "value_to_replace" to "is_false": "value_replaced" diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/close_to/ReplaceValueInCloseTo.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/close_to/ReplaceValueInCloseTo.java new file mode 100644 index 0000000000000..96561c3cf5444 --- /dev/null +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/close_to/ReplaceValueInCloseTo.java @@ -0,0 +1,46 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +package org.elasticsearch.gradle.internal.test.rest.transform.close_to; + +import com.fasterxml.jackson.databind.node.NumericNode; +import com.fasterxml.jackson.databind.node.ObjectNode; + +import org.elasticsearch.gradle.internal.test.rest.transform.ReplaceByKey; +import org.gradle.api.tasks.Internal; + +/** + * Replaces the value of the `value` of a close_to assertion for a given sub-node. + * For example: close_to: { get.fields._routing: { value: 5.1, error: 0.00001 } } + * to close_to: { get.fields._routing: { value: 9.5, error: 0.00001 } } + */ +public class ReplaceValueInCloseTo extends ReplaceByKey { + + public ReplaceValueInCloseTo(String replaceKey, NumericNode replacementNode) { + this(replaceKey, replacementNode, null); + } + + public ReplaceValueInCloseTo(String replaceKey, NumericNode replacementNode, String testName) { + super(replaceKey, replaceKey, replacementNode, testName); + } + + @Override + @Internal + public String getKeyToFind() { + return "close_to"; + } + + @Override + public void transformTest(ObjectNode matchParent) { + ObjectNode closeToNode = (ObjectNode) matchParent.get(getKeyToFind()); + ObjectNode subNode = (ObjectNode) closeToNode.get(requiredChildKey()); + subNode.remove("value"); + subNode.set("value", getReplacementNode()); + } +} diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/text/ReplaceIsTrue.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/text/ReplaceIsTrue.java index 4e8cfe1172768..51db9c88774b7 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/text/ReplaceIsTrue.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/text/ReplaceIsTrue.java @@ -15,4 +15,8 @@ public class ReplaceIsTrue extends ReplaceTextual { public ReplaceIsTrue(String valueToBeReplaced, TextNode replacementNode) { super("is_true", valueToBeReplaced, replacementNode); } + + public ReplaceIsTrue(String valueToBeReplaced, TextNode replacementNode, String testName) { + super("is_true", valueToBeReplaced, replacementNode, testName); + } } diff --git a/build-tools-internal/src/test/java/org/elasticsearch/gradle/internal/test/rest/transform/close_to/ReplaceValueInCloseToTests.java b/build-tools-internal/src/test/java/org/elasticsearch/gradle/internal/test/rest/transform/close_to/ReplaceValueInCloseToTests.java new file mode 100644 index 0000000000000..27f7895f278e5 --- /dev/null +++ b/build-tools-internal/src/test/java/org/elasticsearch/gradle/internal/test/rest/transform/close_to/ReplaceValueInCloseToTests.java @@ -0,0 +1,46 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +package org.elasticsearch.gradle.internal.test.rest.transform.close_to; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.NumericNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; + +import org.elasticsearch.gradle.internal.test.rest.transform.AssertObjectNodes; +import org.elasticsearch.gradle.internal.test.rest.transform.TransformTests; +import org.junit.Test; + +import java.util.Collections; +import java.util.List; + +public class ReplaceValueInCloseToTests extends TransformTests { + + private static final YAMLFactory YAML_FACTORY = new YAMLFactory(); + private static final ObjectMapper MAPPER = new ObjectMapper(YAML_FACTORY); + + @Test + public void testReplaceValue() throws Exception { + String test_original = "/rest/transform/close_to/close_to_replace_original.yml"; + List tests = getTests(test_original); + + String test_transformed = "/rest/transform/close_to/close_to_replace_transformed_value.yml"; + List expectedTransformation = getTests(test_transformed); + + NumericNode replacementNode = MAPPER.convertValue(99.99, NumericNode.class); + + List transformedTests = transformTests( + tests, + Collections.singletonList(new ReplaceValueInCloseTo("aggregations.tsids.buckets.0.voltage.value", replacementNode, null)) + ); + + AssertObjectNodes.areEqual(transformedTests, expectedTransformation); + } +} diff --git a/build-tools-internal/src/test/resources/rest/transform/close_to/close_to_replace_original.yml b/build-tools-internal/src/test/resources/rest/transform/close_to/close_to_replace_original.yml new file mode 100644 index 0000000000000..ffd7eab038062 --- /dev/null +++ b/build-tools-internal/src/test/resources/rest/transform/close_to/close_to_replace_original.yml @@ -0,0 +1,26 @@ +--- +close_to test: + - do: + search: + index: test + body: + size: 0 + aggs: + tsids: + terms: + field: _tsid + order: + _key: asc + aggs: + voltage: + avg: + field: voltage + + - match: {hits.total.value: 4} + - length: {aggregations.tsids.buckets: 2} + - match: {aggregations.tsids.buckets.0.key: "KDODRmbj7vu4rLWvjrJbpUuaET_vOYoRw6ImzKEcF4sEaGKnXSaKfM0" } + - match: {aggregations.tsids.buckets.0.doc_count: 2 } + - close_to: {aggregations.tsids.buckets.0.voltage.value: { value: 6.7, error: 0.01 }} + - match: { aggregations.tsids.buckets.1.key: "KDODRmbj7vu4rLWvjrJbpUvcUWJEddqA4Seo8jbBBBFxwC0lrefCb6A" } + - match: {aggregations.tsids.buckets.1.doc_count: 2 } + - close_to: {aggregations.tsids.buckets.1.voltage.value: { value: 7.30, error: 0.01 }} diff --git a/build-tools-internal/src/test/resources/rest/transform/close_to/close_to_replace_transformed_value.yml b/build-tools-internal/src/test/resources/rest/transform/close_to/close_to_replace_transformed_value.yml new file mode 100644 index 0000000000000..b2a93004ba780 --- /dev/null +++ b/build-tools-internal/src/test/resources/rest/transform/close_to/close_to_replace_transformed_value.yml @@ -0,0 +1,26 @@ +--- +close_to test: + - do: + search: + index: test + body: + size: 0 + aggs: + tsids: + terms: + field: _tsid + order: + _key: asc + aggs: + voltage: + avg: + field: voltage + + - match: {hits.total.value: 4} + - length: {aggregations.tsids.buckets: 2} + - match: {aggregations.tsids.buckets.0.key: "KDODRmbj7vu4rLWvjrJbpUuaET_vOYoRw6ImzKEcF4sEaGKnXSaKfM0" } + - match: {aggregations.tsids.buckets.0.doc_count: 2 } + - close_to: {aggregations.tsids.buckets.0.voltage.value: { value: 99.99, error: 0.01 }} + - match: { aggregations.tsids.buckets.1.key: "KDODRmbj7vu4rLWvjrJbpUvcUWJEddqA4Seo8jbBBBFxwC0lrefCb6A" } + - match: {aggregations.tsids.buckets.1.doc_count: 2 } + - close_to: {aggregations.tsids.buckets.1.voltage.value: { value: 7.30, error: 0.01 }}