From 90cb2eca209bf13299493fa016efb6f4ca241e49 Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Mon, 19 Jun 2017 12:21:03 +0200 Subject: [PATCH 1/2] [Test] Add unit test for XContentParserUtilsTests.parseStoredFieldsValue --- .../xcontent/XContentParserUtilsTests.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java index 195448d9ff2f6..e8319117437d5 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java @@ -20,19 +20,24 @@ package org.elasticsearch.common.xcontent; import org.apache.lucene.util.SetOnce; +import org.elasticsearch.common.CheckedBiConsumer; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; +import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; +import java.util.Base64; import java.util.List; import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; +import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureFieldName; import static org.elasticsearch.common.xcontent.XContentParserUtils.parseTypedKeysObject; +import static org.hamcrest.Matchers.containsString; public class XContentParserUtilsTests extends ESTestCase { @@ -49,6 +54,76 @@ public void testEnsureExpectedToken() throws IOException { } } + public void testParseStoredFieldsValueString() throws IOException { + final String value = randomAlphaOfLengthBetween(0, 10); + assertParseStoredFieldsValue(value, (xcontentType, result) -> assertEquals(value, result)); + } + + public void testParseStoredFieldsValueInt() throws IOException { + final Integer value = randomInt(1_000); + assertParseStoredFieldsValue(value, (xcontentType, result) -> assertEquals(value, result)); + } + + public void testParseStoredFieldsValueLong() throws IOException { + final Long value = randomLong(); + assertParseStoredFieldsValue(value, (xcontentType, result) -> assertEquals(value, result)); + } + + public void testParseStoredFieldsValueDouble() throws IOException { + final Double value = randomDouble(); + assertParseStoredFieldsValue(value, (xcontentType, result) -> assertEquals(value, ((Number) result).doubleValue(), 0.0d)); + } + + public void testParseStoredFieldsValueFloat() throws IOException { + final Float value = randomFloat(); + assertParseStoredFieldsValue(value, (xcontentType, result) -> assertEquals(value, ((Number) result).floatValue(), 0.0f)); + } + + public void testParseStoredFieldsValueBoolean() throws IOException { + final Boolean value = randomBoolean(); + assertParseStoredFieldsValue(value, (xcontentType, result) -> assertEquals(value, result)); + } + + public void testParseStoredFieldsValueBinary() throws IOException { + final byte[] value = randomUnicodeOfLength(scaledRandomIntBetween(10, 1000)).getBytes("UTF-8"); + assertParseStoredFieldsValue(value, (xcontentType, result) -> { + if (xcontentType == XContentType.JSON || xcontentType == XContentType.YAML) { + //binary values will be parsed back and returned as base64 strings when reading from json and yaml + assertArrayEquals(value, Base64.getDecoder().decode((String) result)); + } else { + //binary values will be parsed back and returned as BytesArray when reading from cbor and smile + assertArrayEquals(value, ((BytesArray) result).array()); + } + }); + } + + public void testParseStoredFieldsValueUnknown() throws IOException { + ParsingException e = expectThrows(ParsingException.class, () -> + assertParseStoredFieldsValue(null, (x, r) -> fail("Should have thrown a parsing exception"))); + assertThat(e.getMessage(), containsString("unexpected token")); + } + + private void assertParseStoredFieldsValue(final Object value, final CheckedBiConsumer consumer) + throws IOException { + final XContentType xContentType = randomFrom(XContentType.values()); + try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { + final String fieldName = randomAlphaOfLengthBetween(0, 10); + + builder.startObject(); + builder.field(fieldName, value); + builder.endObject(); + + try (XContentParser parser = createParser(builder)) { + ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); + ensureFieldName(parser, parser.nextToken(), fieldName); + assertNotNull(parser.nextToken()); + consumer.accept(xContentType, XContentParserUtils.parseStoredFieldsValue(parser)); + ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser::getTokenLocation); + assertNull(parser.nextToken()); + } + } + } + public void testParseTypedKeysObject() throws IOException { final String delimiter = randomFrom("#", ":", "/", "-", "_", "|", "_delim_"); final XContentType xContentType = randomFrom(XContentType.values()); From 2c2852f061f18cd967d35de916d7c6cde13de24a Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Fri, 23 Jun 2017 09:43:36 +0200 Subject: [PATCH 2/2] Apply feedback --- .../elasticsearch/common/xcontent/XContentParserUtilsTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java index e8319117437d5..26bf83d7d56b1 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java @@ -60,7 +60,7 @@ public void testParseStoredFieldsValueString() throws IOException { } public void testParseStoredFieldsValueInt() throws IOException { - final Integer value = randomInt(1_000); + final Integer value = randomInt(); assertParseStoredFieldsValue(value, (xcontentType, result) -> assertEquals(value, result)); }