Search in sources :

Example 21 with StreamInput

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

the class SimulateProcessorResultTests method testSerialization.

public void testSerialization() throws IOException {
    String processorTag = randomAsciiOfLengthBetween(1, 10);
    boolean isSuccessful = randomBoolean();
    boolean isIgnoredException = randomBoolean();
    SimulateProcessorResult simulateProcessorResult;
    if (isSuccessful) {
        IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
        if (isIgnoredException) {
            simulateProcessorResult = new SimulateProcessorResult(processorTag, ingestDocument, new IllegalArgumentException("test"));
        } else {
            simulateProcessorResult = new SimulateProcessorResult(processorTag, ingestDocument);
        }
    } else {
        simulateProcessorResult = new SimulateProcessorResult(processorTag, new IllegalArgumentException("test"));
    }
    BytesStreamOutput out = new BytesStreamOutput();
    simulateProcessorResult.writeTo(out);
    StreamInput streamInput = out.bytes().streamInput();
    SimulateProcessorResult otherSimulateProcessorResult = new SimulateProcessorResult(streamInput);
    assertThat(otherSimulateProcessorResult.getProcessorTag(), equalTo(simulateProcessorResult.getProcessorTag()));
    if (isSuccessful) {
        assertIngestDocument(otherSimulateProcessorResult.getIngestDocument(), simulateProcessorResult.getIngestDocument());
        if (isIgnoredException) {
            assertThat(otherSimulateProcessorResult.getFailure(), instanceOf(IllegalArgumentException.class));
            IllegalArgumentException e = (IllegalArgumentException) otherSimulateProcessorResult.getFailure();
            assertThat(e.getMessage(), equalTo("test"));
        } else {
            assertThat(otherSimulateProcessorResult.getFailure(), nullValue());
        }
    } else {
        assertThat(otherSimulateProcessorResult.getIngestDocument(), is(nullValue()));
        assertThat(otherSimulateProcessorResult.getFailure(), instanceOf(IllegalArgumentException.class));
        IllegalArgumentException e = (IllegalArgumentException) otherSimulateProcessorResult.getFailure();
        assertThat(e.getMessage(), equalTo("test"));
    }
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 22 with StreamInput

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

the class WritePipelineResponseTests method testSerializationWithError.

public void testSerializationWithError() throws IOException {
    WritePipelineResponse response = new WritePipelineResponse();
    BytesStreamOutput out = new BytesStreamOutput();
    response.writeTo(out);
    StreamInput streamInput = out.bytes().streamInput();
    WritePipelineResponse otherResponse = new WritePipelineResponse();
    otherResponse.readFrom(streamInput);
    assertThat(otherResponse.isAcknowledged(), equalTo(response.isAcknowledged()));
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 23 with StreamInput

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

the class WriteableIngestDocumentTests method testSerialization.

public void testSerialization() throws IOException {
    Map<String, Object> sourceAndMetadata = RandomDocumentPicks.randomSource(random());
    int numFields = randomIntBetween(1, IngestDocument.MetaData.values().length);
    for (int i = 0; i < numFields; i++) {
        sourceAndMetadata.put(randomFrom(IngestDocument.MetaData.values()).getFieldName(), randomAsciiOfLengthBetween(5, 10));
    }
    Map<String, Object> ingestMetadata = new HashMap<>();
    numFields = randomIntBetween(1, 5);
    for (int i = 0; i < numFields; i++) {
        ingestMetadata.put(randomAsciiOfLengthBetween(5, 10), randomAsciiOfLengthBetween(5, 10));
    }
    WriteableIngestDocument writeableIngestDocument = new WriteableIngestDocument(new IngestDocument(sourceAndMetadata, ingestMetadata));
    BytesStreamOutput out = new BytesStreamOutput();
    writeableIngestDocument.writeTo(out);
    StreamInput streamInput = out.bytes().streamInput();
    WriteableIngestDocument otherWriteableIngestDocument = new WriteableIngestDocument(streamInput);
    assertIngestDocument(otherWriteableIngestDocument.getIngestDocument(), writeableIngestDocument.getIngestDocument());
}
Also used : HashMap(java.util.HashMap) StreamInput(org.elasticsearch.common.io.stream.StreamInput) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 24 with StreamInput

use of org.elasticsearch.common.io.stream.StreamInput 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 25 with StreamInput

use of org.elasticsearch.common.io.stream.StreamInput 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)

Aggregations

StreamInput (org.elasticsearch.common.io.stream.StreamInput)183 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)146 Test (org.junit.Test)52 CrateUnitTest (io.crate.test.integration.CrateUnitTest)37 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)30 BytesArray (org.elasticsearch.common.bytes.BytesArray)24 Version (org.elasticsearch.Version)21 IOException (java.io.IOException)13 BytesReference (org.elasticsearch.common.bytes.BytesReference)10 UUID (java.util.UUID)9 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)9 Symbol (io.crate.analyze.symbol.Symbol)8 BytesRef (org.apache.lucene.util.BytesRef)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ShardId (org.elasticsearch.index.shard.ShardId)6 List (java.util.List)5 AliasFilter (org.elasticsearch.search.internal.AliasFilter)5 Aggregation (io.crate.analyze.symbol.Aggregation)4