### Question:
GossipHandler implements Function<MessageApi, CompletableFuture<ValidationResult>> { @Override public SafeFuture<ValidationResult> apply(final MessageApi message) { final int messageSize = message.getData().readableBytes(); if (messageSize > GOSSIP_MAX_SIZE) { LOG.trace( "Rejecting gossip message of length {} which exceeds maximum size of {}", messageSize, GOSSIP_MAX_SIZE); return VALIDATION_FAILED; } byte[] arr = new byte[message.getData().readableBytes()]; message.getData().slice().readBytes(arr); Bytes bytes = Bytes.wrap(arr); if (!processedMessages.add(bytes)) { LOG.trace("Ignoring duplicate message for topic {}: {} bytes", topic, bytes.size()); return VALIDATION_IGNORED; } LOG.trace("Received message for topic {}: {} bytes", topic, bytes.size()); return handler.handleMessage(bytes); } GossipHandler(
      final Topic topic, final PubsubPublisherApi publisher, final TopicHandler handler); @Override SafeFuture<ValidationResult> apply(final MessageApi message); void gossip(Bytes bytes);  }### Answer:
@Test public void apply_valid() { final Bytes data = Bytes.fromHexString("0x01"); final MockMessageApi message = new MockMessageApi(data, topic); final SafeFuture<ValidationResult> result = gossipHandler.apply(message); assertThat(result).isCompletedWithValue(ValidationResult.Valid); }
@Test public void apply_invalid() { final Bytes data = Bytes.fromHexString("0x01"); final MockMessageApi message = new MockMessageApi(data, topic); when(topicHandler.handleMessage(any())) .thenReturn(SafeFuture.completedFuture(ValidationResult.Invalid)); final SafeFuture<ValidationResult> result = gossipHandler.apply(message); assertThat(result).isCompletedWithValue(ValidationResult.Invalid); }
@Test public void apply_exceedsMaxSize() { final Bytes data = Bytes.wrap(new byte[GOSSIP_MAX_SIZE + 1]); final MockMessageApi message = new MockMessageApi(data, topic); final SafeFuture<ValidationResult> result = gossipHandler.apply(message); assertThat(result).isCompletedWithValue(ValidationResult.Invalid); verify(topicHandler, never()).handleMessage(any()); }
@Test public void apply_bufferCapacityExceedsMaxSize() { ByteBuf data = Unpooled.buffer(GOSSIP_MAX_SIZE + 1).writeBytes(new byte[GOSSIP_MAX_SIZE]); final MockMessageApi message = new MockMessageApi(data, topic); final SafeFuture<ValidationResult> result = gossipHandler.apply(message); assertThat(result).isCompletedWithValue(ValidationResult.Valid); }
@Test @SuppressWarnings("FutureReturnValueIgnored") public void apply_duplicate() { final Bytes data = Bytes.fromHexString("0x01"); final MockMessageApi message = new MockMessageApi(data, topic); gossipHandler.apply(message); final SafeFuture<ValidationResult> result = gossipHandler.apply(message); assertThat(result).isCompletedWithValue(ValidationResult.Ignore); verify(topicHandler).handleMessage(any()); }