Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.test;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ChunkedToXContent;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.test.rest.ObjectPath;
import org.elasticsearch.xcontent.DeprecationHandler;
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContent;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.json.JsonXContent;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Stack;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import static com.carrotsearch.randomizedtesting.generators.RandomStrings.randomAsciiOfLength;
import static org.elasticsearch.common.xcontent.XContentHelper.createParser;
import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;
public final class XContentTestUtils {
private XContentTestUtils() {
}
public static Map convertToMap(ChunkedToXContent chunkedToXContent) throws IOException {
return convertToMap(ChunkedToXContent.wrapAsToXContent(chunkedToXContent));
}
public static Map convertToMap(ToXContent part) throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
if (part.isFragment()) {
builder.startObject();
part.toXContent(builder, EMPTY_PARAMS);
builder.endObject();
} else {
part.toXContent(builder, EMPTY_PARAMS);
}
return XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
}
public static BytesReference convertToXContent(Map map, XContentType xContentType) throws IOException {
try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType)) {
builder.map(map);
return BytesReference.bytes(builder);
}
}
/**
* Compares two maps generated from XContentObjects. The order of elements in arrays is ignored.
*
* @return null if maps are equal or path to the element where the difference was found
*/
public static String differenceBetweenMapsIgnoringArrayOrder(Map first, Map second) {
return differenceBetweenMapsIgnoringArrayOrder("", first, second);
}
private static String differenceBetweenMapsIgnoringArrayOrder(String path, Map first, Map second) {
if (first.size() != second.size()) {
return path + ": sizes of the maps don't match: " + first.size() + " != " + second.size();
}
for (String key : first.keySet()) {
String reason = differenceBetweenObjectsIgnoringArrayOrder(path + "/" + key, first.get(key), second.get(key));
if (reason != null) {
return reason;
}
}
return null;
}
@SuppressWarnings("unchecked")
private static String differenceBetweenObjectsIgnoringArrayOrder(String path, Object first, Object second) {
if (first == null) {
if (second == null) {
return null;
} else {
return path + ": first element is null, the second element is not null";
}
} else if (first instanceof List) {
if (second instanceof List) {
List