@@ -54,21 +54,16 @@ public static <T extends ToXContent> void testFromXContent(int numberOfTestRuns,
54
54
for (int runs = 0 ; runs < numberOfTestRuns ; runs ++) {
55
55
T testInstance = instanceSupplier .get ();
56
56
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 );
67
60
T parsed = parseFunction .apply (parser );
68
61
assertEqualsConsumer .accept (testInstance , parsed );
69
62
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 );
72
67
}
73
68
}
74
69
}
@@ -132,9 +127,26 @@ protected String[] getShuffleFieldsExceptions() {
132
127
}
133
128
134
129
/**
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)}
136
131
*/
137
132
protected ToXContent .Params getToXContentParams () {
138
133
return ToXContent .EMPTY_PARAMS ;
139
134
}
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
+
140
152
}
0 commit comments