public void testWriteableReaderReturnsWrongName() throws IOException {
   BytesStreamOutput out = new BytesStreamOutput();
   NamedWriteableRegistry namedWriteableRegistry =
       new NamedWriteableRegistry(
           Collections.singletonList(
               new NamedWriteableRegistry.Entry(
                   BaseNamedWriteable.class,
                   TestNamedWriteable.NAME,
                   (StreamInput in) ->
                       new TestNamedWriteable(in) {
                         @Override
                         public String getWriteableName() {
                           return "intentionally-broken";
                         }
                       })));
   TestNamedWriteable namedWriteableIn =
       new TestNamedWriteable(
           randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10));
   out.writeNamedWriteable(namedWriteableIn);
   byte[] bytes = BytesReference.toBytes(out.bytes());
   StreamInput in =
       new NamedWriteableAwareStreamInput(StreamInput.wrap(bytes), namedWriteableRegistry);
   assertEquals(in.available(), bytes.length);
   AssertionError e =
       expectThrows(AssertionError.class, () -> in.readNamedWriteable(BaseNamedWriteable.class));
   assertThat(
       e.getMessage(),
       endsWith(
           " claims to have a different name [intentionally-broken] than it was read from [test-named-writeable]."));
 }
  public void testNamedWriteableUnknownNamedWriteable() throws IOException {
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry();
    namedWriteableRegistry.register(
        BaseNamedWriteable.class, TestNamedWriteable.NAME, TestNamedWriteable::new);
    BytesStreamOutput out = new BytesStreamOutput();
    out.writeNamedWriteable(
        new NamedWriteable() {
          @Override
          public String getWriteableName() {
            return "unknown";
          }

          @Override
          public void writeTo(StreamOutput out) throws IOException {}
        });
    StreamInput in =
        new NamedWriteableAwareStreamInput(
            StreamInput.wrap(BytesReference.toBytes(out.bytes())), namedWriteableRegistry);
    try {
      // no named writeable registered with given name under test category, can write but cannot
      // read it back
      in.readNamedWriteable(BaseNamedWriteable.class);
      fail("read should have failed");
    } catch (IllegalArgumentException e) {
      assertThat(
          e.getMessage(),
          equalTo("unknown named writeable [" + BaseNamedWriteable.class.getName() + "][unknown]"));
    }
  }
 /** Read from a Stream. */
 public SignificantTermsAggregationBuilder(StreamInput in) throws IOException {
   super(in, TYPE, ValuesSourceType.ANY);
   bucketCountThresholds = new BucketCountThresholds(in);
   executionHint = in.readOptionalString();
   filterBuilder = in.readOptionalNamedWriteable(QueryBuilder.class);
   includeExclude = in.readOptionalWriteable(IncludeExclude::new);
   significanceHeuristic = in.readNamedWriteable(SignificanceHeuristic.class);
 }
 /** Read from a stream. */
 public HasParentQueryBuilder(StreamInput in) throws IOException {
   super(in);
   type = in.readString();
   score = in.readBoolean();
   query = in.readNamedWriteable(QueryBuilder.class);
   innerHit = in.readOptionalWriteable(InnerHitBuilder::new);
   ignoreUnmapped = in.readBoolean();
 }
 public void testNamedWriteableNotSupportedWithoutWrapping() throws IOException {
   BytesStreamOutput out = new BytesStreamOutput();
   TestNamedWriteable testNamedWriteable = new TestNamedWriteable("test1", "test2");
   out.writeNamedWriteable(testNamedWriteable);
   StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes()));
   try {
     in.readNamedWriteable(BaseNamedWriteable.class);
     fail("Expected UnsupportedOperationException");
   } catch (UnsupportedOperationException e) {
     assertThat(e.getMessage(), is("can't read named writeable from StreamInput"));
   }
 }
 // we use the streaming infra to create a copy of the query provided as
 // argument
 private AF copyAggregation(AF agg) throws IOException {
   try (BytesStreamOutput output = new BytesStreamOutput()) {
     output.writeNamedWriteable(agg);
     try (StreamInput in =
         new NamedWriteableAwareStreamInput(
             output.bytes().streamInput(), namedWriteableRegistry)) {
       @SuppressWarnings("unchecked")
       AF secondAgg = (AF) in.readNamedWriteable(PipelineAggregationBuilder.class);
       return secondAgg;
     }
   }
 }
 public void testNamedWriteableUnknownCategory() throws IOException {
   BytesStreamOutput out = new BytesStreamOutput();
   out.writeNamedWriteable(new TestNamedWriteable("test1", "test2"));
   StreamInput in =
       new NamedWriteableAwareStreamInput(out.bytes().streamInput(), new NamedWriteableRegistry());
   // no named writeable registered with given name, can write but cannot read it back
   IllegalArgumentException e =
       expectThrows(
           IllegalArgumentException.class, () -> in.readNamedWriteable(BaseNamedWriteable.class));
   assertThat(
       e.getMessage(),
       equalTo("unknown named writeable category [" + BaseNamedWriteable.class.getName() + "]"));
 }
 /** Test serialization and deserialization of the test AggregatorFactory. */
 public void testSerialization() throws IOException {
   AF testAgg = createTestAggregatorFactory();
   try (BytesStreamOutput output = new BytesStreamOutput()) {
     output.writeNamedWriteable(testAgg);
     try (StreamInput in =
         new NamedWriteableAwareStreamInput(
             output.bytes().streamInput(), namedWriteableRegistry)) {
       PipelineAggregationBuilder deserializedQuery =
           in.readNamedWriteable(PipelineAggregationBuilder.class);
       assertEquals(deserializedQuery, testAgg);
       assertEquals(deserializedQuery.hashCode(), testAgg.hashCode());
       assertNotSame(deserializedQuery, testAgg);
     }
   }
 }
 public void testNamedWriteable() throws IOException {
   BytesStreamOutput out = new BytesStreamOutput();
   NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry();
   namedWriteableRegistry.register(
       BaseNamedWriteable.class, TestNamedWriteable.NAME, TestNamedWriteable::new);
   TestNamedWriteable namedWriteableIn =
       new TestNamedWriteable(
           randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10));
   out.writeNamedWriteable(namedWriteableIn);
   byte[] bytes = BytesReference.toBytes(out.bytes());
   StreamInput in =
       new NamedWriteableAwareStreamInput(StreamInput.wrap(bytes), namedWriteableRegistry);
   assertEquals(in.available(), bytes.length);
   BaseNamedWriteable namedWriteableOut = in.readNamedWriteable(BaseNamedWriteable.class);
   assertEquals(namedWriteableOut, namedWriteableIn);
   assertEquals(in.available(), 0);
 }
 public void testNamedWriteableReaderReturnsNull() throws IOException {
   BytesStreamOutput out = new BytesStreamOutput();
   NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry();
   namedWriteableRegistry.register(
       BaseNamedWriteable.class, TestNamedWriteable.NAME, (StreamInput in) -> null);
   TestNamedWriteable namedWriteableIn =
       new TestNamedWriteable(
           randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10));
   out.writeNamedWriteable(namedWriteableIn);
   byte[] bytes = BytesReference.toBytes(out.bytes());
   StreamInput in =
       new NamedWriteableAwareStreamInput(StreamInput.wrap(bytes), namedWriteableRegistry);
   assertEquals(in.available(), bytes.length);
   IOException e =
       expectThrows(IOException.class, () -> in.readNamedWriteable(BaseNamedWriteable.class));
   assertThat(
       e.getMessage(),
       endsWith(
           "] returned null which is not allowed and probably means it screwed up the stream."));
 }
 /** Read from a stream. */
 public FieldMaskingSpanQueryBuilder(StreamInput in) throws IOException {
   super(in);
   queryBuilder = (SpanQueryBuilder) in.readNamedWriteable(QueryBuilder.class);
   fieldName = in.readString();
 }
 /** Read from a stream. */
 public SpanWithinQueryBuilder(StreamInput in) throws IOException {
   super(in);
   big = (SpanQueryBuilder) in.readNamedWriteable(QueryBuilder.class);
   little = (SpanQueryBuilder) in.readNamedWriteable(QueryBuilder.class);
 }