From d5df738538cf438d45b155e78819e43efbd1fc81 Mon Sep 17 00:00:00 2001 From: javanna Date: Sat, 25 Jun 2016 15:35:20 +0200 Subject: [PATCH] [TEST] ObjectPath to support parsing yaml or json that have an array as root object ObjectPath used a Map up until now for the internal representation of its navigable object. That works in most of the cases, but there could also be an array as root object, in which case a List needs to be used instead of a Map. This commit changes the internal representation of the object to Object which can either be a List or a Map. The change is minimal as ObjectPath already had the checks in place to verify the type of the object in the current position and navigate through it. Note: The new test added to ObjectPathTest uses yaml format explicitly as auto-detection of json format works only for a json object that starts with '{', not if the root object is actually an array and starts with '['. --- .../elasticsearch/test/rest/ObjectPath.java | 9 ++++--- .../test/rest/test/ObjectPathTests.java | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/ObjectPath.java b/test/framework/src/main/java/org/elasticsearch/test/rest/ObjectPath.java index 85ade469023..36755083176 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/ObjectPath.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/ObjectPath.java @@ -31,16 +31,19 @@ import java.util.Map; */ public class ObjectPath { - private final Map object; + private final Object object; public static ObjectPath createFromXContent(String input) throws IOException { try (XContentParser parser = XContentFactory.xContent(input).createParser(input)) { + if (parser.nextToken() == XContentParser.Token.START_ARRAY) { + return new ObjectPath(parser.listOrderedMap()); + } return new ObjectPath(parser.mapOrdered()); } } - public ObjectPath(Map map) throws IOException { - this.object = map; + public ObjectPath(Object object) throws IOException { + this.object = object; } /** diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/test/ObjectPathTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/test/ObjectPathTests.java index f5616a957db..26dc5f928a1 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/test/ObjectPathTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/test/ObjectPathTests.java @@ -198,4 +198,28 @@ public class ObjectPathTests extends ESTestCase { assertThat(object, notNullValue()); assertThat(object.toString(), equalTo("value1")); } + + @SuppressWarnings("unchecked") + public void testEvaluateArrayAsRoot() throws Exception { + String input = "---\n" + + "- alias: \"test_alias1\"\n" + + " index: \"test1\"\n" + + "- alias: \"test_alias2\"\n" + + " index: \"test2\""; + + ObjectPath objectPath = ObjectPath.createFromXContent(input); + Object object = objectPath.evaluate(""); + assertThat(object, notNullValue()); + assertThat(object, instanceOf(List.class)); + assertThat(((List)object).size(), equalTo(2)); + object = objectPath.evaluate("0"); + assertThat(object, notNullValue()); + assertThat(object, instanceOf(Map.class)); + assertThat(((Map)object).get("alias"), equalTo("test_alias1")); + object = objectPath.evaluate("1.index"); + assertThat(object, notNullValue()); + assertThat(object, instanceOf(String.class)); + assertThat(object, equalTo("test2")); + } + }