summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/common
diff options
context:
space:
mode:
authorTanguy Leroux <tlrx.dev@gmail.com>2017-04-21 15:41:27 +0200
committerGitHub <noreply@github.com>2017-04-21 15:41:27 +0200
commit480bf0996d1c2255051f9f9c5ddad5ee08246c30 (patch)
tree558f86c06e282e5f7b50592918a069f6a3cfc682 /core/src/test/java/org/elasticsearch/common
parent251b6d452b1fbf5a3422eb296a602b7f15b39cea (diff)
Add utility method to parse named XContent objects with typed prefix (#24240)
This commit adds a XContentParserUtils.parseTypedKeysObject() method that can be used to parse named XContent objects identified by a field name containing a type identifier, a delimiter and the name of the object to parse.
Diffstat (limited to 'core/src/test/java/org/elasticsearch/common')
-rw-r--r--core/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java71
1 files changed, 71 insertions, 0 deletions
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 00edb25d5c..d0426e1e10 100644
--- a/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java
+++ b/core/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java
@@ -19,15 +19,22 @@
package org.elasticsearch.common.xcontent;
+import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParsingException;
+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.List;
+import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
+import static org.elasticsearch.common.xcontent.XContentParserUtils.parseTypedKeysObject;
public class XContentParserUtilsTests extends ESTestCase {
+
public void testEnsureExpectedToken() throws IOException {
final XContentParser.Token randomToken = randomFrom(XContentParser.Token.values());
try (XContentParser parser = createParser(JsonXContent.jsonXContent, "{}")) {
@@ -40,4 +47,68 @@ public class XContentParserUtilsTests extends ESTestCase {
ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser::getTokenLocation);
}
}
+
+ public void testParseTypedKeysObject() throws IOException {
+ final String delimiter = randomFrom("#", ":", "/", "-", "_", "|", "_delim_");
+ final XContentType xContentType = randomFrom(XContentType.values());
+
+ List<NamedXContentRegistry.Entry> namedXContents = new ArrayList<>();
+ namedXContents.add(new NamedXContentRegistry.Entry(Boolean.class, new ParseField("bool"), parser -> {
+ ensureExpectedToken(XContentParser.Token.VALUE_BOOLEAN, parser.nextToken(), parser::getTokenLocation);
+ return parser.booleanValue();
+ }));
+ namedXContents.add(new NamedXContentRegistry.Entry(Long.class, new ParseField("long"), parser -> {
+ ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, parser.nextToken(), parser::getTokenLocation);
+ return parser.longValue();
+ }));
+ final NamedXContentRegistry namedXContentRegistry = new NamedXContentRegistry(namedXContents);
+
+ BytesReference bytes = toXContent((builder, params) -> builder.field("test", 0), xContentType, randomBoolean());
+ try (XContentParser parser = xContentType.xContent().createParser(namedXContentRegistry, bytes)) {
+ parser.nextToken();
+ ParsingException e = expectThrows(ParsingException.class, () -> parseTypedKeysObject(parser, delimiter, Boolean.class));
+ assertEquals("Failed to parse object: expecting token of type [FIELD_NAME] but found [START_OBJECT]", e.getMessage());
+
+ parser.nextToken();
+ e = expectThrows(ParsingException.class, () -> parseTypedKeysObject(parser, delimiter, Boolean.class));
+ assertEquals("Cannot parse object of class [Boolean] without type information. Set [typed_keys] parameter " +
+ "on the request to ensure the type information is added to the response output", e.getMessage());
+ }
+
+ bytes = toXContent((builder, params) -> builder.field("type" + delimiter + "name", 0), xContentType, randomBoolean());
+ try (XContentParser parser = xContentType.xContent().createParser(namedXContentRegistry, bytes)) {
+ ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
+ ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
+
+ NamedXContentRegistry.UnknownNamedObjectException e = expectThrows(NamedXContentRegistry.UnknownNamedObjectException.class,
+ () -> parseTypedKeysObject(parser, delimiter, Boolean.class));
+ assertEquals("Unknown Boolean [type]", e.getMessage());
+ assertEquals("type", e.getName());
+ assertEquals("java.lang.Boolean", e.getCategoryClass());
+ }
+
+ final long longValue = randomLong();
+ final boolean boolValue = randomBoolean();
+ bytes = toXContent((builder, params) -> {
+ builder.field("long" + delimiter + "l", longValue);
+ builder.field("bool" + delimiter + "b", boolValue);
+ return builder;
+ }, xContentType, randomBoolean());
+
+ try (XContentParser parser = xContentType.xContent().createParser(namedXContentRegistry, bytes)) {
+ ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
+
+ ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
+ Long parsedLong = parseTypedKeysObject(parser, delimiter, Long.class);
+ assertNotNull(parsedLong);
+ assertEquals(longValue, parsedLong.longValue());
+
+ ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
+ Boolean parsedBoolean = parseTypedKeysObject(parser, delimiter, Boolean.class);
+ assertNotNull(parsedBoolean);
+ assertEquals(boolValue, parsedBoolean);
+
+ ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser::getTokenLocation);
+ }
+ }
}