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.
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.test;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.xcontent.DeprecationHandler;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.test.rest.yaml.ObjectPath;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
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 org.opensearch.common.xcontent.XContentHelper.createParser;
import static org.opensearch.core.xcontent.ToXContent.EMPTY_PARAMS;
import static com.carrotsearch.randomizedtesting.generators.RandomStrings.randomAsciiOfLength;
public final class XContentTestUtils {
private XContentTestUtils() {
}
public static Map convertToMap(ToXContent part) throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
part.toXContent(builder, EMPTY_PARAMS);
builder.endObject();
return XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
}
public static BytesReference convertToXContent(Map map, XContentType xContentType) throws IOException {
try (XContentBuilder builder = MediaTypeRegistry.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