From 67e8b975597e4795a197b2a2b9e47be9015ba4aa Mon Sep 17 00:00:00 2001 From: Ivan Malutin <137520241+ivamly@users.noreply.github.com> Date: Wed, 31 Jul 2024 20:31:53 +0300 Subject: [PATCH] Enhance unit tests for CodecService methods (#111382) --- .../elasticsearch/index/codec/CodecTests.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/server/src/test/java/org/elasticsearch/index/codec/CodecTests.java b/server/src/test/java/org/elasticsearch/index/codec/CodecTests.java index 2c2af49e7d062..002e42b3198e6 100644 --- a/server/src/test/java/org/elasticsearch/index/codec/CodecTests.java +++ b/server/src/test/java/org/elasticsearch/index/codec/CodecTests.java @@ -38,7 +38,9 @@ import org.hamcrest.Matchers; import java.io.IOException; +import java.util.Arrays; import java.util.Collections; +import java.util.List; import static org.hamcrest.Matchers.instanceOf; @@ -96,6 +98,29 @@ public void testLegacyBestCompression() throws Exception { } } + public void testCodecRetrievalForUnknownCodec() throws Exception { + CodecService codecService = createCodecService(); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> codecService.codec("unknown_codec")); + assertEquals("failed to find codec [unknown_codec]", exception.getMessage()); + } + + public void testAvailableCodecsContainsExpectedCodecs() throws Exception { + CodecService codecService = createCodecService(); + String[] availableCodecs = codecService.availableCodecs(); + List codecList = Arrays.asList(availableCodecs); + int expectedCodecCount = Codec.availableCodecs().size() + 5; + + assertTrue(codecList.contains(CodecService.DEFAULT_CODEC)); + assertTrue(codecList.contains(CodecService.LEGACY_DEFAULT_CODEC)); + assertTrue(codecList.contains(CodecService.BEST_COMPRESSION_CODEC)); + assertTrue(codecList.contains(CodecService.LEGACY_BEST_COMPRESSION_CODEC)); + assertTrue(codecList.contains(CodecService.LUCENE_DEFAULT_CODEC)); + + assertFalse(codecList.contains("unknown_codec")); + + assertEquals(expectedCodecCount, availableCodecs.length); + } + private CodecService createCodecService() throws IOException { Settings nodeSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build(); IndexSettings settings = IndexSettingsModule.newIndexSettings("_na", nodeSettings);