1919
2020package org .elasticsearch .common .xcontent ;
2121
22+ import org .elasticsearch .common .CheckedBiConsumer ;
2223import org .elasticsearch .common .ParseField ;
2324import org .elasticsearch .common .ParsingException ;
25+ import org .elasticsearch .common .bytes .BytesArray ;
2426import org .elasticsearch .common .bytes .BytesReference ;
2527import org .elasticsearch .common .xcontent .json .JsonXContent ;
2628import org .elasticsearch .test .ESTestCase ;
2729
2830import java .io .IOException ;
2931import java .util .ArrayList ;
32+ import java .util .Base64 ;
3033import java .util .List ;
3134
3235import static org .elasticsearch .common .xcontent .XContentHelper .toXContent ;
3336import static org .elasticsearch .common .xcontent .XContentParserUtils .ensureExpectedToken ;
37+ import static org .elasticsearch .common .xcontent .XContentParserUtils .ensureFieldName ;
3438import static org .elasticsearch .common .xcontent .XContentParserUtils .parseTypedKeysObject ;
39+ import static org .hamcrest .Matchers .containsString ;
3540
3641public class XContentParserUtilsTests extends ESTestCase {
3742
@@ -48,6 +53,76 @@ public void testEnsureExpectedToken() throws IOException {
4853 }
4954 }
5055
56+ public void testParseStoredFieldsValueString () throws IOException {
57+ final String value = randomAlphaOfLengthBetween (0 , 10 );
58+ assertParseStoredFieldsValue (value , (xcontentType , result ) -> assertEquals (value , result ));
59+ }
60+
61+ public void testParseStoredFieldsValueInt () throws IOException {
62+ final Integer value = randomInt ();
63+ assertParseStoredFieldsValue (value , (xcontentType , result ) -> assertEquals (value , result ));
64+ }
65+
66+ public void testParseStoredFieldsValueLong () throws IOException {
67+ final Long value = randomLong ();
68+ assertParseStoredFieldsValue (value , (xcontentType , result ) -> assertEquals (value , result ));
69+ }
70+
71+ public void testParseStoredFieldsValueDouble () throws IOException {
72+ final Double value = randomDouble ();
73+ assertParseStoredFieldsValue (value , (xcontentType , result ) -> assertEquals (value , ((Number ) result ).doubleValue (), 0.0d ));
74+ }
75+
76+ public void testParseStoredFieldsValueFloat () throws IOException {
77+ final Float value = randomFloat ();
78+ assertParseStoredFieldsValue (value , (xcontentType , result ) -> assertEquals (value , ((Number ) result ).floatValue (), 0.0f ));
79+ }
80+
81+ public void testParseStoredFieldsValueBoolean () throws IOException {
82+ final Boolean value = randomBoolean ();
83+ assertParseStoredFieldsValue (value , (xcontentType , result ) -> assertEquals (value , result ));
84+ }
85+
86+ public void testParseStoredFieldsValueBinary () throws IOException {
87+ final byte [] value = randomUnicodeOfLength (scaledRandomIntBetween (10 , 1000 )).getBytes ("UTF-8" );
88+ assertParseStoredFieldsValue (value , (xcontentType , result ) -> {
89+ if (xcontentType == XContentType .JSON || xcontentType == XContentType .YAML ) {
90+ //binary values will be parsed back and returned as base64 strings when reading from json and yaml
91+ assertArrayEquals (value , Base64 .getDecoder ().decode ((String ) result ));
92+ } else {
93+ //binary values will be parsed back and returned as BytesArray when reading from cbor and smile
94+ assertArrayEquals (value , ((BytesArray ) result ).array ());
95+ }
96+ });
97+ }
98+
99+ public void testParseStoredFieldsValueUnknown () throws IOException {
100+ ParsingException e = expectThrows (ParsingException .class , () ->
101+ assertParseStoredFieldsValue (null , (x , r ) -> fail ("Should have thrown a parsing exception" )));
102+ assertThat (e .getMessage (), containsString ("unexpected token" ));
103+ }
104+
105+ private void assertParseStoredFieldsValue (final Object value , final CheckedBiConsumer <XContentType , Object , IOException > consumer )
106+ throws IOException {
107+ final XContentType xContentType = randomFrom (XContentType .values ());
108+ try (XContentBuilder builder = XContentBuilder .builder (xContentType .xContent ())) {
109+ final String fieldName = randomAlphaOfLengthBetween (0 , 10 );
110+
111+ builder .startObject ();
112+ builder .field (fieldName , value );
113+ builder .endObject ();
114+
115+ try (XContentParser parser = createParser (builder )) {
116+ ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .nextToken (), parser ::getTokenLocation );
117+ ensureFieldName (parser , parser .nextToken (), fieldName );
118+ assertNotNull (parser .nextToken ());
119+ consumer .accept (xContentType , XContentParserUtils .parseStoredFieldsValue (parser ));
120+ ensureExpectedToken (XContentParser .Token .END_OBJECT , parser .nextToken (), parser ::getTokenLocation );
121+ assertNull (parser .nextToken ());
122+ }
123+ }
124+ }
125+
51126 public void testParseTypedKeysObject () throws IOException {
52127 final String delimiter = randomFrom ("#" , ":" , "/" , "-" , "_" , "|" , "_delim_" );
53128 final XContentType xContentType = randomFrom (XContentType .values ());
0 commit comments