Search in sources :

Example 96 with BytesReference

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));
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 97 with BytesReference

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());
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 98 with BytesReference

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()));
        }
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) SecureString(org.opensearch.common.settings.SecureString)

Example 99 with BytesReference

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)));
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) IntStream(java.util.stream.IntStream) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Arrays(java.util.Arrays) BytesReference(org.opensearch.common.bytes.BytesReference) CheckedFunction(org.opensearch.common.CheckedFunction) CheckedBiConsumer(org.opensearch.common.CheckedBiConsumer) CheckedConsumer(org.opensearch.common.CheckedConsumer) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) SecureString(org.opensearch.common.settings.SecureString) Locale(java.util.Locale) Map(java.util.Map) Matchers.iterableWithSize(org.hamcrest.Matchers.iterableWithSize) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LinkedHashSet(java.util.LinkedHashSet) BytesRef(org.apache.lucene.util.BytesRef) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) Instant(java.time.Instant) EOFException(java.io.EOFException) Collectors(java.util.stream.Collectors) Tuple(org.opensearch.common.collect.Tuple) Objects(java.util.Objects) List(java.util.List) BytesArray(org.opensearch.common.bytes.BytesArray) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) BytesArray(org.opensearch.common.bytes.BytesArray) Matchers.hasToString(org.hamcrest.Matchers.hasToString) SecureString(org.opensearch.common.settings.SecureString) Matchers.containsString(org.hamcrest.Matchers.containsString) BytesRef(org.apache.lucene.util.BytesRef)

Example 100 with BytesReference

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)));
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) IntStream(java.util.stream.IntStream) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Arrays(java.util.Arrays) BytesReference(org.opensearch.common.bytes.BytesReference) CheckedFunction(org.opensearch.common.CheckedFunction) CheckedBiConsumer(org.opensearch.common.CheckedBiConsumer) CheckedConsumer(org.opensearch.common.CheckedConsumer) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) SecureString(org.opensearch.common.settings.SecureString) Locale(java.util.Locale) Map(java.util.Map) Matchers.iterableWithSize(org.hamcrest.Matchers.iterableWithSize) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LinkedHashSet(java.util.LinkedHashSet) BytesRef(org.apache.lucene.util.BytesRef) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) Instant(java.time.Instant) EOFException(java.io.EOFException) Collectors(java.util.stream.Collectors) Tuple(org.opensearch.common.collect.Tuple) Objects(java.util.Objects) List(java.util.List) BytesArray(org.opensearch.common.bytes.BytesArray) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) BytesArray(org.opensearch.common.bytes.BytesArray) Matchers.hasToString(org.hamcrest.Matchers.hasToString) SecureString(org.opensearch.common.settings.SecureString) Matchers.containsString(org.hamcrest.Matchers.containsString) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

BytesReference (org.opensearch.common.bytes.BytesReference)373 XContentType (org.opensearch.common.xcontent.XContentType)117 XContentParser (org.opensearch.common.xcontent.XContentParser)113 IOException (java.io.IOException)86 BytesArray (org.opensearch.common.bytes.BytesArray)82 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)56 ArrayList (java.util.ArrayList)50 HashMap (java.util.HashMap)47 Map (java.util.Map)46 List (java.util.List)45 Matchers.containsString (org.hamcrest.Matchers.containsString)43 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)40 Version (org.opensearch.Version)36 ReleasableBytesReference (org.opensearch.common.bytes.ReleasableBytesReference)30 ToXContent (org.opensearch.common.xcontent.ToXContent)29 Collections (java.util.Collections)27 ShardId (org.opensearch.index.shard.ShardId)27 Settings (org.opensearch.common.settings.Settings)26 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)26 OpenSearchException (org.opensearch.OpenSearchException)24