Skip to content

[Rest Api Compatibility] Add transformation for simple text replaces #71118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
task.addAllowedWarning("added allowed warning")
task.addAllowedWarningRegex("added allowed warning regex .* [0-9]")
task.removeWarning("one", "warning to remove")
task.replaceIsTrue("value_to_replace", "replaced_value")
task.replaceIsFalse("value_to_replace", "replaced_value")
})
// can't actually spin up test cluster from this test
tasks.withType(Test).configureEach{ enabled = false }
Expand All @@ -235,6 +237,10 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
- match: { _type: "_foo" }
- match: { _source.blah: 1234 }
- match: { _source.junk: true }
- is_true: "value_to_replace"
- is_false: "value_to_replace"
- is_true: "value_not_to_replace"
- is_false: "value_not_to_replace"
---
"two":
- do:
Expand All @@ -245,6 +251,10 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
- match: { _type: "_foo" }
- match: { _source.blah: 1234 }
- match: { _source.junk: true }
- is_true: "value_to_replace"
- is_false: "value_to_replace"
- is_true: "value_not_to_replace"
- is_false: "value_not_to_replace"

""".stripIndent()
when:
Expand Down Expand Up @@ -294,6 +304,10 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
- match: {}
- match:
_source.junk: true
- is_true: "replaced_value"
- is_false: "replaced_value"
- is_true: "value_not_to_replace"
- is_false: "value_not_to_replace"
- match:
_source.added:
name: "jake"
Expand All @@ -320,6 +334,10 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
_type: "_doc"
- match: {}
- match: {}
- is_true: "replaced_value"
- is_false: "replaced_value"
- is_true: "value_not_to_replace"
- is_false: "value_not_to_replace"
""".stripIndent()).readAll()

