@@ -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}
0 commit comments