use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class ReplicationResponseTests method testShardInfoToAndFromXContent.
public void testShardInfoToAndFromXContent() throws IOException {
final Tuple<ShardInfo, ShardInfo> tuple = RandomObjects.randomShardInfo(random());
ShardInfo shardInfo = tuple.v1();
ShardInfo expectedShardInfo = tuple.v2();
final XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(shardInfo, xContentType, humanReadable);
// Shuffle the XContent fields
if (randomBoolean()) {
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
originalBytes = shuffleXContent(parser, randomBoolean()).bytes();
}
}
ShardInfo parsedShardInfo;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
// Move to the first start object
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
parsedShardInfo = ShardInfo.fromXContent(parser);
assertNull(parser.nextToken());
}
assertShardInfo(expectedShardInfo, parsedShardInfo);
BytesReference expectedFinalBytes = toXContent(expectedShardInfo, xContentType, humanReadable);
BytesReference finalBytes = toXContent(parsedShardInfo, xContentType, humanReadable);
assertToXContentEquivalent(expectedFinalBytes, finalBytes, xContentType);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class ClientYamlTestExecutionContext method createEntity.
private HttpEntity createEntity(List<Map<String, Object>> bodies, Map<String, String> headers) throws IOException {
if (bodies.isEmpty()) {
return null;
}
if (bodies.size() == 1) {
XContentType xContentType = getContentType(headers, XContentType.values());
BytesRef bytesRef = bodyAsBytesRef(bodies.get(0), xContentType);
return new ByteArrayEntity(bytesRef.bytes, bytesRef.offset, bytesRef.length, ContentType.create(xContentType.mediaTypeWithoutParameters(), StandardCharsets.UTF_8));
} else {
XContentType xContentType = getContentType(headers, STREAMING_CONTENT_TYPES);
List<BytesRef> bytesRefList = new ArrayList<>();
int totalBytesLength = 0;
for (Map<String, Object> body : bodies) {
BytesRef bytesRef = bodyAsBytesRef(body, xContentType);
bytesRefList.add(bytesRef);
totalBytesLength += bytesRef.length - bytesRef.offset + 1;
}
byte[] bytes = new byte[totalBytesLength];
int position = 0;
for (BytesRef bytesRef : bytesRefList) {
for (int i = bytesRef.offset; i < bytesRef.length; i++) {
bytes[position++] = bytesRef.bytes[i];
}
bytes[position++] = xContentType.xContent().streamSeparator();
}
return new ByteArrayEntity(bytes, ContentType.create(xContentType.mediaTypeWithoutParameters(), StandardCharsets.UTF_8));
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class ClientYamlTestExecutionContext method getContentType.
private XContentType getContentType(Map<String, String> headers, XContentType[] supportedContentTypes) {
XContentType xContentType = null;
String contentType = headers.get("Content-Type");
if (contentType != null) {
xContentType = XContentType.fromMediaType(contentType);
}
if (xContentType != null) {
return xContentType;
}
if (randomizeContentType) {
return RandomizedTest.randomFrom(supportedContentTypes);
}
return XContentType.JSON;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class AbstractSerializingTestCase method testFromXContent.
/**
* Generic test that creates new instance from the test instance and checks
* both for equality and asserts equality on the two instances.
*/
public void testFromXContent() throws IOException {
for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) {
T testInstance = createTestInstance();
XContentType xContentType = randomFrom(XContentType.values());
XContentBuilder builder = toXContent(testInstance, xContentType);
XContentBuilder shuffled = shuffleXContent(builder);
assertParsedInstance(xContentType, shuffled.bytes(), testInstance);
for (Map.Entry<String, T> alternateVersion : getAlternateVersions().entrySet()) {
String instanceAsString = alternateVersion.getKey();
assertParsedInstance(XContentType.JSON, new BytesArray(instanceAsString), alternateVersion.getValue());
}
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class AbstractStreamableXContentTestCase method testFromXContent.
/**
* Generic test that creates new instance from the test instance and checks
* both for equality and asserts equality on the two queries.
*/
public void testFromXContent() throws IOException {
for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) {
T testInstance = createTestInstance();
XContentType xContentType = randomFrom(XContentType.values());
XContentBuilder builder = toXContent(testInstance, xContentType);
XContentBuilder shuffled = shuffleXContent(builder);
assertParsedInstance(xContentType, shuffled.bytes(), testInstance);
for (Map.Entry<String, T> alternateVersion : getAlternateVersions().entrySet()) {
String instanceAsString = alternateVersion.getKey();
assertParsedInstance(XContentType.JSON, new BytesArray(instanceAsString), alternateVersion.getValue());
}
}
}
Aggregations