Repository: apache/kafka
File: clients/src/test/java/org/apache/kafka/common/telemetry/internals/ClientTelemetryUtilsTest.java (Java)

Code before:
        assertThrows(IllegalArgumentException.class, () -> ClientTelemetryUtils.validateClientInstanceId(Uuid.ZERO_UUID));

        Uuid uuid = Uuid.randomUuid();
        assertEquals(uuid, ClientTelemetryUtils.validateClientInstanceId(uuid));
    }

    @ParameterizedTest
    @ValueSource(ints = {300_000, Integer.MAX_VALUE - 1, Integer.MAX_VALUE})
    public void testValidateIntervalMsValid(int pushIntervalMs) {
        assertEquals(pushIntervalMs, ClientTelemetryUtils.validateIntervalMs(pushIntervalMs));
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 0})
    public void testValidateIntervalMsInvalid(int pushIntervalMs) {
        assertEquals(ClientTelemetryReporter.DEFAULT_PUSH_INTERVAL_MS, ClientTelemetryUtils.validateIntervalMs(pushIntervalMs));
    }

    @Test
    public void testPreferredCompressionType() {
        // Test with no unsupported types
        assertEquals(CompressionType.NONE, ClientTelemetryUtils.preferredCompressionType(Collections.emptyList(), Collections.emptySet()));
        assertEquals(CompressionType.NONE, ClientTelemetryUtils.preferredCompressionType(Arrays.asList(CompressionType.NONE, CompressionType.GZIP), Collections.emptySet()));
        assertEquals(CompressionType.GZIP, ClientTelemetryUtils.preferredCompressionType(Arrays.asList(CompressionType.GZIP, CompressionType.NONE), Collections.emptySet()));

        // Test with unsupported types filtering
        assertEquals(CompressionType.LZ4, ClientTelemetryUtils.preferredCompressionType(Arrays.asList(CompressionType.GZIP, CompressionType.LZ4), Set.of(CompressionType.GZIP)));
        assertEquals(CompressionType.NONE, ClientTelemetryUtils.preferredCompressionType(Arrays.asList(CompressionType.GZIP, CompressionType.NONE), Set.of(CompressionType.GZIP)));
        assertEquals(CompressionType.SNAPPY, ClientTelemetryUtils.preferredCompressionType(Arrays.asList(CompressionType.GZIP, CompressionType.LZ4, CompressionType.SNAPPY), Set.of(CompressionType.GZIP, CompressionType.LZ4)));

        // Test when all types are unsupported
        assertEquals(CompressionType.NONE, ClientTelemetryUtils.preferredCompressionType(Arrays.asList(CompressionType.GZIP), Set.of(CompressionType.GZIP)));
        assertEquals(CompressionType.NONE, ClientTelemetryUtils.preferredCompressionType(Arrays.asList(CompressionType.GZIP, CompressionType.LZ4), Set.of(CompressionType.GZIP, CompressionType.LZ4)));

        // Test priority order with unsupported types (first available wins)
        assertEquals(CompressionType.ZSTD, ClientTelemetryUtils.preferredCompressionType(Arrays.asList(CompressionType.ZSTD, CompressionType.SNAPPY), Set.of()));
        assertEquals(CompressionType.SNAPPY, ClientTelemetryUtils.preferredCompressionType(Arrays.asList(CompressionType.ZSTD, CompressionType.SNAPPY), Set.of(CompressionType.ZSTD)));
        assertEquals(CompressionType.GZIP, ClientTelemetryUtils.preferredCompressionType(Arrays.asList(CompressionType.LZ4, CompressionType.GZIP, CompressionType.SNAPPY), Set.of(CompressionType.LZ4)));

        // Test NullPointerException for null acceptedCompressionTypes parameter
        assertThrows(NullPointerException.class, () ->
            ClientTelemetryUtils.preferredCompressionType(null, Collections.emptySet()));
        assertThrows(NullPointerException.class, () -> 
            ClientTelemetryUtils.preferredCompressionType(null, Set.of(CompressionType.GZIP)));

        // Test NullPointerException for null unsupportedCompressionTypes parameter
        assertThrows(NullPointerException.class, () ->
            ClientTelemetryUtils.preferredCompressionType(Arrays.asList(CompressionType.GZIP, CompressionType.NONE), null));
        assertThrows(NullPointerException.class, () -> 
            ClientTelemetryUtils.preferredCompressionType(Arrays.asList(CompressionType.LZ4, CompressionType.SNAPPY), null));
    }

