use of org.opensearch.common.bytes.BytesReference in project OpenSearch by opensearch-project.
the class ManifestTests method testXContent.
public void testXContent() throws IOException {
Manifest state = randomManifest();
final XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject();
Manifest.FORMAT.toXContent(builder, state);
builder.endObject();
BytesReference bytes = BytesReference.bytes(builder);
try (XContentParser parser = createParser(JsonXContent.jsonXContent, bytes)) {
assertThat(Manifest.fromXContent(parser), equalTo(state));
}
}
use of org.opensearch.common.bytes.BytesReference in project OpenSearch by opensearch-project.
the class MetadataTests method testUnknownFieldClusterMetadata.
public void testUnknownFieldClusterMetadata() throws IOException {
BytesReference metadata = BytesReference.bytes(JsonXContent.contentBuilder().startObject().startObject("meta-data").field("random", "value").endObject().endObject());
try (XContentParser parser = createParser(JsonXContent.jsonXContent, metadata)) {
Metadata.Builder.fromXContent(parser);
fail();
} catch (IllegalArgumentException e) {
assertEquals("Unexpected field [random]", e.getMessage());
}
}
use of org.opensearch.common.bytes.BytesReference in project OpenSearch by opensearch-project.
the class BaseStreamTests method testSecureStringSerialization.
public void testSecureStringSerialization() throws IOException {
try (BytesStreamOutput output = new BytesStreamOutput()) {
final SecureString secureString = new SecureString("super secret".toCharArray());
output.writeSecureString(secureString);
final BytesReference bytesReference = output.bytes();
final StreamInput input = getStreamInput(bytesReference);
;
assertThat(secureString, is(equalTo(input.readSecureString())));
}
try (BytesStreamOutput output = new BytesStreamOutput()) {
final SecureString secureString = randomBoolean() ? null : new SecureString("super secret".toCharArray());
output.writeOptionalSecureString(secureString);
final BytesReference bytesReference = output.bytes();
final StreamInput input = getStreamInput(bytesReference);
;
if (secureString != null) {
assertThat(input.readOptionalSecureString(), is(equalTo(secureString)));
} else {
assertThat(input.readOptionalSecureString(), is(nullValue()));
}
}
}
use of org.opensearch.common.bytes.BytesReference in project OpenSearch by opensearch-project.
the class BaseStreamTests method testBooleanSerialization.
public void testBooleanSerialization() throws IOException {
final BytesStreamOutput output = new BytesStreamOutput();
output.writeBoolean(false);
output.writeBoolean(true);
final BytesReference bytesReference = output.bytes();
final BytesRef bytesRef = bytesReference.toBytesRef();
assertThat(bytesRef.length, equalTo(2));
final byte[] bytes = bytesRef.bytes;
assertThat(bytes[0], equalTo((byte) 0));
assertThat(bytes[1], equalTo((byte) 1));
final StreamInput input = getStreamInput(bytesReference);
assertFalse(input.readBoolean());
assertTrue(input.readBoolean());
final Set<Byte> set = IntStream.range(Byte.MIN_VALUE, Byte.MAX_VALUE).mapToObj(v -> (byte) v).collect(Collectors.toSet());
set.remove((byte) 0);
set.remove((byte) 1);
final byte[] corruptBytes = new byte[] { randomFrom(set) };
final BytesReference corrupt = new BytesArray(corruptBytes);
final IllegalStateException e = expectThrows(IllegalStateException.class, () -> corrupt.streamInput().readBoolean());
final String message = String.format(Locale.ROOT, "unexpected byte [0x%02x]", corruptBytes[0]);
assertThat(e, hasToString(containsString(message)));
}
use of org.opensearch.common.bytes.BytesReference in project OpenSearch by opensearch-project.
the class BaseStreamTests method testOptionalBooleanSerialization.
public void testOptionalBooleanSerialization() throws IOException {
final BytesStreamOutput output = new BytesStreamOutput();
output.writeOptionalBoolean(false);
output.writeOptionalBoolean(true);
output.writeOptionalBoolean(null);
final BytesReference bytesReference = output.bytes();
final BytesRef bytesRef = bytesReference.toBytesRef();
assertThat(bytesRef.length, equalTo(3));
final byte[] bytes = bytesRef.bytes;
assertThat(bytes[0], equalTo((byte) 0));
assertThat(bytes[1], equalTo((byte) 1));
assertThat(bytes[2], equalTo((byte) 2));
final StreamInput input = getStreamInput(bytesReference);
final Boolean maybeFalse = input.readOptionalBoolean();
assertNotNull(maybeFalse);
assertFalse(maybeFalse);
final Boolean maybeTrue = input.readOptionalBoolean();
assertNotNull(maybeTrue);
assertTrue(maybeTrue);
assertNull(input.readOptionalBoolean());
final Set<Byte> set = IntStream.range(Byte.MIN_VALUE, Byte.MAX_VALUE).mapToObj(v -> (byte) v).collect(Collectors.toSet());
set.remove((byte) 0);
set.remove((byte) 1);
set.remove((byte) 2);
final byte[] corruptBytes = new byte[] { randomFrom(set) };
final BytesReference corrupt = new BytesArray(corruptBytes);
final IllegalStateException e = expectThrows(IllegalStateException.class, () -> corrupt.streamInput().readOptionalBoolean());
final String message = String.format(Locale.ROOT, "unexpected byte [0x%02x]", corruptBytes[0]);
assertThat(e, hasToString(containsString(message)));
}
Aggregations