Package org.elasticsearch.test
Class XContentTestUtils
java.lang.Object
org.elasticsearch.test.XContentTestUtils
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionconvertToMap(ChunkedToXContent chunkedToXContent) convertToMap(org.elasticsearch.xcontent.ToXContent part) static BytesReferenceconvertToXContent(Map<String, ?> map, org.elasticsearch.xcontent.XContentType xContentType) createJsonMapView(InputStream inputStream) static StringCompares two maps generated from XContentObjects.static org.elasticsearch.xcontent.XContentBuilderinsertIntoXContent(org.elasticsearch.xcontent.XContent xContent, BytesReference original, List<String> paths, Supplier<String> key, Supplier<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 BytesReferenceinsertRandomFields(org.elasticsearch.xcontent.XContentType contentType, BytesReference xContent, Predicate<String> excludeFilter, 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 Map<String,Object> convertToMap(ChunkedToXContent chunkedToXContent) throws IOException - Throws:
IOException
-
convertToMap
public static Map<String,Object> convertToMap(org.elasticsearch.xcontent.ToXContent part) throws IOException - Throws:
IOException
-
convertToXContent
public static BytesReference convertToXContent(Map<String, ?> map, org.elasticsearch.xcontent.XContentType xContentType) throws IOException- Throws:
IOException
-
differenceBetweenMapsIgnoringArrayOrder
public static String differenceBetweenMapsIgnoringArrayOrder(Map<String, Object> first, Map<String, 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 BytesReference insertRandomFields(org.elasticsearch.xcontent.XContentType contentType, BytesReference xContent, Predicate<String> excludeFilter, Random random) throws IOException This 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:
IOException
-
insertIntoXContent
public static org.elasticsearch.xcontent.XContentBuilder insertIntoXContent(org.elasticsearch.xcontent.XContent xContent, BytesReference original, List<String> paths, Supplier<String> key, Supplier<Object> value) throws IOException 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. The key/value arguments can suppliers that either return fixed or random values.- Throws:
IOException
-
createJsonMapView
-