rreddy-22 commented on code in PR #15382:
URL: https://github.com/apache/kafka/pull/15382#discussion_r1492844519


##########
tools/src/test/java/org/apache/kafka/tools/consumer/group/ListConsumerGroupTest.java:
##########
@@ -123,42 +128,89 @@ public void testListGroupCommand(String quorum) throws 
Exception {
         String simpleGroup = "simple-group";
         addSimpleGroupExecutor(simpleGroup);
         addConsumerGroupExecutor(1);
-        final AtomicReference<String> out = new AtomicReference<>("");
 
-        String[] cgcArgs1 = new String[]{"--bootstrap-server", 
bootstrapServers(listenerName()), "--list"};
-        TestUtils.waitForCondition(() -> {
-            out.set(kafka.utils.TestUtils.grabConsoleOutput(() -> {
-                ConsumerGroupCommand.main(cgcArgs1);
-                return null;
-            }));
-            return !out.get().contains("STATE") && 
out.get().contains(simpleGroup) && out.get().contains(GROUP);
-        }, "Expected to find " + simpleGroup + ", " + GROUP + " and no header, 
but found " + out.get());
-
-        String[] cgcArgs2 = new String[]{"--bootstrap-server", 
bootstrapServers(listenerName()), "--list", "--state"};
-        TestUtils.waitForCondition(() -> {
-            out.set(kafka.utils.TestUtils.grabConsoleOutput(() -> {
-                ConsumerGroupCommand.main(cgcArgs2);
-                return null;
-            }));
-            return out.get().contains("STATE") && 
out.get().contains(simpleGroup) && out.get().contains(GROUP);
-        }, "Expected to find " + simpleGroup + ", " + GROUP + " and the 
header, but found " + out.get());
-
-        String[] cgcArgs3 = new String[]{"--bootstrap-server", 
bootstrapServers(listenerName()), "--list", "--state", "Stable"};
-        TestUtils.waitForCondition(() -> {
-            out.set(kafka.utils.TestUtils.grabConsoleOutput(() -> {
-                ConsumerGroupCommand.main(cgcArgs3);
-                return null;
-            }));
-            return out.get().contains("STATE") && out.get().contains(GROUP) && 
out.get().contains("Stable");
-        }, "Expected to find " + GROUP + " in state Stable and the header, but 
found " + out.get());
-
-        String[] cgcArgs4 = new String[]{"--bootstrap-server", 
bootstrapServers(listenerName()), "--list", "--state", "stable"};
+        validateListOutput(
+            Arrays.asList("--bootstrap-server", 
bootstrapServers(listenerName()), "--list"),
+            Collections.emptyList(),
+            mkSet(
+                Collections.singletonList(GROUP),
+                Collections.singletonList(simpleGroup)
+            )
+        );
+
+        validateListOutput(
+            Arrays.asList("--bootstrap-server", 
bootstrapServers(listenerName()), "--list", "--state"),
+            Arrays.asList("GROUP", "STATE"),
+            mkSet(
+                Arrays.asList(GROUP, "Stable"),
+                Arrays.asList(simpleGroup, "Empty")
+            )
+        );
+
+        validateListOutput(
+            Arrays.asList("--bootstrap-server", 
bootstrapServers(listenerName()), "--list", "--state", "Stable"),
+            Arrays.asList("GROUP", "STATE"),
+            mkSet(
+                Arrays.asList(GROUP, "Stable")
+            )
+        );
+
+        validateListOutput(
+            Arrays.asList("--bootstrap-server", 
bootstrapServers(listenerName()), "--list", "--state", "stable"),
+            Arrays.asList("GROUP", "STATE"),
+            mkSet(
+                Arrays.asList(GROUP, "Stable")
+            )
+        );
+    }
+
+    /**
+     * Validates that the output of the list command corresponds to the 
expected values.
+     *
+     * @param args              The arguments for the command line tool.
+     * @param expectedHeader    The expected header as a list of strings; or 
an empty list
+     *                          if a header is not expected.
+     * @param expectedGroups    The expected groups as a set of list of 
strings. The list
+     *                          of strings corresponds to the columns.
+     * @throws InterruptedException
+     */
+    private static void validateListOutput(
+        List<String> args,
+        List<String> expectedHeader,
+        Set<List<String>> expectedGroups
+    ) throws InterruptedException {
+        final AtomicReference<String> out = new AtomicReference<>("");
         TestUtils.waitForCondition(() -> {
-            out.set(kafka.utils.TestUtils.grabConsoleOutput(() -> {
-                ConsumerGroupCommand.main(cgcArgs4);
-                return null;
-            }));
-            return out.get().contains("STATE") && out.get().contains(GROUP) && 
out.get().contains("Stable");
-        }, "Expected to find " + GROUP + " in state Stable and the header, but 
found " + out.get());
+            String output = runAndGrabConsoleOutput(args);
+            out.set(output);
+
+            int index = 0;
+            String[] lines = output.split("\n");
+
+            // Parse the header if one is expected.
+            if (expectedHeader.size() > 0) {

Review Comment:
   can we use if(!expectedHeader.isEmpty()) 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to