mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-09 06:25:07 +00:00
[TEST] extend ObjectPathTests to support also yaml format
This commit is contained in:
parent
44dc801e90
commit
bbaa23bdfd
@ -18,10 +18,14 @@
|
|||||||
*/
|
*/
|
||||||
package org.elasticsearch.test.rest.test;
|
package org.elasticsearch.test.rest.test;
|
||||||
|
|
||||||
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
import org.elasticsearch.test.rest.Stash;
|
|
||||||
import org.elasticsearch.test.rest.ObjectPath;
|
import org.elasticsearch.test.rest.ObjectPath;
|
||||||
|
import org.elasticsearch.test.rest.Stash;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -34,94 +38,135 @@ import static org.hamcrest.Matchers.notNullValue;
|
|||||||
import static org.hamcrest.Matchers.nullValue;
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
|
||||||
public class ObjectPathTests extends ESTestCase {
|
public class ObjectPathTests extends ESTestCase {
|
||||||
|
|
||||||
|
private static XContentBuilder randomXContentBuilder() throws IOException {
|
||||||
|
//only string based formats are supported, no cbor nor smile
|
||||||
|
XContentType xContentType = randomFrom(XContentType.JSON, XContentType.YAML);
|
||||||
|
return XContentBuilder.builder(XContentFactory.xContent(xContentType));
|
||||||
|
}
|
||||||
|
|
||||||
public void testEvaluateObjectPathEscape() throws Exception {
|
public void testEvaluateObjectPathEscape() throws Exception {
|
||||||
String json = "{ \"field1\": { \"field2.field3\" : \"value2\" } }";
|
XContentBuilder xContentBuilder = randomXContentBuilder();
|
||||||
ObjectPath objectPath = ObjectPath.createFromXContent(json);
|
xContentBuilder.startObject();
|
||||||
|
xContentBuilder.startObject("field1");
|
||||||
|
xContentBuilder.field("field2.field3", "value2");
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
ObjectPath objectPath = ObjectPath.createFromXContent(xContentBuilder.string());
|
||||||
Object object = objectPath.evaluate("field1.field2\\.field3");
|
Object object = objectPath.evaluate("field1.field2\\.field3");
|
||||||
assertThat(object, instanceOf(String.class));
|
assertThat(object, instanceOf(String.class));
|
||||||
assertThat((String)object, equalTo("value2"));
|
assertThat(object, equalTo("value2"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEvaluateObjectPathWithDoubleDot() throws Exception {
|
public void testEvaluateObjectPathWithDots() throws Exception {
|
||||||
String json = "{ \"field1\": { \"field2\" : \"value2\" } }";
|
XContentBuilder xContentBuilder = randomXContentBuilder();
|
||||||
ObjectPath objectPath = ObjectPath.createFromXContent(json);
|
xContentBuilder.startObject();
|
||||||
|
xContentBuilder.startObject("field1");
|
||||||
|
xContentBuilder.field("field2", "value2");
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
ObjectPath objectPath = ObjectPath.createFromXContent(xContentBuilder.string());
|
||||||
Object object = objectPath.evaluate("field1..field2");
|
Object object = objectPath.evaluate("field1..field2");
|
||||||
assertThat(object, instanceOf(String.class));
|
assertThat(object, instanceOf(String.class));
|
||||||
assertThat((String)object, equalTo("value2"));
|
assertThat(object, equalTo("value2"));
|
||||||
}
|
object = objectPath.evaluate("field1.field2.");
|
||||||
|
|
||||||
public void testEvaluateObjectPathEndsWithDot() throws Exception {
|
|
||||||
String json = "{ \"field1\": { \"field2\" : \"value2\" } }";
|
|
||||||
ObjectPath objectPath = ObjectPath.createFromXContent(json);
|
|
||||||
Object object = objectPath.evaluate("field1.field2.");
|
|
||||||
assertThat(object, instanceOf(String.class));
|
assertThat(object, instanceOf(String.class));
|
||||||
assertThat((String)object, equalTo("value2"));
|
assertThat(object, equalTo("value2"));
|
||||||
}
|
object = objectPath.evaluate("field1.field2");
|
||||||
|
|
||||||
public void testEvaluateString() throws Exception {
|
|
||||||
String json = "{ \"field1\": { \"field2\" : \"value2\" } }";
|
|
||||||
ObjectPath objectPath = ObjectPath.createFromXContent(json);
|
|
||||||
Object object = objectPath.evaluate("field1.field2");
|
|
||||||
assertThat(object, instanceOf(String.class));
|
assertThat(object, instanceOf(String.class));
|
||||||
assertThat((String)object, equalTo("value2"));
|
assertThat(object, equalTo("value2"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEvaluateInteger() throws Exception {
|
public void testEvaluateInteger() throws Exception {
|
||||||
String json = "{ \"field1\": { \"field2\" : 333 } }";
|
XContentBuilder xContentBuilder = randomXContentBuilder();
|
||||||
ObjectPath objectPath = ObjectPath.createFromXContent(json);
|
xContentBuilder.startObject();
|
||||||
|
xContentBuilder.startObject("field1");
|
||||||
|
xContentBuilder.field("field2", 333);
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
ObjectPath objectPath = ObjectPath.createFromXContent(xContentBuilder.string());
|
||||||
Object object = objectPath.evaluate("field1.field2");
|
Object object = objectPath.evaluate("field1.field2");
|
||||||
assertThat(object, instanceOf(Integer.class));
|
assertThat(object, instanceOf(Integer.class));
|
||||||
assertThat((Integer)object, equalTo(333));
|
assertThat(object, equalTo(333));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEvaluateDouble() throws Exception {
|
public void testEvaluateDouble() throws Exception {
|
||||||
String json = "{ \"field1\": { \"field2\" : 3.55 } }";
|
XContentBuilder xContentBuilder = randomXContentBuilder();
|
||||||
ObjectPath objectPath = ObjectPath.createFromXContent(json);
|
xContentBuilder.startObject();
|
||||||
|
xContentBuilder.startObject("field1");
|
||||||
|
xContentBuilder.field("field2", 3.55);
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
ObjectPath objectPath = ObjectPath.createFromXContent(xContentBuilder.string());
|
||||||
Object object = objectPath.evaluate("field1.field2");
|
Object object = objectPath.evaluate("field1.field2");
|
||||||
assertThat(object, instanceOf(Double.class));
|
assertThat(object, instanceOf(Double.class));
|
||||||
assertThat((Double)object, equalTo(3.55));
|
assertThat(object, equalTo(3.55));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEvaluateArray() throws Exception {
|
public void testEvaluateArray() throws Exception {
|
||||||
String json = "{ \"field1\": { \"array1\" : [ \"value1\", \"value2\" ] } }";
|
XContentBuilder xContentBuilder = randomXContentBuilder();
|
||||||
ObjectPath objectPath = ObjectPath.createFromXContent(json);
|
xContentBuilder.startObject();
|
||||||
|
xContentBuilder.startObject("field1");
|
||||||
|
xContentBuilder.array("array1", "value1", "value2");
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
ObjectPath objectPath = ObjectPath.createFromXContent(xContentBuilder.string());
|
||||||
Object object = objectPath.evaluate("field1.array1");
|
Object object = objectPath.evaluate("field1.array1");
|
||||||
assertThat(object, instanceOf(List.class));
|
assertThat(object, instanceOf(List.class));
|
||||||
List list = (List) object;
|
List list = (List) object;
|
||||||
assertThat(list.size(), equalTo(2));
|
assertThat(list.size(), equalTo(2));
|
||||||
assertThat(list.get(0), instanceOf(String.class));
|
assertThat(list.get(0), instanceOf(String.class));
|
||||||
assertThat((String)list.get(0), equalTo("value1"));
|
assertThat(list.get(0), equalTo("value1"));
|
||||||
assertThat(list.get(1), instanceOf(String.class));
|
assertThat(list.get(1), instanceOf(String.class));
|
||||||
assertThat((String)list.get(1), equalTo("value2"));
|
assertThat(list.get(1), equalTo("value2"));
|
||||||
}
|
object = objectPath.evaluate("field1.array1.1");
|
||||||
|
|
||||||
public void testEvaluateArrayElement() throws Exception {
|
|
||||||
String json = "{ \"field1\": { \"array1\" : [ \"value1\", \"value2\" ] } }";
|
|
||||||
ObjectPath objectPath = ObjectPath.createFromXContent(json);
|
|
||||||
Object object = objectPath.evaluate("field1.array1.1");
|
|
||||||
assertThat(object, instanceOf(String.class));
|
assertThat(object, instanceOf(String.class));
|
||||||
assertThat((String)object, equalTo("value2"));
|
assertThat(object, equalTo("value2"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public void testEvaluateArrayElementObject() throws Exception {
|
public void testEvaluateArrayElementObject() throws Exception {
|
||||||
String json = "{ \"field1\": { \"array1\" : [ {\"element\": \"value1\"}, {\"element\":\"value2\"} ] } }";
|
XContentBuilder xContentBuilder = randomXContentBuilder();
|
||||||
ObjectPath objectPath = ObjectPath.createFromXContent(json);
|
xContentBuilder.startObject();
|
||||||
|
xContentBuilder.startObject("field1");
|
||||||
|
xContentBuilder.startArray("array1");
|
||||||
|
xContentBuilder.startObject();
|
||||||
|
xContentBuilder.field("element", "value1");
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.startObject();
|
||||||
|
xContentBuilder.field("element", "value2");
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.endArray();
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
ObjectPath objectPath = ObjectPath.createFromXContent(xContentBuilder.string());
|
||||||
Object object = objectPath.evaluate("field1.array1.1.element");
|
Object object = objectPath.evaluate("field1.array1.1.element");
|
||||||
assertThat(object, instanceOf(String.class));
|
assertThat(object, instanceOf(String.class));
|
||||||
assertThat((String)object, equalTo("value2"));
|
assertThat(object, equalTo("value2"));
|
||||||
}
|
object = objectPath.evaluate("");
|
||||||
|
assertThat(object, notNullValue());
|
||||||
public void testEvaluateArrayElementObjectWrongPath() throws Exception {
|
assertThat(object, instanceOf(Map.class));
|
||||||
String json = "{ \"field1\": { \"array1\" : [ {\"element\": \"value1\"}, {\"element\":\"value2\"} ] } }";
|
assertThat(((Map<String, Object>)object).containsKey("field1"), equalTo(true));
|
||||||
ObjectPath objectPath = ObjectPath.createFromXContent(json);
|
object = objectPath.evaluate("field1.array2.1.element");
|
||||||
Object object = objectPath.evaluate("field1.array2.1.element");
|
|
||||||
assertThat(object, nullValue());
|
assertThat(object, nullValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void testEvaluateObjectKeys() throws Exception {
|
public void testEvaluateObjectKeys() throws Exception {
|
||||||
String json = "{ \"metadata\": { \"templates\" : {\"template_1\": { \"field\" : \"value\"}, \"template_2\": { \"field\" : \"value\"} } } }";
|
XContentBuilder xContentBuilder = randomXContentBuilder();
|
||||||
ObjectPath objectPath = ObjectPath.createFromXContent(json);
|
xContentBuilder.startObject();
|
||||||
|
xContentBuilder.startObject("metadata");
|
||||||
|
xContentBuilder.startObject("templates");
|
||||||
|
xContentBuilder.startObject("template_1");
|
||||||
|
xContentBuilder.field("field", "value");
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.startObject("template_2");
|
||||||
|
xContentBuilder.field("field", "value");
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
ObjectPath objectPath = ObjectPath.createFromXContent(xContentBuilder.string());
|
||||||
Object object = objectPath.evaluate("metadata.templates");
|
Object object = objectPath.evaluate("metadata.templates");
|
||||||
assertThat(object, instanceOf(Map.class));
|
assertThat(object, instanceOf(Map.class));
|
||||||
Map<String, Object> map = (Map<String, Object>)object;
|
Map<String, Object> map = (Map<String, Object>)object;
|
||||||
@ -130,19 +175,16 @@ public class ObjectPathTests extends ESTestCase {
|
|||||||
assertThat(strings, contains("template_1", "template_2"));
|
assertThat(strings, contains("template_1", "template_2"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public void testEvaluateEmptyPath() throws Exception {
|
|
||||||
String json = "{ \"field1\": { \"array1\" : [ {\"element\": \"value1\"}, {\"element\":\"value2\"} ] } }";
|
|
||||||
ObjectPath objectPath = ObjectPath.createFromXContent(json);
|
|
||||||
Object object = objectPath.evaluate("");
|
|
||||||
assertThat(object, notNullValue());
|
|
||||||
assertThat(object, instanceOf(Map.class));
|
|
||||||
assertThat(((Map<String, Object>)object).containsKey("field1"), equalTo(true));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testEvaluateStashInPropertyName() throws Exception {
|
public void testEvaluateStashInPropertyName() throws Exception {
|
||||||
String json = "{ \"field1\": { \"elements\" : {\"element1\": \"value1\"}}}";
|
XContentBuilder xContentBuilder = randomXContentBuilder();
|
||||||
ObjectPath objectPath = ObjectPath.createFromXContent(json);
|
xContentBuilder.startObject();
|
||||||
|
xContentBuilder.startObject("field1");
|
||||||
|
xContentBuilder.startObject("elements");
|
||||||
|
xContentBuilder.field("element1", "value1");
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
xContentBuilder.endObject();
|
||||||
|
ObjectPath objectPath = ObjectPath.createFromXContent(xContentBuilder.string());
|
||||||
try {
|
try {
|
||||||
objectPath.evaluate("field1.$placeholder.element1");
|
objectPath.evaluate("field1.$placeholder.element1");
|
||||||
fail("evaluate should have failed due to unresolved placeholder");
|
fail("evaluate should have failed due to unresolved placeholder");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user