use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.
the class MultiGetShardRequestTests method testSerialization.
public void testSerialization() throws IOException {
MultiGetRequest multiGetRequest = new MultiGetRequest();
if (randomBoolean()) {
multiGetRequest.preference(randomAlphaOfLength(randomIntBetween(1, 10)));
}
if (randomBoolean()) {
multiGetRequest.realtime(false);
}
if (randomBoolean()) {
multiGetRequest.refresh(true);
}
MultiGetShardRequest multiGetShardRequest = new MultiGetShardRequest(multiGetRequest, "index", 0);
int numItems = iterations(10, 30);
for (int i = 0; i < numItems; i++) {
MultiGetRequest.Item item = new MultiGetRequest.Item("alias-" + randomAlphaOfLength(randomIntBetween(1, 10)), "id-" + i);
if (randomBoolean()) {
int numFields = randomIntBetween(1, 5);
String[] fields = new String[numFields];
for (int j = 0; j < fields.length; j++) {
fields[j] = randomAlphaOfLength(randomIntBetween(1, 10));
}
item.storedFields(fields);
}
if (randomBoolean()) {
item.version(randomIntBetween(1, Integer.MAX_VALUE));
item.versionType(randomFrom(VersionType.values()));
}
if (randomBoolean()) {
item.fetchSourceContext(new FetchSourceContext(randomBoolean()));
}
multiGetShardRequest.add(0, item);
}
BytesStreamOutput out = new BytesStreamOutput();
out.setVersion(randomVersion(random()));
multiGetShardRequest.writeTo(out);
StreamInput in = out.bytes().streamInput();
in.setVersion(out.getVersion());
MultiGetShardRequest multiGetShardRequest2 = new MultiGetShardRequest(in);
assertThat(multiGetShardRequest2.index(), equalTo(multiGetShardRequest.index()));
assertThat(multiGetShardRequest2.preference(), equalTo(multiGetShardRequest.preference()));
assertThat(multiGetShardRequest2.realtime(), equalTo(multiGetShardRequest.realtime()));
assertThat(multiGetShardRequest2.refresh(), equalTo(multiGetShardRequest.refresh()));
assertThat(multiGetShardRequest2.items.size(), equalTo(multiGetShardRequest.items.size()));
for (int i = 0; i < multiGetShardRequest2.items.size(); i++) {
MultiGetRequest.Item item = multiGetShardRequest.items.get(i);
MultiGetRequest.Item item2 = multiGetShardRequest2.items.get(i);
assertThat(item2.index(), equalTo(item.index()));
assertThat(item2.id(), equalTo(item.id()));
assertThat(item2.storedFields(), equalTo(item.storedFields()));
assertThat(item2.version(), equalTo(item.version()));
assertThat(item2.versionType(), equalTo(item.versionType()));
assertThat(item2.fetchSourceContext(), equalTo(item.fetchSourceContext()));
}
assertThat(multiGetShardRequest2.indices(), equalTo(multiGetShardRequest.indices()));
assertThat(multiGetShardRequest2.indicesOptions(), equalTo(multiGetShardRequest.indicesOptions()));
}
use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.
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(in);
assertEquals(XContentType.JSON, serialized.getXContentType());
assertEquals("{}", serialized.getSource().utf8ToString());
}
use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.
the class DiscoveryNodeTests method testDiscoveryNodeSerializationKeepsHost.
public void testDiscoveryNodeSerializationKeepsHost() throws Exception {
InetAddress inetAddress = InetAddress.getByAddress("name1", new byte[] { (byte) 192, (byte) 168, (byte) 0, (byte) 1 });
TransportAddress transportAddress = new TransportAddress(inetAddress, randomIntBetween(0, 65535));
DiscoveryNode node = new DiscoveryNode("name1", "id1", transportAddress, emptyMap(), emptySet(), Version.CURRENT);
BytesStreamOutput streamOutput = new BytesStreamOutput();
streamOutput.setVersion(Version.CURRENT);
node.writeTo(streamOutput);
StreamInput in = StreamInput.wrap(streamOutput.bytes().toBytesRef().bytes);
DiscoveryNode serialized = new DiscoveryNode(in);
assertEquals(transportAddress.address().getHostString(), serialized.getHostName());
assertEquals(transportAddress.address().getHostString(), serialized.getAddress().address().getHostString());
assertEquals(transportAddress.getAddress(), serialized.getHostAddress());
assertEquals(transportAddress.getAddress(), serialized.getAddress().getAddress());
assertEquals(transportAddress.getPort(), serialized.getAddress().getPort());
}
use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.
the class GeometryIOTests method testRandomSerialization.
public void testRandomSerialization() throws Exception {
for (int i = 0; i < randomIntBetween(1, 20); i++) {
boolean hasAlt = randomBoolean();
Geometry geometry = randomGeometry(hasAlt);
if (shapeSupported(geometry) && randomBoolean()) {
// Shape builder conversion doesn't support altitude
ShapeBuilder<?, ?, ?> shapeBuilder = geometryToShapeBuilder(geometry);
if (randomBoolean()) {
Geometry actual = shapeBuilder.buildGeometry();
assertEquals(geometry, actual);
}
if (randomBoolean()) {
// Test ShapeBuilder -> Geometry Serialization
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.writeNamedWriteable(shapeBuilder);
try (StreamInput in = out.bytes().streamInput()) {
Geometry actual = GeometryIO.readGeometry(in);
assertEquals(geometry, actual);
assertEquals(0, in.available());
}
}
} else {
// Test Geometry -> ShapeBuilder Serialization
try (BytesStreamOutput out = new BytesStreamOutput()) {
GeometryIO.writeGeometry(out, geometry);
try (StreamInput in = out.bytes().streamInput()) {
try (StreamInput nin = new NamedWriteableAwareStreamInput(in, this.writableRegistry())) {
ShapeBuilder<?, ?, ?> actual = nin.readNamedWriteable(ShapeBuilder.class);
assertEquals(shapeBuilder, actual);
assertEquals(0, in.available());
}
}
}
}
// Test Geometry -> Geometry
try (BytesStreamOutput out = new BytesStreamOutput()) {
GeometryIO.writeGeometry(out, geometry);
;
try (StreamInput in = out.bytes().streamInput()) {
Geometry actual = GeometryIO.readGeometry(in);
assertEquals(geometry, actual);
assertEquals(0, in.available());
}
}
}
}
}
use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.
the class FieldMemoryStatsTests method testSerialize.
public void testSerialize() throws IOException {
FieldMemoryStats stats = randomFieldMemoryStats();
BytesStreamOutput out = new BytesStreamOutput();
stats.writeTo(out);
StreamInput input = out.bytes().streamInput();
FieldMemoryStats read = new FieldMemoryStats(input);
assertEquals(-1, input.read());
assertEquals(stats, read);
}
Aggregations