Skip to content

Commit 334c255

Browse files
olcbeanChristoph Büscher
authored and
Christoph Büscher
committed
XContentTests : Insert random fields at random positions (#30867)
Currently AbstractXContentTestCase#testFromXContent appends random fields, but in a fixed position. This PR shuffles all fields after the random fields have been appended, hence the random fields are actually added to random positions.
1 parent 44f280f commit 334c255

File tree

2 files changed

+90
-13
lines changed

2 files changed

+90
-13
lines changed

test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,16 @@ public static <T extends ToXContent> void testFromXContent(int numberOfTestRuns,
5454
for (int runs = 0; runs < numberOfTestRuns; runs++) {
5555
T testInstance = instanceSupplier.get();
5656
XContentType xContentType = randomFrom(XContentType.values());
57-
BytesReference shuffled = toShuffledXContent(testInstance, xContentType, toXContentParams,false,
58-
createParserFunction, shuffleFieldsExceptions);
59-
BytesReference withRandomFields;
60-
if (supportsUnknownFields) {
61-
// we add a few random fields to check that parser is lenient on new fields
62-
withRandomFields = XContentTestUtils.insertRandomFields(xContentType, shuffled, randomFieldsExcludeFilter, random());
63-
} else {
64-
withRandomFields = shuffled;
65-
}
66-
XContentParser parser = createParserFunction.apply(XContentFactory.xContent(xContentType), withRandomFields);
57+
BytesReference shuffledContent = insertRandomFieldsAndShuffle(testInstance, xContentType, supportsUnknownFields,
58+
shuffleFieldsExceptions, randomFieldsExcludeFilter, createParserFunction, toXContentParams);
59+
XContentParser parser = createParserFunction.apply(XContentFactory.xContent(xContentType), shuffledContent);
6760
T parsed = parseFunction.apply(parser);
6861
assertEqualsConsumer.accept(testInstance, parsed);
6962
if (assertToXContentEquivalence) {
70-
assertToXContentEquivalent(shuffled, XContentHelper.toXContent(parsed, xContentType, toXContentParams, false),
71-
xContentType);
63+
assertToXContentEquivalent(
64+
XContentHelper.toXContent(testInstance, xContentType, toXContentParams, false),
65+
XContentHelper.toXContent(parsed, xContentType, toXContentParams, false),
66+
xContentType);
7267
}
7368
}
7469
}
@@ -132,9 +127,26 @@ protected String[] getShuffleFieldsExceptions() {
132127
}
133128

134129
/**
135-
* Params that have to be provided when calling calling {@link ToXContent#toXContent(XContentBuilder, ToXContent.Params)}
130+
* Params that have to be provided when calling {@link ToXContent#toXContent(XContentBuilder, ToXContent.Params)}
136131
*/
137132
protected ToXContent.Params getToXContentParams() {
138133
return ToXContent.EMPTY_PARAMS;
139134
}
135+
136+
static BytesReference insertRandomFieldsAndShuffle(ToXContent testInstance, XContentType xContentType,
137+
boolean supportsUnknownFields, String[] shuffleFieldsExceptions, Predicate<String> randomFieldsExcludeFilter,
138+
CheckedBiFunction<XContent, BytesReference, XContentParser, IOException> createParserFunction,
139+
ToXContent.Params toXContentParams) throws IOException {
140+
BytesReference xContent = XContentHelper.toXContent(testInstance, xContentType, toXContentParams, false);
141+
BytesReference withRandomFields;
142+
if (supportsUnknownFields) {
143+
// add a few random fields to check that the parser is lenient on new fields
144+
withRandomFields = XContentTestUtils.insertRandomFields(xContentType, xContent, randomFieldsExcludeFilter, random());
145+
} else {
146+
withRandomFields = xContent;
147+
}
148+
XContentParser parserWithRandonFields = createParserFunction.apply(XContentFactory.xContent(xContentType), withRandomFields);
149+
return BytesReference.bytes(ESTestCase.shuffleXContent(parserWithRandonFields, false, shuffleFieldsExceptions));
150+
}
151+
140152
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.test;
21+
22+
import com.carrotsearch.randomizedtesting.RandomizedContext;
23+
24+
import org.elasticsearch.common.bytes.BytesReference;
25+
import org.elasticsearch.common.xcontent.ToXContent;
26+
import org.elasticsearch.common.xcontent.ToXContentObject;
27+
import org.elasticsearch.common.xcontent.XContentBuilder;
28+
import org.elasticsearch.common.xcontent.XContentParser;
29+
import org.elasticsearch.common.xcontent.XContentType;
30+
31+
import java.io.IOException;
32+
import java.util.Map;
33+
34+
import static org.hamcrest.Matchers.equalTo;
35+
import static org.hamcrest.Matchers.not;
36+
37+
public class AbstractXContentTestCaseTests extends ESTestCase {
38+
39+
public void testInsertRandomFieldsAndShuffle() throws Exception {
40+
TestInstance t = new TestInstance();
41+
BytesReference insertRandomFieldsAndShuffle = RandomizedContext.current().runWithPrivateRandomness(1,
42+
() -> AbstractXContentTestCase.insertRandomFieldsAndShuffle(t, XContentType.JSON, true, new String[] {}, null,
43+
this::createParser, ToXContent.EMPTY_PARAMS));
44+
try (XContentParser parser = createParser(XContentType.JSON.xContent(), insertRandomFieldsAndShuffle)) {
45+
Map<String, Object> mapOrdered = parser.mapOrdered();
46+
assertThat(mapOrdered.size(), equalTo(2));
47+
assertThat(mapOrdered.keySet().iterator().next(), not(equalTo("field")));
48+
}
49+
}
50+
51+
private class TestInstance implements ToXContentObject {
52+
53+
@Override
54+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
55+
builder.startObject();
56+
{
57+
builder.field("field", 1);
58+
}
59+
builder.endObject();
60+
return builder;
61+
}
62+
63+
}
64+
65+
}

0 commit comments

Comments
 (0)