Search in sources :

Example 81 with XContentType

use of 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 82 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class GetResultTests method testGetSourceAsBytes.

public void testGetSourceAsBytes() {
    XContentType xContentType = randomFrom(XContentType.values());
    Tuple<GetResult, GetResult> tuple = randomGetResult(xContentType);
    GetResult getResult = tuple.v1();
    if (getResult.isExists() && getResult.isSourceEmpty() == false) {
        assertNotNull(getResult.sourceRef());
    } else {
        assertNull(getResult.sourceRef());
    }
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType)

Aggregations

XContentType (org.elasticsearch.common.xcontent.XContentType)82 BytesReference (org.elasticsearch.common.bytes.BytesReference)48 XContentParser (org.elasticsearch.common.xcontent.XContentParser)42 Map (java.util.Map)19 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)19 HashMap (java.util.HashMap)18 IndexRequest (org.elasticsearch.action.index.IndexRequest)11 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)10 IOException (java.io.IOException)9 BytesRef (org.apache.lucene.util.BytesRef)8 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)8 Collections.emptyMap (java.util.Collections.emptyMap)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 WriteRequest (org.elasticsearch.action.support.WriteRequest)5