Package org.elasticsearch.test
Class XContentTestUtils
java.lang.Object
org.elasticsearch.test.XContentTestUtils
public final class XContentTestUtils
extends java.lang.Object
-
Method Summary
Modifier and Type Method Description static java.util.Map<java.lang.String,java.lang.Object>convertToMap(org.elasticsearch.common.xcontent.ToXContent part)static org.elasticsearch.common.bytes.BytesReferenceconvertToXContent(java.util.Map<java.lang.String,?> map, org.elasticsearch.common.xcontent.XContentType xContentType)static java.lang.StringdifferenceBetweenMapsIgnoringArrayOrder(java.util.Map<java.lang.String,java.lang.Object> first, java.util.Map<java.lang.String,java.lang.Object> second)Compares two maps generated from XContentObjects.static org.elasticsearch.common.xcontent.XContentBuilderinsertIntoXContent(org.elasticsearch.common.xcontent.XContent xContent, org.elasticsearch.common.bytes.BytesReference original, java.util.List<java.lang.String> paths, java.util.function.Supplier<java.lang.String> key, java.util.function.Supplier<java.lang.Object> value)Inserts key/value pairs into xContent passed in asBytesReferenceand returns a newXContentBuilderThe paths argument uses dot separated fieldnames and numbers for array indices, similar to what we do inObjectPath.static org.elasticsearch.common.bytes.BytesReferenceinsertRandomFields(org.elasticsearch.common.xcontent.XContentType contentType, org.elasticsearch.common.bytes.BytesReference xContent, java.util.function.Predicate<java.lang.String> excludeFilter, java.util.Random random)This method takes the input xContent data and adds a random field value, inner object or array into each json object.
-
Method Details
-
convertToMap
public static java.util.Map<java.lang.String,java.lang.Object> convertToMap(org.elasticsearch.common.xcontent.ToXContent part) throws java.io.IOException- Throws:
java.io.IOException
-
convertToXContent
public static org.elasticsearch.common.bytes.BytesReference convertToXContent(java.util.Map<java.lang.String,?> map, org.elasticsearch.common.xcontent.XContentType xContentType) throws java.io.IOException- Throws:
java.io.IOException
-
differenceBetweenMapsIgnoringArrayOrder
public static java.lang.String differenceBetweenMapsIgnoringArrayOrder(java.util.Map<java.lang.String,java.lang.Object> first, java.util.Map<java.lang.String,java.lang.Object> second)Compares two maps generated from XContentObjects. The order of elements in arrays is ignored.- Returns:
- null if maps are equal or path to the element where the difference was found
-
insertRandomFields
public static org.elasticsearch.common.bytes.BytesReference insertRandomFields(org.elasticsearch.common.xcontent.XContentType contentType, org.elasticsearch.common.bytes.BytesReference xContent, java.util.function.Predicate<java.lang.String> excludeFilter, java.util.Random random) throws java.io.IOExceptionThis method takes the input xContent data and adds a random field value, inner object or array into each json object. This can e.g. be used to test if parsers that handle the resulting xContent can handle the augmented xContent correctly, for example when testing lenient parsing. If the xContent output contains objects that should be skipped of such treatment, an optional filteringPredicatecan be supplied that checks xContent paths that should be excluded from this treatment. This predicate should check the xContent path that we want to insert to and returntrueif the path should be excluded. Paths are string concatenating field names and array indices, so e.g. in:{ "foo1 : { "bar" : [ { ... }, { ... }, { "baz" : { // insert here } } ] } }"foo1.bar.2.baz" would point to the desired insert location. To exclude inserting into the "foo1" object we would user aPredicatelike
or if we don't want any random insertions in the "foo1" tree we could use(path) -> path.endsWith("foo1")(path) -> path.contains("foo1")- Throws:
java.io.IOException
-
insertIntoXContent
public static org.elasticsearch.common.xcontent.XContentBuilder insertIntoXContent(org.elasticsearch.common.xcontent.XContent xContent, org.elasticsearch.common.bytes.BytesReference original, java.util.List<java.lang.String> paths, java.util.function.Supplier<java.lang.String> key, java.util.function.Supplier<java.lang.Object> value) throws java.io.IOExceptionInserts key/value pairs into xContent passed in asBytesReferenceand returns a newXContentBuilderThe paths argument uses dot separated fieldnames and numbers for array indices, similar to what we do inObjectPath. The key/value arguments can suppliers that either return fixed or random values.- Throws:
java.io.IOException
-