Search in sources :

Example 76 with XContentType

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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ShardInfo(org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo)

Example 77 with 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));
    }
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) ArrayList(java.util.ArrayList) BytesRef(org.apache.lucene.util.BytesRef)

Example 78 with XContentType

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;
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType)

Example 79 with XContentType

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());
        }
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) XContentType(org.elasticsearch.common.xcontent.XContentType) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 80 with XContentType

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());
        }
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) XContentType(org.elasticsearch.common.xcontent.XContentType) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

XContentType (org.elasticsearch.common.xcontent.XContentType)86 BytesReference (org.elasticsearch.common.bytes.BytesReference)50 XContentParser (org.elasticsearch.common.xcontent.XContentParser)45 Map (java.util.Map)20 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)20 HashMap (java.util.HashMap)14 IndexRequest (org.elasticsearch.action.index.IndexRequest)11 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)10 IOException (java.io.IOException)9 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)8 ArrayList (java.util.ArrayList)6 Collections.emptyMap (java.util.Collections.emptyMap)6 List (java.util.List)6 ElasticsearchException (org.elasticsearch.ElasticsearchException)6 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)6 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)6 Collections.singletonMap (java.util.Collections.singletonMap)5 HttpEntity (org.apache.http.HttpEntity)5 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)5 GetRequest (org.elasticsearch.action.get.GetRequest)5