expectedAll.eachWithIndex{ ObjectNode expected, int i ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SequenceWriter;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;
import org.elasticsearch.gradle.Version;
Expand All @@ -24,6 +25,8 @@
import org.elasticsearch.gradle.test.rest.transform.match.AddMatch;
import org.elasticsearch.gradle.test.rest.transform.match.RemoveMatch;
import org.elasticsearch.gradle.test.rest.transform.match.ReplaceMatch;
import org.elasticsearch.gradle.test.rest.transform.text.ReplaceIsFalse;
import org.elasticsearch.gradle.test.rest.transform.text.ReplaceIsTrue;
import org.elasticsearch.gradle.test.rest.transform.warnings.InjectAllowedWarnings;
import org.elasticsearch.gradle.test.rest.transform.warnings.InjectWarnings;
import org.elasticsearch.gradle.test.rest.transform.warnings.RemoveWarnings;
Expand Down Expand Up @@ -92,7 +95,8 @@ public RestCompatTestTransformTask(
}

/**
* Replaces all the values of a match assertion all project REST tests. For example "match":{"_type": "foo"} to "match":{"_type": "bar"}
* Replaces all the values of a match assertion for all project REST tests.
* For example "match":{"_type": "foo"} to "match":{"_type": "bar"}
*
* @param subKey the key name directly under match to replace. For example "_type"
* @param value the value used in the replacement. For example "bar"
Expand All @@ -101,6 +105,28 @@ public void replaceMatch(String subKey, Object value) {
transformations.add(new ReplaceMatch(subKey, MAPPER.convertValue(value, JsonNode.class)));
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a Gradle test for these 2 to YamlRestCompatTestPluginFuncTest / "transform task executes and works as configured" ?

When updating these just add the task configuration to the build file, the thing you want to change to the test.yml. The test should fail and output the result. In the past I just copy/paste that output and ensure that the diff looks correct (e.g. the only thing that changed is what is expected).

* Replaces all the values of a is_true assertion for all project REST tests.
* For example "is_true": "value_to_replace" to "match": "value_replaced"
*
* @param oldValue the value that has to match and will be replaced
* @param newValue the value used in the replacement
*/
public void replaceIsTrue(String oldValue, Object newValue) {
transformations.add(new ReplaceIsTrue(oldValue, MAPPER.convertValue(newValue, TextNode.class)));
}

/**
* Replaces all the values of a is_true assertion for all project REST tests.
* For example "is_false": "value_to_replace" to "match": "value_replaced"
*
* @param oldValue the value that has to match and will be replaced
* @param newValue the value used in the replacement
*/
public void replaceIsFalse(String oldValue, Object newValue) {
transformations.add(new ReplaceIsFalse(oldValue, MAPPER.convertValue(newValue, TextNode.class)));
}

/**
* Replaces the values of a match assertion for the given REST test. For example "match":{"_type": "foo"} to "match":{"_type": "bar"}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.elasticsearch.gradle.test.rest.transform;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
Expand All @@ -27,4 +28,12 @@ public interface RestTestTransformByParentObject extends RestTestTransform<Objec
default String requiredChildKey() {
return null;
}

/**
* @param child a node on which the transformation will be applied.
* @return true if the transformation should be applied on child node, otherwise false.
*/
default boolean matches(JsonNode child) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Javadoc please.

return child.has(requiredChildKey());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;

import java.util.Iterator;
import java.util.LinkedList;
Expand Down Expand Up @@ -144,9 +145,15 @@ private void traverseTest(
} else {
if (entry.getValue().isObject()) {
ObjectNode child = (ObjectNode) entry.getValue();
if (child.has(transform.requiredChildKey())) {
if (transform.matches(child)) {
transform.transformTest((ObjectNode) currentNode);
}
} else if (entry.getValue().isTextual()) {
TextNode value = (TextNode) entry.getValue();
if (transform.matches(value)) {
transform.transformTest((ObjectNode) currentNode);
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.gradle.test.rest.transform.text;

import com.fasterxml.jackson.databind.node.TextNode;

public class ReplaceIsFalse extends ReplaceTextual {
public ReplaceIsFalse(String valueToBeReplaced, TextNode replacementNode) {
super("is_false", valueToBeReplaced, replacementNode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.gradle.test.rest.transform.text;

import com.fasterxml.jackson.databind.node.TextNode;

public class ReplaceIsTrue extends ReplaceTextual {
public ReplaceIsTrue(String valueToBeReplaced, TextNode replacementNode) {
super("is_true", valueToBeReplaced, replacementNode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.gradle.test.rest.transform.text;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import org.elasticsearch.gradle.test.rest.transform.RestTestContext;
import org.elasticsearch.gradle.test.rest.transform.RestTestTransformByParentObject;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;

/**
* A transformation to replace the flat textual fields.
*/
class ReplaceTextual implements RestTestTransformByParentObject {
private final String keyToReplaceName;
private final String valueToBeReplaced;
private final TextNode replacementNode;
private final String testName;

ReplaceTextual(String keyToReplaceName, String valueToBeReplaced, TextNode replacementNode) {
this.keyToReplaceName = keyToReplaceName;
this.valueToBeReplaced = valueToBeReplaced;
this.replacementNode = replacementNode;
this.testName = null;
}

ReplaceTextual(String keyToReplaceName, String valueToBeReplaced, TextNode replacementNode, String testName) {
this.keyToReplaceName = keyToReplaceName;
this.valueToBeReplaced = valueToBeReplaced;
this.replacementNode = replacementNode;
this.testName = testName;
}

@Override
@Internal
public String getKeyToFind() {
return keyToReplaceName;
}

@Override
public String requiredChildKey() {
return valueToBeReplaced;
}

@Override
public boolean shouldApply(RestTestContext testContext) {
return testName == null || testContext.getTestName().equals(testName);
}

@Override
public void transformTest(ObjectNode matchParent) {
matchParent.set(getKeyToFind(), replacementNode);
}

@Input
public String getValueToBeReplaced() {
return valueToBeReplaced;
}

@Input
public JsonNode getReplacementNode() {
return replacementNode;
}

@Input
@Optional
public String getTestName() {
return testName;
}

@Override
public boolean matches(JsonNode child) {
return child.asText().equals(requiredChildKey());
}

}
Loading