TEST_F(BrotliDecompressorImplTest, CompressAndDecompress) {
  Buffer::OwnedImpl buffer;
  Buffer::OwnedImpl accumulation_buffer;

  Brotli::Compressor::BrotliCompressorImpl compressor{
      default_quality,
      default_window_bits,
      default_input_block_bits,
      false,
      Brotli::Compressor::BrotliCompressorImpl::EncoderMode::Default,
      4096};

  std::string original_text{};
  for (uint64_t i = 0; i < 20; ++i) {
    TestUtility::feedBufferWithRandomCharacters(buffer, default_input_size * i, i);
    original_text.append(buffer.toString());
    compressor.compress(buffer, Envoy::Compression::Compressor::State::Flush);
    accumulation_buffer.add(buffer);
    drainBuffer(buffer);
  }

  ASSERT_EQ(0, buffer.length());

  compressor.compress(buffer, Envoy::Compression::Compressor::State::Finish);
  ASSERT_GE(10, buffer.length());

  accumulation_buffer.add(buffer);

  drainBuffer(buffer);
  ASSERT_EQ(0, buffer.length());

  std::string json{R"EOF({
  "disable_ring_buffer_reallocation": false,
  "chunk_size": 4096
})EOF"};
  envoy::extensions::compression::brotli::decompressor::v3::Brotli brotli;
  TestUtility::loadFromJson(json, brotli);

  BrotliDecompressorLibraryFactory lib_factory;
  NiceMock<Server::Configuration::MockFactoryContext> context;
  Envoy::Compression::Decompressor::DecompressorFactoryPtr factory =
      lib_factory.createDecompressorFactoryFromProto(brotli, context);
  EXPECT_EQ("brotli.", factory->statsPrefix());
  EXPECT_EQ("br", factory->contentEncoding());

  Envoy::Compression::Decompressor::DecompressorPtr decompressor =
      factory->createDecompressor("test.");
  decompressor->decompress(accumulation_buffer, buffer);
  std::string decompressed_text{buffer.toString()};
  ASSERT_EQ(original_text.length(), decompressed_text.length());
  EXPECT_EQ(original_text, decompressed_text);
}
TEST_F(BrotliDecompressorImplTest, CompressAndDecompress) {
  Buffer::OwnedImpl buffer;
  Buffer::OwnedImpl accumulation_buffer;

  Brotli::Compressor::BrotliCompressorImpl compressor{
      default_quality,
      default_window_bits,
      default_input_block_bits,
      false,
      Brotli::Compressor::BrotliCompressorImpl::EncoderMode::Default,
      4096};

  std::string original_text{};
  for (uint64_t i = 0; i < 20; ++i) {
    TestUtility::feedBufferWithRandomCharacters(buffer, default_input_size * i, i);
    original_text.append(buffer.toString());
    compressor.compress(buffer, Envoy::Compression::Compressor::State::Flush);
    accumulation_buffer.add(buffer);
    drainBuffer(buffer);
  }

  ASSERT_EQ(0, buffer.length());

  compressor.compress(buffer, Envoy::Compression::Compressor::State::Finish);
  ASSERT_GE(10, buffer.length());

  accumulation_buffer.add(buffer);

  drainBuffer(buffer);
  ASSERT_EQ(0, buffer.length());

  std::string json{R"EOF({
  "disable_ring_buffer_reallocation": false,
  "chunk_size": 4096
})EOF"};
  envoy::extensions::compression::brotli::decompressor::v3::Brotli brotli;
  TestUtility::loadFromJson(json, brotli);

  BrotliDecompressorLibraryFactory lib_factory;
  NiceMock<Server::Configuration::MockFactoryContext> context;
  Envoy::Compression::Decompressor::DecompressorFactoryPtr factory =
      lib_factory.createDecompressorFactoryFromProto(brotli, context);
  EXPECT_EQ("brotli.", factory->statsPrefix());
  EXPECT_EQ("br", factory->contentEncoding());

  Envoy::Compression::Decompressor::DecompressorPtr decompressor =
      factory->createDecompressor("test.");
  decompressor->decompress(accumulation_buffer, buffer);
  std::string decompressed_text{buffer.toString()};
  ASSERT_EQ(original_text.length(), decompressed_text.length());
  EXPECT_EQ(original_text, decompressed_text);
}