Search in sources :

Example 31 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class IndexRequestTests method testSerializationOfEmptyRequestWorks.

// reindex makes use of index requests without a source so this needs to be handled
public void testSerializationOfEmptyRequestWorks() throws IOException {
    IndexRequest request = new IndexRequest("index", "type");
    assertNull(request.getContentType());
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        request.writeTo(out);
        try (StreamInput in = out.bytes().streamInput()) {
            IndexRequest serialized = new IndexRequest();
            serialized.readFrom(in);
            assertNull(request.getContentType());
            assertEquals("index", request.index());
            assertEquals("type", request.type());
        }
    }
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 32 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class IndexRequestTests method testIndexRequestXContentSerialization.

public void testIndexRequestXContentSerialization() throws IOException {
    IndexRequest indexRequest = new IndexRequest("foo", "bar", "1");
    indexRequest.source("{}", XContentType.JSON);
    assertEquals(XContentType.JSON, indexRequest.getContentType());
    BytesStreamOutput out = new BytesStreamOutput();
    indexRequest.writeTo(out);
    StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes);
    IndexRequest serialized = new IndexRequest();
    serialized.readFrom(in);
    assertEquals(XContentType.JSON, serialized.getContentType());
    assertEquals(new BytesArray("{}"), serialized.source());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 33 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class PutPipelineRequestTests method testSerializationWithXContent.

public void testSerializationWithXContent() throws IOException {
    PutPipelineRequest request = new PutPipelineRequest("1", new BytesArray("{}".getBytes(StandardCharsets.UTF_8)), XContentType.JSON);
    assertEquals(XContentType.JSON, request.getXContentType());
    BytesStreamOutput output = new BytesStreamOutput();
    request.writeTo(output);
    StreamInput in = StreamInput.wrap(output.bytes().toBytesRef().bytes);
    PutPipelineRequest serialized = new PutPipelineRequest();
    serialized.readFrom(in);
    assertEquals(XContentType.JSON, serialized.getXContentType());
    assertEquals("{}", serialized.getSource().utf8ToString());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 34 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class PutPipelineRequestTests method testSerializationBwc.

public void testSerializationBwc() throws IOException {
    final byte[] data = Base64.getDecoder().decode("ADwDATECe30=");
    final Version version = randomFrom(Version.V_5_0_0, Version.V_5_0_1, Version.V_5_0_2, Version.V_5_0_3_UNRELEASED, Version.V_5_1_1_UNRELEASED, Version.V_5_1_2_UNRELEASED, Version.V_5_2_0_UNRELEASED);
    try (StreamInput in = StreamInput.wrap(data)) {
        in.setVersion(version);
        PutPipelineRequest request = new PutPipelineRequest();
        request.readFrom(in);
        assertEquals(XContentType.JSON, request.getXContentType());
        assertEquals("{}", request.getSource().utf8ToString());
        try (BytesStreamOutput out = new BytesStreamOutput()) {
            out.setVersion(version);
            request.writeTo(out);
            assertArrayEquals(data, out.bytes().toBytesRef().bytes);
        }
    }
}
Also used : Version(org.elasticsearch.Version) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 35 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class IndexGraveyardTests method testSerialization.

public void testSerialization() throws IOException {
    final IndexGraveyard graveyard = createRandom();
    final BytesStreamOutput out = new BytesStreamOutput();
    graveyard.writeTo(out);
    assertThat(new IndexGraveyard(out.bytes().streamInput()), equalTo(graveyard));
}
Also used : BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)222 StreamInput (org.elasticsearch.common.io.stream.StreamInput)147 Test (org.junit.Test)45 CrateUnitTest (io.crate.test.integration.CrateUnitTest)36 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)24 IOException (java.io.IOException)21 BytesArray (org.elasticsearch.common.bytes.BytesArray)21 BytesReference (org.elasticsearch.common.bytes.BytesReference)18 Version (org.elasticsearch.Version)15 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 ArrayList (java.util.ArrayList)12 BytesRef (org.apache.lucene.util.BytesRef)11 Map (java.util.Map)10 UUID (java.util.UUID)9 Symbol (io.crate.analyze.symbol.Symbol)8 HashMap (java.util.HashMap)8 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)8 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)7 List (java.util.List)6