Skip to content

Commit 68bc5d5

Browse files
committed
Rework RestCompatTestTransformTask to CC compatibility
To keep supporting configuration cache we need to avoid Serializing JsonNode instances
1 parent 33c9309 commit 68bc5d5

File tree

15 files changed

+126
-62
lines changed

15 files changed

+126
-62
lines changed

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/compat/compat/RestCompatTestTransformTask.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.gradle.VersionProperties;
2727
import org.elasticsearch.gradle.internal.test.rest.transform.RestTestTransform;
2828
import org.elasticsearch.gradle.internal.test.rest.transform.RestTestTransformer;
29+
import org.elasticsearch.gradle.internal.test.rest.transform.SerializableJsonNode;
2930
import org.elasticsearch.gradle.internal.test.rest.transform.close_to.ReplaceValueInCloseTo;
3031
import org.elasticsearch.gradle.internal.test.rest.transform.do_.ReplaceKeyInDo;
3132
import org.elasticsearch.gradle.internal.test.rest.transform.headers.InjectHeaders;
@@ -169,7 +170,7 @@ public void skipTestsByFilePattern(String filePattern, String reason) {
169170
* @param value the value used in the replacement. For example "bar"
170171
*/
171172
public void replaceValueInMatch(String subKey, Object value) {
172-
getTransformations().add(new ReplaceValueInMatch(subKey, MAPPER.convertValue(value, JsonNode.class)));
173+
getTransformations().add(new ReplaceValueInMatch(subKey, SerializableJsonNode.of(value, JsonNode.class)));
173174
}
174175

175176
/**
@@ -180,7 +181,7 @@ public void replaceValueInMatch(String subKey, Object value) {
180181
* @param testName the testName to apply replacement
181182
*/
182183
public void replaceValueInMatch(String subKey, Object value, String testName) {
183-
getTransformations().add(new ReplaceValueInMatch(subKey, MAPPER.convertValue(value, JsonNode.class), testName));
184+
getTransformations().add(new ReplaceValueInMatch(subKey, SerializableJsonNode.of(value, JsonNode.class), testName));
184185
}
185186

186187
/**
@@ -225,7 +226,7 @@ public void replaceKeyInLength(String oldKeyName, String newKeyName) {
225226
* @param value the value used in the replacement. For example 99
226227
*/
227228
public void replaceValueInLength(String subKey, int value) {
228-
getTransformations().add(new ReplaceValueInLength(subKey, MAPPER.convertValue(value, NumericNode.class)));
229+
getTransformations().add(new ReplaceValueInLength(subKey, SerializableJsonNode.of(value, NumericNode.class)));
229230
}
230231

231232
/**
@@ -237,7 +238,7 @@ public void replaceValueInLength(String subKey, int value) {
237238
* @param testName the testName to apply replacement
238239
*/
239240
public void replaceValueInLength(String subKey, int value, String testName) {
240-
getTransformations().add(new ReplaceValueInLength(subKey, MAPPER.convertValue(value, NumericNode.class), testName));
241+
getTransformations().add(new ReplaceValueInLength(subKey, SerializableJsonNode.of(value, NumericNode.class), testName));
241242
}
242243

243244
/**
@@ -260,7 +261,7 @@ public void replaceKeyInMatch(String oldKeyName, String newKeyName) {
260261
* @param testName the testName to apply replacement
261262
*/
262263
public void replaceValueInCloseTo(String subKey, double newValue, String testName) {
263-
getTransformations().add(new ReplaceValueInCloseTo(subKey, MAPPER.convertValue(newValue, NumericNode.class), testName));
264+
getTransformations().add(new ReplaceValueInCloseTo(subKey, SerializableJsonNode.of(newValue, NumericNode.class), testName));
264265
}
265266

266267
/**
@@ -271,7 +272,7 @@ public void replaceValueInCloseTo(String subKey, double newValue, String testNam
271272
* @param newValue the value used in the replacement. For example 9.5
272273
*/
273274
public void replaceValueInCloseTo(String subKey, double newValue) {
274-
getTransformations().add(new ReplaceValueInCloseTo(subKey, MAPPER.convertValue(newValue, NumericNode.class)));
275+
getTransformations().add(new ReplaceValueInCloseTo(subKey, SerializableJsonNode.of(newValue, NumericNode.class)));
275276
}
276277

277278
/**
@@ -282,7 +283,7 @@ public void replaceValueInCloseTo(String subKey, double newValue) {
282283
* @param newValue the value used in the replacement
283284
*/
284285
public void replaceIsTrue(String oldValue, Object newValue) {
285-
getTransformations().add(new ReplaceIsTrue(oldValue, MAPPER.convertValue(newValue, TextNode.class)));
286+
getTransformations().add(new ReplaceIsTrue(oldValue, SerializableJsonNode.of(newValue, TextNode.class)));
286287
}
287288

288289
/**
@@ -294,7 +295,7 @@ public void replaceIsTrue(String oldValue, Object newValue) {
294295
* @param testName the testName to apply replacement
295296
*/
296297
public void replaceIsTrue(String oldValue, Object newValue, String testName) {
297-
getTransformations().add(new ReplaceIsTrue(oldValue, MAPPER.convertValue(newValue, TextNode.class), testName));
298+
getTransformations().add(new ReplaceIsTrue(oldValue, SerializableJsonNode.of(newValue, TextNode.class), testName));
298299
}
299300

300301
/**
@@ -305,7 +306,7 @@ public void replaceIsTrue(String oldValue, Object newValue, String testName) {
305306
* @param newValue the value used in the replacement
306307
*/
307308
public void replaceIsFalse(String oldValue, Object newValue) {
308-
getTransformations().add(new ReplaceIsFalse(oldValue, MAPPER.convertValue(newValue, TextNode.class)));
309+
getTransformations().add(new ReplaceIsFalse(oldValue, SerializableJsonNode.of(newValue, TextNode.class)));
309310
}
310311

311312
/**
@@ -317,7 +318,7 @@ public void replaceIsFalse(String oldValue, Object newValue) {
317318
* @param testName the testName to apply replacement
318319
*/
319320
public void replaceIsFalse(String oldValue, Object newValue, String testName) {
320-
getTransformations().add(new ReplaceIsFalse(oldValue, MAPPER.convertValue(newValue, TextNode.class), testName));
321+
getTransformations().add(new ReplaceIsFalse(oldValue, SerializableJsonNode.of(newValue, TextNode.class), testName));
321322
}
322323

323324
/**
@@ -329,7 +330,7 @@ public void replaceIsFalse(String oldValue, Object newValue, String testName) {
329330
* @param newValue the value used in the replacement
330331
*/
331332
public void replaceValueTextByKeyValue(String key, String oldValue, Object newValue) {
332-
getTransformations().add(new ReplaceTextual(key, oldValue, MAPPER.convertValue(newValue, TextNode.class)));
333+
getTransformations().add(new ReplaceTextual(key, oldValue, SerializableJsonNode.of(newValue, TextNode.class)));
333334
}
334335

335336
/**
@@ -342,7 +343,7 @@ public void replaceValueTextByKeyValue(String key, String oldValue, Object newVa
342343
* @param testName the testName to apply replacement
343344
*/
344345
public void replaceValueTextByKeyValue(String key, String oldValue, Object newValue, String testName) {
345-
getTransformations().add(new ReplaceTextual(key, oldValue, MAPPER.convertValue(newValue, TextNode.class), testName));
346+
getTransformations().add(new ReplaceTextual(key, oldValue, SerializableJsonNode.of(newValue, TextNode.class), testName));
346347
}
347348

348349
/**
@@ -376,7 +377,7 @@ public void removeMatch(String subKey, String testName) {
376377
* @param testName the testName to apply addition
377378
*/
378379
public void addMatch(String subKey, Object value, String testName) {
379-
getTransformations().add(new AddMatch(subKey, MAPPER.convertValue(value, JsonNode.class), testName));
380+
getTransformations().add(new AddMatch(subKey, SerializableJsonNode.of(value, JsonNode.class), testName));
380381
}
381382

382383
/**

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/ReplaceByKey.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.elasticsearch.gradle.internal.test.rest.transform;
1111

1212
import com.fasterxml.jackson.databind.JsonNode;
13+
import com.fasterxml.jackson.databind.node.ObjectNode;
1314

1415
import org.gradle.api.tasks.Input;
1516
import org.gradle.api.tasks.Optional;
@@ -25,18 +26,23 @@
2526
public abstract class ReplaceByKey implements RestTestTransformByParentObject {
2627
private final String requiredChildKey;
2728
private final String newChildKey;
28-
private final JsonNode replacementNode;
29+
private final SerializableJsonNode replacementNode;
2930
private final String testName;
3031

31-
public ReplaceByKey(String requiredChildKey, JsonNode replacementNode) {
32+
public ReplaceByKey(String requiredChildKey, SerializableJsonNode<JsonNode> replacementNode) {
3233
this(requiredChildKey, replacementNode, null);
3334
}
3435

35-
public ReplaceByKey(String requiredChildKey, JsonNode replacementNode, String testName) {
36+
public ReplaceByKey(String requiredChildKey, SerializableJsonNode<JsonNode> replacementNode, String testName) {
3637
this(requiredChildKey, requiredChildKey, replacementNode, testName);
3738
}
3839

39-
public ReplaceByKey(String requiredChildKey, String newChildKey, JsonNode replacementNode, String testName) {
40+
public ReplaceByKey(
41+
String requiredChildKey,
42+
String newChildKey,
43+
SerializableJsonNode<? extends JsonNode> replacementNode,
44+
String testName
45+
) {
4046
this.requiredChildKey = requiredChildKey;
4147
this.newChildKey = newChildKey;
4248
this.replacementNode = replacementNode;
@@ -60,7 +66,7 @@ public boolean shouldApply(RestTestContext testContext) {
6066

6167
@Input
6268
@Optional
63-
public JsonNode getReplacementNode() {
69+
public SerializableJsonNode<? extends JsonNode> getReplacementNode() {
6470
return replacementNode;
6571
}
6672

@@ -69,4 +75,10 @@ public JsonNode getReplacementNode() {
6975
public String getTestName() {
7076
return testName;
7177
}
78+
79+
protected void updateReplacement(ObjectNode matchNode) {
80+
matchNode.remove(requiredChildKey());
81+
matchNode.set(getNewChildKey(), replacementNode.toJsonNode());
82+
}
83+
7284
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.test.rest.transform;
11+
12+
import com.fasterxml.jackson.databind.JsonNode;
13+
import com.fasterxml.jackson.databind.ObjectMapper;
14+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
15+
16+
import java.io.Serializable;
17+
18+
/**
19+
* A serializable wrapper for a JsonNode that can be used as Gradle task inputs.
20+
* This is necessary because JsonNode serialization is not supported by Gradle configuration cache
21+
* as it relies on DataInput.readFully which is unsupported by Gradle.
22+
*
23+
* @param <T> The type of JsonNode this wrapper will hold.
24+
*/
25+
public class SerializableJsonNode<T extends JsonNode> implements Serializable {
26+
27+
private Object value;
28+
private Class<? extends JsonNode> type;
29+
30+
SerializableJsonNode(Object value, Class<? extends JsonNode> type) {
31+
this.value = value;
32+
this.type = type;
33+
}
34+
35+
public static SerializableJsonNode of(Object value, Class<? extends JsonNode> type) {
36+
return new SerializableJsonNode(value, type);
37+
}
38+
39+
public T toJsonNode() {
40+
YAMLFactory YAML_FACTORY = new YAMLFactory();
41+
ObjectMapper MAPPER = new ObjectMapper(YAML_FACTORY);
42+
return (T) MAPPER.convertValue(value, type);
43+
}
44+
}

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/close_to/ReplaceValueInCloseTo.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.fasterxml.jackson.databind.node.ObjectNode;
1414

1515
import org.elasticsearch.gradle.internal.test.rest.transform.ReplaceByKey;
16+
import org.elasticsearch.gradle.internal.test.rest.transform.SerializableJsonNode;
1617
import org.gradle.api.tasks.Internal;
1718

1819
/**
@@ -22,11 +23,11 @@
2223
*/
2324
public class ReplaceValueInCloseTo extends ReplaceByKey {
2425

25-
public ReplaceValueInCloseTo(String replaceKey, NumericNode replacementNode) {
26+
public ReplaceValueInCloseTo(String replaceKey, SerializableJsonNode<NumericNode> replacementNode) {
2627
this(replaceKey, replacementNode, null);
2728
}
2829

29-
public ReplaceValueInCloseTo(String replaceKey, NumericNode replacementNode, String testName) {
30+
public ReplaceValueInCloseTo(String replaceKey, SerializableJsonNode<NumericNode> replacementNode, String testName) {
3031
super(replaceKey, replaceKey, replacementNode, testName);
3132
}
3233

@@ -41,6 +42,7 @@ public void transformTest(ObjectNode matchParent) {
4142
ObjectNode closeToNode = (ObjectNode) matchParent.get(getKeyToFind());
4243
ObjectNode subNode = (ObjectNode) closeToNode.get(requiredChildKey());
4344
subNode.remove("value");
44-
subNode.set("value", getReplacementNode());
45+
subNode.set("value", getReplacementNode().toJsonNode());
4546
}
47+
4648
}

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/length/ReplaceValueInLength.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
package org.elasticsearch.gradle.internal.test.rest.transform.length;
1111

12-
import com.fasterxml.jackson.databind.node.NumericNode;
1312
import com.fasterxml.jackson.databind.node.ObjectNode;
1413

1514
import org.elasticsearch.gradle.internal.test.rest.transform.ReplaceByKey;
15+
import org.elasticsearch.gradle.internal.test.rest.transform.SerializableJsonNode;
1616
import org.gradle.api.tasks.Internal;
1717

1818
/**
@@ -21,11 +21,11 @@
2121
*/
2222
public class ReplaceValueInLength extends ReplaceByKey {
2323

24-
public ReplaceValueInLength(String replaceKey, NumericNode replacementNode) {
24+
public ReplaceValueInLength(String replaceKey, SerializableJsonNode replacementNode) {
2525
this(replaceKey, replacementNode, null);
2626
}
2727

28-
public ReplaceValueInLength(String replaceKey, NumericNode replacementNode, String testName) {
28+
public ReplaceValueInLength(String replaceKey, SerializableJsonNode replacementNode, String testName) {
2929
super(replaceKey, replaceKey, replacementNode, testName);
3030
}
3131

@@ -38,7 +38,7 @@ public String getKeyToFind() {
3838
@Override
3939
public void transformTest(ObjectNode matchParent) {
4040
ObjectNode matchNode = (ObjectNode) matchParent.get(getKeyToFind());
41-
matchNode.remove(requiredChildKey());
42-
matchNode.set(getNewChildKey(), getReplacementNode());
41+
updateReplacement(matchNode);
4342
}
43+
4444
}

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/match/AddMatch.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import org.elasticsearch.gradle.internal.test.rest.transform.RestTestContext;
1818
import org.elasticsearch.gradle.internal.test.rest.transform.RestTestTransformByParentArray;
19+
import org.elasticsearch.gradle.internal.test.rest.transform.SerializableJsonNode;
1920
import org.gradle.api.tasks.Input;
2021
import org.gradle.api.tasks.Internal;
2122

@@ -28,9 +29,9 @@ public class AddMatch implements RestTestTransformByParentArray {
2829
private static JsonNodeFactory jsonNodeFactory = JsonNodeFactory.withExactBigDecimals(false);
2930
private final String matchKey;
3031
private final String testName;
31-
private final JsonNode matchValue;
32+
private final SerializableJsonNode<JsonNode> matchValue;
3233

33-
public AddMatch(String matchKey, JsonNode matchValue, String testName) {
34+
public AddMatch(String matchKey, SerializableJsonNode<JsonNode> matchValue, String testName) {
3435
this.matchKey = matchKey;
3536
this.matchValue = matchValue;
3637
this.testName = Objects.requireNonNull(testName, "adding matches is only supported for named tests");
@@ -45,7 +46,7 @@ public boolean shouldApply(RestTestContext testContext) {
4546
public void transformTest(ArrayNode matchParent) {
4647
ObjectNode matchObject = new ObjectNode(jsonNodeFactory);
4748
ObjectNode matchContent = new ObjectNode(jsonNodeFactory);
48-
matchContent.set(matchKey, matchValue);
49+
matchContent.set(matchKey, matchValue.toJsonNode());
4950
matchObject.set("match", matchContent);
5051
matchParent.add(matchObject);
5152
}
@@ -70,6 +71,6 @@ public String getTestName() {
7071

7172
@Input
7273
public JsonNode getMatchValue() {
73-
return matchValue;
74+
return matchValue.toJsonNode();
7475
}
7576
}

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/match/ReplaceValueInMatch.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313
import com.fasterxml.jackson.databind.node.ObjectNode;
1414

1515
import org.elasticsearch.gradle.internal.test.rest.transform.ReplaceByKey;
16+
import org.elasticsearch.gradle.internal.test.rest.transform.SerializableJsonNode;
1617
import org.gradle.api.tasks.Internal;
1718

1819
/**
1920
* A transformation to replace the value of a match. For example, change from "match":{"_type": "foo"} to "match":{"_type": "bar"}
2021
*/
2122
public class ReplaceValueInMatch extends ReplaceByKey {
2223

23-
public ReplaceValueInMatch(String replaceKey, JsonNode replacementNode) {
24+
public ReplaceValueInMatch(String replaceKey, SerializableJsonNode<JsonNode> replacementNode) {
2425
this(replaceKey, replacementNode, null);
2526
}
2627

27-
public ReplaceValueInMatch(String replaceKey, JsonNode replacementNode, String testName) {
28+
public ReplaceValueInMatch(String replaceKey, SerializableJsonNode<JsonNode> replacementNode, String testName) {
2829
super(replaceKey, replaceKey, replacementNode, testName);
2930
}
3031

@@ -37,7 +38,6 @@ public String getKeyToFind() {
3738
@Override
3839
public void transformTest(ObjectNode matchParent) {
3940
ObjectNode matchNode = (ObjectNode) matchParent.get(getKeyToFind());
40-
matchNode.remove(requiredChildKey());
41-
matchNode.set(getNewChildKey(), getReplacementNode());
41+
updateReplacement(matchNode);
4242
}
4343
}

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/text/ReplaceIsFalse.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

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

14+
import org.elasticsearch.gradle.internal.test.rest.transform.SerializableJsonNode;
15+
1416
public class ReplaceIsFalse extends ReplaceTextual {
15-
public ReplaceIsFalse(String valueToBeReplaced, TextNode replacementNode) {
17+
public ReplaceIsFalse(String valueToBeReplaced, SerializableJsonNode<TextNode> replacementNode) {
1618
super("is_false", valueToBeReplaced, replacementNode);
1719
}
1820

19-
public ReplaceIsFalse(String valueToBeReplaced, TextNode replacementNode, String testName) {
21+
public ReplaceIsFalse(String valueToBeReplaced, SerializableJsonNode<TextNode> replacementNode, String testName) {
2022
super("is_false", valueToBeReplaced, replacementNode, testName);
2123
}
2224
}

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/transform/text/ReplaceIsTrue.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

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

14+
import org.elasticsearch.gradle.internal.test.rest.transform.SerializableJsonNode;
15+
1416
public class ReplaceIsTrue extends ReplaceTextual {
15-
public ReplaceIsTrue(String valueToBeReplaced, TextNode replacementNode) {
17+
public ReplaceIsTrue(String valueToBeReplaced, SerializableJsonNode<TextNode> replacementNode) {
1618
super("is_true", valueToBeReplaced, replacementNode);
1719
}
1820

19-
public ReplaceIsTrue(String valueToBeReplaced, TextNode replacementNode, String testName) {
21+
public ReplaceIsTrue(String valueToBeReplaced, SerializableJsonNode<TextNode> replacementNode, String testName) {
2022
super("is_true", valueToBeReplaced, replacementNode, testName);
2123
}
2224
}

0 commit comments

Comments
 (0)