3333import java .io .IOException ;
3434
3535import static org .elasticsearch .common .xcontent .XContentHelper .toXContent ;
36+ import static org .elasticsearch .test .XContentTestUtils .insertRandomFields ;
3637
3738public class ShardSearchFailureTests extends ESTestCase {
3839
@@ -48,13 +49,31 @@ public static ShardSearchFailure createTestItem() {
4849 }
4950
5051 public void testFromXContent () throws IOException {
52+ doFromXContentTestWithRandomFields (false );
53+ }
54+
55+ /**
56+ * This test adds random fields and objects to the xContent rendered out to
57+ * ensure we can parse it back to be forward compatible with additions to
58+ * the xContent
59+ */
60+ public void testFromXContentWithRandomFields () throws IOException {
61+ doFromXContentTestWithRandomFields (true );
62+ }
63+
64+ private void doFromXContentTestWithRandomFields (boolean addRandomFields ) throws IOException {
5165 ShardSearchFailure response = createTestItem ();
5266 XContentType xContentType = randomFrom (XContentType .values ());
5367 boolean humanReadable = randomBoolean ();
5468 BytesReference originalBytes = toShuffledXContent (response , xContentType , ToXContent .EMPTY_PARAMS , humanReadable );
55-
69+ BytesReference mutated ;
70+ if (addRandomFields ) {
71+ mutated = insertRandomFields (xContentType , originalBytes , null , random ());
72+ } else {
73+ mutated = originalBytes ;
74+ }
5675 ShardSearchFailure parsed ;
57- try (XContentParser parser = createParser (xContentType .xContent (), originalBytes )) {
76+ try (XContentParser parser = createParser (xContentType .xContent (), mutated )) {
5877 assertEquals (XContentParser .Token .START_OBJECT , parser .nextToken ());
5978 parsed = ShardSearchFailure .fromXContent (parser );
6079 assertEquals (XContentParser .Token .END_OBJECT , parser .currentToken ());
@@ -64,8 +83,11 @@ public void testFromXContent() throws IOException {
6483 assertEquals (response .shard ().getNodeId (), parsed .shard ().getNodeId ());
6584 assertEquals (response .shardId (), parsed .shardId ());
6685
67- // we cannot compare the cause, because it will be wrapped in an outer ElasticSearchException
68- // best effort: try to check that the original message appears somewhere in the rendered xContent
86+ /**
87+ * we cannot compare the cause, because it will be wrapped in an outer
88+ * ElasticSearchException best effort: try to check that the original
89+ * message appears somewhere in the rendered xContent
90+ */
6991 String originalMsg = response .getCause ().getMessage ();
7092 assertEquals (parsed .getCause ().getMessage (), "Elasticsearch exception [type=parsing_exception, reason=" + originalMsg + "]" );
7193 String nestedMsg = response .getCause ().getCause ().getMessage ();
0 commit comments