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")); + } + }