Skip to content

Commit 073ee48

Browse files
committed
[Test] Add unit test for XContentParserUtilsTests.parseStoredFieldsValue (#25288)
1 parent 4e26618 commit 073ee48

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

core/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,24 @@
1919

2020
package org.elasticsearch.common.xcontent;
2121

22+
import org.elasticsearch.common.CheckedBiConsumer;
2223
import org.elasticsearch.common.ParseField;
2324
import org.elasticsearch.common.ParsingException;
25+
import org.elasticsearch.common.bytes.BytesArray;
2426
import org.elasticsearch.common.bytes.BytesReference;
2527
import org.elasticsearch.common.xcontent.json.JsonXContent;
2628
import org.elasticsearch.test.ESTestCase;
2729

2830
import java.io.IOException;
2931
import java.util.ArrayList;
32+
import java.util.Base64;
3033
import java.util.List;
3134

3235
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
3336
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
37+
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureFieldName;
3438
import static org.elasticsearch.common.xcontent.XContentParserUtils.parseTypedKeysObject;
39+
import static org.hamcrest.Matchers.containsString;
3540

3641
public 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

Comments
 (0)