Review comment (suggestion):
Can you add a test when the list has no common match i.e. `ClientTelemetryUtils.preferredCompressionType(List.of(CompressionType.GZIP, CompressionType.LZ4), Set.of(CompressionType.SNAPPY)`

Code after:
        Uuid uuid = Uuid.randomUuid();
        assertEquals(uuid, ClientTelemetryUtils.validateClientInstanceId(uuid));
    }

    @ParameterizedTest
    @ValueSource(ints = {300_000, Integer.MAX_VALUE - 1, Integer.MAX_VALUE})
    public void testValidateIntervalMsValid(int pushIntervalMs) {
        assertEquals(pushIntervalMs, ClientTelemetryUtils.validateIntervalMs(pushIntervalMs));
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 0})
    public void testValidateIntervalMsInvalid(int pushIntervalMs) {
        assertEquals(ClientTelemetryReporter.DEFAULT_PUSH_INTERVAL_MS, ClientTelemetryUtils.validateIntervalMs(pushIntervalMs));
    }

    @Test
    public void testPreferredCompressionType() {
        // Test with no unsupported types
        assertEquals(CompressionType.NONE, ClientTelemetryUtils.preferredCompressionType(List.of(), Set.of()));
        assertEquals(CompressionType.NONE, ClientTelemetryUtils.preferredCompressionType(List.of(CompressionType.NONE, CompressionType.GZIP), Set.of()));
        assertEquals(CompressionType.GZIP, ClientTelemetryUtils.preferredCompressionType(List.of(CompressionType.GZIP, CompressionType.NONE), Set.of()));

        // Test unsupported type filtering (returns first available type, or NONE if all are unsupported)
        assertEquals(CompressionType.LZ4, ClientTelemetryUtils.preferredCompressionType(List.of(CompressionType.GZIP, CompressionType.LZ4), Set.of(CompressionType.GZIP)));
        assertEquals(CompressionType.SNAPPY, ClientTelemetryUtils.preferredCompressionType(List.of(CompressionType.GZIP, CompressionType.LZ4, CompressionType.SNAPPY), Set.of(CompressionType.GZIP, CompressionType.LZ4)));
        assertEquals(CompressionType.NONE, ClientTelemetryUtils.preferredCompressionType(List.of(CompressionType.GZIP, CompressionType.LZ4), Set.of(CompressionType.GZIP, CompressionType.LZ4)));
        
        // Test edge case: no match between requested and supported types
        assertEquals(CompressionType.GZIP, ClientTelemetryUtils.preferredCompressionType(List.of(CompressionType.GZIP, CompressionType.LZ4), Set.of(CompressionType.SNAPPY)));

        // Test NullPointerException for null parameters
        assertThrows(NullPointerException.class, () ->
            ClientTelemetryUtils.preferredCompressionType(null, Set.of()));
        assertThrows(NullPointerException.class, () ->
            ClientTelemetryUtils.preferredCompressionType(List.of(CompressionType.GZIP, CompressionType.NONE), null));
    }

    @ParameterizedTest
    @EnumSource(CompressionType.class)
    public void testCompressDecompress(CompressionType compressionType) throws IOException {
        MetricsData metricsData = getMetricsData();
        byte[] raw = metricsData.toByteArray();
        ByteBuffer compressed = ClientTelemetryUtils.compress(metricsData, compressionType);
        assertNotNull(compressed);
        if (compressionType != CompressionType.NONE) {
            assertTrue(compressed.limit() < raw.length);
        } else {
            assertArrayEquals(raw, Utils.toArray(compressed));
        }
        ByteBuffer decompressed = ClientTelemetryUtils.decompress(compressed, compressionType);