Skip to content

Commit 6a792d6

Browse files
authored
[Test] Add unit test for XContentParserUtilsTests.parseStoredFieldsValue (#25288)
1 parent fdb3a97 commit 6a792d6

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
@@ -20,19 +20,24 @@
2020
package org.elasticsearch.common.xcontent;
2121

2222
import org.apache.lucene.util.SetOnce;
23+
import org.elasticsearch.common.CheckedBiConsumer;
2324
import org.elasticsearch.common.ParseField;
2425
import org.elasticsearch.common.ParsingException;
26+
import org.elasticsearch.common.bytes.BytesArray;
2527
import org.elasticsearch.common.bytes.BytesReference;
2628
import org.elasticsearch.common.xcontent.json.JsonXContent;
2729
import org.elasticsearch.test.ESTestCase;
2830

2931
import java.io.IOException;
3032
import java.util.ArrayList;
33+
import java.util.Base64;
3134
import java.util.List;
3235

3336
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
3437
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
38+
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureFieldName;
3539
import static org.elasticsearch.common.xcontent.XContentParserUtils.parseTypedKeysObject;
40+
import static org.hamcrest.Matchers.containsString;
3641

3742
public class XContentParserUtilsTests extends ESTestCase {
3843

@@ -49,6 +54,76 @@ public void testEnsureExpectedToken() throws IOException {
4954
}
5055
}
5156

57+
public void testParseStoredFieldsValueString() throws IOException {
58+
final String value = randomAlphaOfLengthBetween(0, 10);
59+
assertParseStoredFieldsValue(value, (xcontentType, result) -> assertEquals(value, result));
60+
}
61+
62+
public void testParseStoredFieldsValueInt() throws IOException {
63+
final Integer value = randomInt();
64+
assertParseStoredFieldsValue(value, (xcontentType, result) -> assertEquals(value, result));
65+
}
66+
67+
public void testParseStoredFieldsValueLong() throws IOException {
68+
final Long value = randomLong();
69+
assertParseStoredFieldsValue(value, (xcontentType, result) -> assertEquals(value, result));
70+
}
71+
72+
public void testParseStoredFieldsValueDouble() throws IOException {
73+
final Double value = randomDouble();
74+
assertParseStoredFieldsValue(value, (xcontentType, result) -> assertEquals(value, ((Number) result).doubleValue(), 0.0d));
75+
}
76+
77+
public void testParseStoredFieldsValueFloat() throws IOException {
78+
final Float value = randomFloat();
79+
assertParseStoredFieldsValue(value, (xcontentType, result) -> assertEquals(value, ((Number) result).floatValue(), 0.0f));
80+
}
81+
82+
public void testParseStoredFieldsValueBoolean() throws IOException {
83+
final Boolean value = randomBoolean();
84+
assertParseStoredFieldsValue(value, (xcontentType, result) -> assertEquals(value, result));
85+
}
86+
87+
public void testParseStoredFieldsValueBinary() throws IOException {
88+
final byte[] value = randomUnicodeOfLength(scaledRandomIntBetween(10, 1000)).getBytes("UTF-8");
89+
assertParseStoredFieldsValue(value, (xcontentType, result) -> {
90+
if (xcontentType == XContentType.JSON || xcontentType == XContentType.YAML) {
91+
//binary values will be parsed back and returned as base64 strings when reading from json and yaml
92+
assertArrayEquals(value, Base64.getDecoder().decode((String) result));
93+
} else {
94+
//binary values will be parsed back and returned as BytesArray when reading from cbor and smile
95+
assertArrayEquals(value, ((BytesArray) result).array());
96+
}
97+
});
98+
}
99+
100+
public void testParseStoredFieldsValueUnknown() throws IOException {
101+
ParsingException e = expectThrows(ParsingException.class, () ->
102+
assertParseStoredFieldsValue(null, (x, r) -> fail("Should have thrown a parsing exception")));
103+
assertThat(e.getMessage(), containsString("unexpected token"));
104+
}
105+
106+
private void assertParseStoredFieldsValue(final Object value, final CheckedBiConsumer<XContentType, Object, IOException> consumer)
107+
throws IOException {
108+
final XContentType xContentType = randomFrom(XContentType.values());
109+
try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
110+
final String fieldName = randomAlphaOfLengthBetween(0, 10);
111+
112+
builder.startObject();
113+
builder.field(fieldName, value);
114+
builder.endObject();
115+
116+
try (XContentParser parser = createParser(builder)) {
117+
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
118+
ensureFieldName(parser, parser.nextToken(), fieldName);
119+
assertNotNull(parser.nextToken());
120+
consumer.accept(xContentType, XContentParserUtils.parseStoredFieldsValue(parser));
121+
ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser::getTokenLocation);
122+
assertNull(parser.nextToken());
123+
}
124+
}
125+
}
126+
52127
public void testParseTypedKeysObject() throws IOException {
53128
final String delimiter = randomFrom("#", ":", "/", "-", "_", "|", "_delim_");
54129
final XContentType xContentType = randomFrom(XContentType.values());

0 commit comments

Comments
 (0)