Skip to content

Commit

Permalink
[REST Compatible API] transformations for keys in do (#72823)
Browse files Browse the repository at this point in the history
  • Loading branch information
joegallo committed May 14, 2021
1 parent c3fb60d commit 34ab0c0
Show file tree
Hide file tree
Showing 19 changed files with 146 additions and 1,032 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +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.replaceKeyInDo("do_.some.key_to_replace", "do_.some.key_that_was_replaced")
task.replaceKeyInMatch("match_.some.key_to_replace", "match_.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
Expand All @@ -220,7 +221,7 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
file("distribution/bwc/minor/checkoutDir/src/yamlRestTest/resources/rest-api-spec/test/test.yml" ) << """
"one":
- do:
get:
do_.some.key_to_replace:
index: test
id: 1
warnings:
Expand All @@ -229,7 +230,7 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
- match: { _type: "_foo" }
- match: { _source.blah: 1234 }
- match: { _source.junk: true }
- match: { some.key_to_replace: true }
- match: { match_.some.key_to_replace: true }
- is_true: "value_to_replace"
- is_false: "value_to_replace"
- is_true: "value_not_to_replace"
Expand Down Expand Up @@ -275,7 +276,7 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
---
one:
- do:
get:
do_.some.key_that_was_replaced:
index: "test"
id: 1
warnings:
Expand All @@ -299,7 +300,7 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
- match:
_source.junk: true
- match:
some.key_that_was_replaced : true
match_.some.key_that_was_replaced: true
- is_true: "replaced_value"
- is_false: "replaced_value"
- is_true: "value_not_to_replace"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@
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.do_.ReplaceKeyInDo;
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.ReplaceKeyInMatch;
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 @@ -118,6 +119,16 @@ 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 do section.
* @see ReplaceKeyInDo
* @param oldKeyName the key name directly under do to replace.
* @param newKeyName the new key name directly under do.
*/
public void replaceKeyInDo(String oldKeyName, String newKeyName) {
transformations.add(new ReplaceKeyInDo(oldKeyName, newKeyName, null));
}

/**
* A transformation to replace the key in a length assertion.
* @see ReplaceKeyInLength
Expand Down
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.do_; // 'do' is a reserved word

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 do. For example, change from "do":{"some-thing":{}} to "do":{"some-other-thing":{}}
*/
public class ReplaceKeyInDo extends ReplaceByKey {

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

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

@Override
public void transformTest(ObjectNode doParent) {
ObjectNode doNode = (ObjectNode) doParent.get(getKeyToFind());
JsonNode previousValue = doNode.get(requiredChildKey());
doNode.remove(requiredChildKey());
doNode.set(getNewChildKey(), previousValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

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

Expand All @@ -30,10 +30,10 @@ public String getKeyToFind() {
}

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

import com.fasterxml.jackson.databind.node.ObjectNode;
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 ReplaceKeyInDoTests extends TransformTests {

@Test
public void testReplaceKeyInDo() throws Exception {

String test_original = "/rest/transform/do/replace_key_in_do_original.yml";
List<ObjectNode> tests = getTests(test_original);

String test_transformed = "/rest/transform/do/replace_key_in_do_transformed.yml";
List<ObjectNode> expectedTransformation = getTests(test_transformed);

List<ObjectNode> transformedTests = transformTests(
tests,
Collections.singletonList(new ReplaceKeyInDo("do.key.to_replace", "do.key.replaced", null))
);

AssertObjectNodes.areEqual(transformedTests, expectedTransformation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"First test":

- do:
do.key.to_replace:
something_else.is: true

- do:
and: again

do.key.to_replace:
some_other.thing.is: true

- length: { length.key: 1 }
- match: { _shards.total: 2 }
- match: { _shards.successful: 2 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"First test":

- do:
do.key.replaced:
something_else.is: true

- do:
and: again

do.key.replaced:
some_other.thing.is: true

- length: { length.key: 1 }
- match: { _shards.total: 2 }
- match: { _shards.successful: 2 }
15 changes: 15 additions & 0 deletions x-pack/plugin/watcher/qa/rest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ if (BuildParams.inFipsJvm){
tasks.named("yamlRestTest").configure{enabled = false }
}

tasks.named("transformV7RestTests").configure({ task ->
task.replaceKeyInDo("watcher.ack_watch", "xpack-watcher.ack_watch")
task.replaceKeyInDo("watcher.activate_watch", "xpack-watcher.activate_watch")
task.replaceKeyInDo("watcher.deactivate_watch", "xpack-watcher.deactivate_watch")
task.replaceKeyInDo("watcher.delete_watch", "xpack-watcher.delete_watch")
task.replaceKeyInDo("watcher.execute_watch", "xpack-watcher.execute_watch")
task.replaceKeyInDo("watcher.get_watch", "xpack-watcher.get_watch")
task.replaceKeyInDo("watcher.put_watch", "xpack-watcher.put_watch")
task.replaceKeyInDo("watcher.start", "xpack-watcher.start")
task.replaceKeyInDo("watcher.stats", "xpack-watcher.stats")
task.replaceKeyInDo("watcher.stop", "xpack-watcher.stop")

task.addAllowedWarningRegex(".*_xpack/watcher/.* is deprecated.*")
})

tasks.named("yamlRestCompatTest").configure {
systemProperty 'tests.rest.blacklist', [
'mustache/10_webhook/Test webhook action with mustache integration',
Expand Down

This file was deleted.

0 comments on commit 34ab0c0

Please sign in to comment.