Repository: apache/kafka
File: tools/src/test/java/org/apache/kafka/tools/ConfigCommandTest.java (Java)

Code before:
            "--alter",
            "--add-config", "consumer.heartbeat.interval.ms=6000"));

        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, alterOpts::checkArgs);
        assertEquals("An entity name must be specified with --alter of groups", exception.getMessage());
    }

    @Test
    public void testDescribeGroupConfigOldBroker() {
        ConfigCommand.ConfigCommandOptions describeOpts = new ConfigCommand.ConfigCommandOptions(toArray("--bootstrap-server", "localhost:9092",
            "--entity-type", "groups",
            "--describe"));

        KafkaFutureImpl<Collection<ConfigResource>> future = new KafkaFutureImpl<>();
        ListConfigResourcesResult listConfigResourcesResult = mock(ListConfigResourcesResult.class);
        when(listConfigResourcesResult.all()).thenReturn(future);

        AtomicBoolean listedConfigResources = new AtomicBoolean(false);
        Node node = new Node(1, "localhost", 9092);
        MockAdminClient mockAdminClient = new MockAdminClient(List.of(node), node) {
            @Override
            public ListConfigResourcesResult listConfigResources(Set<ConfigResource.Type> configResourceTypes, ListConfigResourcesOptions options) {
                ConfigResource.Type type = configResourceTypes.iterator().next();
                assertEquals(ConfigResource.Type.GROUP, type);
                future.completeExceptionally(new UnsupportedVersionException("The v0 ListConfigResources only supports CLIENT_METRICS"));
                listedConfigResources.set(true);
                return listConfigResourcesResult;
            }
        };

        ConfigCommand.describeConfig(mockAdminClient, describeOpts);
        assertTrue(listedConfigResources.get());
    }

    public static String[] toArray(String... first) {
        return first;
    }

    @SafeVarargs
    public static String[] toArray(List<String>... lists) {
        return Stream.of(lists).flatMap(List::stream).toArray(String[]::new);
    }

    @SafeVarargs
    public static List<String> concat(List<String>... lists) {
        return Stream.of(lists).flatMap(List::stream).toList();
    }

    @SafeVarargs
    public static <K, V> Map<K, V> concat(Map<K, V>... maps) {
        Map<K, V> res = new HashMap<>();

Review comment (suggestion):
Should we add a test case for other exceptions to ensure they are propagated correctly and not swallowed  

Code after:
            "--entity-type", "groups",
            "--alter",
            "--add-config", "consumer.heartbeat.interval.ms=6000"));

        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, alterOpts::checkArgs);
        assertEquals("An entity name must be specified with --alter of groups", exception.getMessage());
    }

    @Test
    public void testDescribeGroupConfigOldBroker() {
        ConfigCommand.ConfigCommandOptions describeOpts = new ConfigCommand.ConfigCommandOptions(toArray("--bootstrap-server", "localhost:9092",
            "--entity-type", "groups",
            "--describe"));

        KafkaFutureImpl<Collection<ConfigResource>> future = new KafkaFutureImpl<>();
        ListConfigResourcesResult listConfigResourcesResult = mock(ListConfigResourcesResult.class);
        when(listConfigResourcesResult.all()).thenReturn(future);

        AtomicBoolean listedConfigResources = new AtomicBoolean(false);
        Node node = new Node(1, "localhost", 9092);
        MockAdminClient mockAdminClient = new MockAdminClient(List.of(node), node) {
            @Override
            public ListConfigResourcesResult listConfigResources(Set<ConfigResource.Type> configResourceTypes, ListConfigResourcesOptions options) {
                ConfigResource.Type type = configResourceTypes.iterator().next();
                assertEquals(ConfigResource.Type.GROUP, type);
                future.completeExceptionally(new UnsupportedVersionException("The v0 ListConfigResources only supports CLIENT_METRICS"));
                listedConfigResources.set(true);
                return listConfigResourcesResult;
            }
        };

        ConfigCommand.describeConfig(mockAdminClient, describeOpts);
        assertTrue(listedConfigResources.get());
    }

    @Test
    public void testDescribeGroupConfigOldBrokerNotAuthorized() {
        ConfigCommand.ConfigCommandOptions describeOpts = new ConfigCommand.ConfigCommandOptions(toArray("--bootstrap-server", "localhost:9092",
            "--entity-type", "groups",
            "--describe"));

        KafkaFutureImpl<Collection<ConfigResource>> future = new KafkaFutureImpl<>();
        ListConfigResourcesResult listConfigResourcesResult = mock(ListConfigResourcesResult.class);
        when(listConfigResourcesResult.all()).thenReturn(future);

        AtomicBoolean listedConfigResources = new AtomicBoolean(false);
        Node node = new Node(1, "localhost", 9092);
        MockAdminClient mockAdminClient = new MockAdminClient(List.of(node), node) {
            @Override
            public ListConfigResourcesResult listConfigResources(Set<ConfigResource.Type> configResourceTypes, ListConfigResourcesOptions options) {
                ConfigResource.Type type = configResourceTypes.iterator().next();