use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class ExceptionSerializationTests method serialize.
private <T extends Exception> T serialize(T exception, Version version) throws IOException {
ElasticsearchAssertions.assertVersionSerializable(version, exception);
BytesStreamOutput out = new BytesStreamOutput();
out.setVersion(version);
out.writeException(exception);
StreamInput in = out.bytes().streamInput();
in.setVersion(version);
return in.readException();
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class ExplainRequestTests method testSerialize50Request.
// BWC test for changes from #20916
public void testSerialize50Request() throws IOException {
ExplainRequest request = new ExplainRequest("index", "type", "id");
request.fetchSourceContext(new FetchSourceContext(true, new String[] { "field1.*" }, new String[] { "field2.*" }));
request.filteringAlias(new AliasFilter(QueryBuilders.termQuery("filter_field", "value"), new String[] { "alias0", "alias1" }));
request.preference("the_preference");
request.query(QueryBuilders.termQuery("field", "value"));
request.storedFields(new String[] { "field1", "field2" });
request.routing("some_routing");
BytesArray requestBytes = new BytesArray(Base64.getDecoder().decode("AAABBWluZGV4BHR5cGUCaWQBDHNvbWVfcm91dGluZwEOdGhlX3ByZWZlcmVuY2UEdGVybT" + "+AAAAABWZpZWxkFQV2YWx1ZQIGYWxpYXMwBmFsaWFzMQECBmZpZWxkMQZmaWVsZDIBAQEIZmllbGQxLioBCGZpZWxkMi4qAA"));
try (StreamInput in = new NamedWriteableAwareStreamInput(requestBytes.streamInput(), namedWriteableRegistry)) {
in.setVersion(Version.V_5_0_0);
ExplainRequest readRequest = new ExplainRequest();
readRequest.readFrom(in);
assertEquals(0, in.available());
assertArrayEquals(request.filteringAlias().getAliases(), readRequest.filteringAlias().getAliases());
expectThrows(IllegalStateException.class, () -> readRequest.filteringAlias().getQueryBuilder());
assertArrayEquals(request.storedFields(), readRequest.storedFields());
assertEquals(request.preference(), readRequest.preference());
assertEquals(request.query(), readRequest.query());
assertEquals(request.routing(), readRequest.routing());
assertEquals(request.fetchSourceContext(), readRequest.fetchSourceContext());
BytesStreamOutput output = new BytesStreamOutput();
output.setVersion(Version.V_5_0_0);
readRequest.writeTo(output);
assertEquals(output.bytes().toBytesRef(), requestBytes.toBytesRef());
}
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class BulkRequestTests method testSmileIsSupported.
public void testSmileIsSupported() throws IOException {
XContentType xContentType = XContentType.SMILE;
BytesReference data;
try (BytesStreamOutput out = new BytesStreamOutput()) {
try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, out)) {
builder.startObject();
builder.startObject("index");
builder.field("_index", "index");
builder.field("_type", "type");
builder.field("_id", "test");
builder.endObject();
builder.endObject();
}
out.write(xContentType.xContent().streamSeparator());
try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, out)) {
builder.startObject();
builder.field("field", "value");
builder.endObject();
}
out.write(xContentType.xContent().streamSeparator());
data = out.bytes();
}
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(data, null, null, xContentType);
assertEquals(1, bulkRequest.requests().size());
DocWriteRequest docWriteRequest = bulkRequest.requests().get(0);
assertEquals(DocWriteRequest.OpType.INDEX, docWriteRequest.opType());
assertEquals("index", docWriteRequest.index());
assertEquals("type", docWriteRequest.type());
assertEquals("test", docWriteRequest.id());
assertThat(docWriteRequest, instanceOf(IndexRequest.class));
IndexRequest request = (IndexRequest) docWriteRequest;
assertEquals(1, request.sourceAsMap().size());
assertEquals("value", request.sourceAsMap().get("field"));
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class PutIndexTemplateRequestTests method testPutIndexTemplateRequestSerializationXContent.
public void testPutIndexTemplateRequestSerializationXContent() throws IOException {
PutIndexTemplateRequest request = new PutIndexTemplateRequest("foo");
String mapping = YamlXContent.contentBuilder().startObject().field("foo", "bar").endObject().string();
request.patterns(Collections.singletonList("foo"));
request.mapping("bar", mapping, XContentType.YAML);
assertNotEquals(mapping, request.mappings().get("bar"));
assertEquals(XContentHelper.convertToJson(new BytesArray(mapping), false, XContentType.YAML), request.mappings().get("bar"));
BytesStreamOutput out = new BytesStreamOutput();
request.writeTo(out);
StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes);
PutIndexTemplateRequest serialized = new PutIndexTemplateRequest();
serialized.readFrom(in);
assertEquals(XContentHelper.convertToJson(new BytesArray(mapping), false, XContentType.YAML), serialized.mappings().get("bar"));
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class GetFieldMappingsResponseTests method testSerialization.
public void testSerialization() throws IOException {
Map<String, Map<String, Map<String, FieldMappingMetaData>>> mappings = new HashMap<>();
FieldMappingMetaData fieldMappingMetaData = new FieldMappingMetaData("my field", new BytesArray("{}"));
mappings.put("index", Collections.singletonMap("type", Collections.singletonMap("field", fieldMappingMetaData)));
GetFieldMappingsResponse response = new GetFieldMappingsResponse(mappings);
try (BytesStreamOutput out = new BytesStreamOutput()) {
response.writeTo(out);
GetFieldMappingsResponse serialized = new GetFieldMappingsResponse();
try (StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes)) {
serialized.readFrom(in);
FieldMappingMetaData metaData = serialized.fieldMappings("index", "type", "field");
assertNotNull(metaData);
assertEquals(new BytesArray("{}"), metaData.getSource());
}
}
}
Aggregations