Skip to content

[Rest Api Compatibility] transformations for keys in match and length #72156

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 16 commits into from
May 4, 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 @@ -191,13 +191,13 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {

// avoids a dependency problem in this test, the distribution in use here is inconsequential to the test
import org.elasticsearch.gradle.testclusters.TestDistribution;

dependencies {
yamlRestTestImplementation "junit:junit:4.12"
}
tasks.named("transformV7RestTests").configure({ task ->
task.replaceMatch("_type", "_doc")
task.replaceMatch("_source.values", ["z", "x", "y"], "one")
task.replaceValueInMatch("_type", "_doc")
task.replaceValueInMatch("_source.values", ["z", "x", "y"], "one")
task.removeMatch("_source.blah")
task.removeMatch("_source.junk", "two")
task.addMatch("_source.added", [name: 'jake', likes: 'cheese'], "one")
Expand All @@ -208,6 +208,8 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
task.removeWarning("one", "warning to remove")
task.replaceIsTrue("value_to_replace", "replaced_value")
task.replaceIsFalse("value_to_replace", "replaced_value")
task.replaceKeyInMatch("some.key_to_replace", "some.key_that_was_replaced")
task.replaceKeyInLength("key.in_length_to_replace", "key.in_length_that_was_replaced")
})
// can't actually spin up test cluster from this test
tasks.withType(Test).configureEach{ enabled = false }
Expand All @@ -227,10 +229,12 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
- match: { _type: "_foo" }
- match: { _source.blah: 1234 }
- match: { _source.junk: true }
- match: { some.key_to_replace: true }
- is_true: "value_to_replace"
- is_false: "value_to_replace"
- is_true: "value_not_to_replace"
- is_false: "value_not_to_replace"
- length: { key.in_length_to_replace: 1 }
---
"two":
- do:
Expand Down Expand Up @@ -294,14 +298,18 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
- match: {}
- match:
_source.junk: true
- match:
some.key_that_was_replaced : true
- is_true: "replaced_value"
- is_false: "replaced_value"
- is_true: "value_not_to_replace"
- is_false: "value_not_to_replace"
- length: { key.in_length_that_was_replaced: 1 }
- match:
_source.added:
name: "jake"
likes: "cheese"

---
two:
- do:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
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.headers.InjectHeaders;
import org.elasticsearch.gradle.internal.test.rest.transform.length.ReplaceKeyInLength;
import org.elasticsearch.gradle.internal.test.rest.transform.match.AddMatch;
import org.elasticsearch.gradle.internal.test.rest.transform.match.RemoveMatch;
import org.elasticsearch.gradle.internal.test.rest.transform.match.ReplaceMatch;
import org.elasticsearch.gradle.internal.test.rest.transform.match.ReplaceValueInMatch;
import org.elasticsearch.gradle.internal.test.rest.transform.text.ReplaceIsFalse;
import org.elasticsearch.gradle.internal.test.rest.transform.text.ReplaceIsTrue;
import org.elasticsearch.gradle.internal.test.rest.transform.warnings.InjectAllowedWarnings;
import org.elasticsearch.gradle.internal.test.rest.transform.warnings.InjectWarnings;
import org.elasticsearch.gradle.internal.test.rest.transform.warnings.RemoveWarnings;
import org.elasticsearch.gradle.internal.test.rest.transform.match.ReplaceKeyInMatch;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileSystemOperations;
Expand Down Expand Up @@ -101,13 +103,44 @@ public RestCompatTestTransformTask(
* @param subKey the key name directly under match to replace. For example "_type"
* @param value the value used in the replacement. For example "bar"
*/
public void replaceMatch(String subKey, Object value) {
transformations.add(new ReplaceMatch(subKey, MAPPER.convertValue(value, JsonNode.class)));
public void replaceValueInMatch(String subKey, Object value) {
transformations.add(new ReplaceValueInMatch(subKey, MAPPER.convertValue(value, JsonNode.class)));
}

/**
* Replaces the values of a match assertion for the given REST test. 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"
* @param testName the testName to apply replacement
*/
public void replaceValueInMatch(String subKey, Object value, String testName) {
transformations.add(new ReplaceValueInMatch(subKey, MAPPER.convertValue(value, JsonNode.class), testName));
}

/**
* A transformation to replace the key in a length assertion.
* @see ReplaceKeyInLength
* @param oldKeyName the key name directly under length to replace.
* @param newKeyName the new key name directly under length.
*/
public void replaceKeyInLength(String oldKeyName, String newKeyName) {
transformations.add(new ReplaceKeyInLength(oldKeyName, newKeyName, null));
}

/**
* A transformation to replace the key in a match assertion.
* @see ReplaceKeyInMatch
* @param oldKeyName the key name directly under match to replace.
* @param newKeyName the new key name directly under match.
*/
public void replaceKeyInMatch(String oldKeyName, String newKeyName) {
transformations.add(new ReplaceKeyInMatch(oldKeyName, newKeyName, null));
}

/**
* 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"
* 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
Expand All @@ -117,8 +150,8 @@ public void replaceIsTrue(String oldValue, Object newValue) {
}

/**
* 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"
* 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"
*
* @param oldValue the value that has to match and will be replaced
* @param newValue the value used in the replacement
Expand All @@ -128,14 +161,15 @@ public void replaceIsFalse(String oldValue, Object newValue) {
}

/**
* Replaces the values of a match assertion for the given REST test. For example "match":{"_type": "foo"} to "match":{"_type": "bar"}
* Replaces all the values of a is_false assertion for given REST test.
* For example "is_false": "value_to_replace" to "is_false": "value_replaced"
*
* @param subKey the key name directly under match to replace. For example "_type"
* @param value the value used in the replacement. For example "bar"
* @param testName the testName to apply replacement
* @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 replaceMatch(String subKey, Object value, String testName) {
transformations.add(new ReplaceMatch(subKey, MAPPER.convertValue(value, JsonNode.class), testName));
public void replaceIsFalse(String oldValue, Object newValue, String testName) {
transformations.add(new ReplaceIsFalse(oldValue, MAPPER.convertValue(newValue, TextNode.class), testName));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.internal.test.rest.transform;

import com.fasterxml.jackson.databind.JsonNode;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;

/**
* An abstract common class to handle replacing key and values under a parent object
* This class can be subclass to transform the
* "getKeyToFind": {"requiredChildKey": "foo"}
* into
* "getKeyToFind": {"newChildKey": "getReplacementNode"}
* a getKeyToFind and transformTest would have to be implemented in a subclass
*/
public abstract class ReplaceByKey implements RestTestTransformByParentObject {
private final String requiredChildKey;
private final String newChildKey;
private final JsonNode replacementNode;
private final String testName;

public ReplaceByKey(String requiredChildKey, JsonNode replacementNode) {
this(requiredChildKey, replacementNode, null);
}

public ReplaceByKey(String requiredChildKey, JsonNode replacementNode, String testName) {
this(requiredChildKey, requiredChildKey, replacementNode, testName);
}

public ReplaceByKey(String requiredChildKey, String newChildKey, JsonNode replacementNode, String testName) {
this.requiredChildKey = requiredChildKey;
this.newChildKey = newChildKey;
this.replacementNode = replacementNode;
this.testName = testName;
}

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

@Input
public String getNewChildKey() {
return newChildKey;
}

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

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

@Input
@Optional
public String getTestName() {
return testName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.internal.test.rest.transform.length;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.elasticsearch.gradle.internal.test.rest.transform.ReplaceByKey;
import org.gradle.api.tasks.Internal;

/**
* A transformation to replace the key in a length assertion.
* For example, change from "length ":{"index._type": 1} to "length ":{"index._doc": 1}
*/
public class ReplaceKeyInLength extends ReplaceByKey {

public ReplaceKeyInLength(String replaceKey, String newKeyName, String testName) {
super(replaceKey, newKeyName, null, testName);
}

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

@Override
public void transformTest(ObjectNode matchParent) {
ObjectNode matchNode = (ObjectNode) matchParent.get(getKeyToFind());
JsonNode previousValue = matchNode.get(requiredChildKey());
matchNode.remove(requiredChildKey());
matchNode.set(getNewChildKey(), previousValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.internal.test.rest.transform.match;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.elasticsearch.gradle.internal.test.rest.transform.ReplaceByKey;
import org.gradle.api.tasks.Internal;

/**
* A transformation to replace the key in a match. For example, change from "match":{"index._type": "foo"} to "match":{"index._doc": "foo"}
*/
public class ReplaceKeyInMatch extends ReplaceByKey {

public ReplaceKeyInMatch(String replaceKey, String newKeyName, String testName) {
super(replaceKey, newKeyName, null, testName);
}

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

@Override
public void transformTest(ObjectNode matchParent) {
ObjectNode matchNode = (ObjectNode) matchParent.get(getKeyToFind());
JsonNode previousValue = matchNode.get(requiredChildKey());
matchNode.remove(requiredChildKey());
matchNode.set(getNewChildKey(), previousValue);
}
}

This file was deleted.

Loading