Repository: provectus/kafka-ui
Issue: Consumers sorting is broken
Labels: type/bug
scope/backend
status/accepted
status/confirmed

<!--

Don't forget to check for existing issues/discussions regarding your proposal. We might already have it.
https://github.com/provectus/kafka-ui/issues
https://github.com/provectus/kafka-ui/discussions

-->

**Describe the bug**
<!--(A clear and concise description of what the bug is.)-->
/api/clusters/local/consumer-groups/paged?page=1&perPage=25&search=&orderBy=MEMBERS&sortOrder=DESC
returns payload in ASC order

**Set up**
<!--
How do you run the app? Please provide as much info as possible:
1. App version (docker image version or check commit hash in the top left corner in UI)
2. Helm chart version, if you use one
3. Any IAAC configs

We might close the issue without further explanation if you don't provide such information.
-->


**Steps to Reproduce**
<!-- We'd like you to provide an example setup (via docker-compose, helm, etc.) 
to reproduce the problem, especially with a complex setups. -->
Steps to reproduce the behavior:

1. 

**Expected behavior**
<!--
(A clear and concise description of what you expected to happen)
-->

**Screenshots**
<!--
(If applicable, add screenshots to help explain your problem)
-->


**Additional context**
<!--
(Add any other context about the problem here)
-->


Issue comments:

Pull request review comments:
This empty line could be removed :).
Instead of hardcoding `CONNECT_REST_PORT`, we can use `CONNECT_PORT` defined already in the class.
I like the spacing like separating logical blocks
sure

Patch that resolved the issue:
diff --git a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/ConsumerGroupService.java b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/ConsumerGroupService.java
index beb9f849798..d17f5249781 100644
--- a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/ConsumerGroupService.java
+++ b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/ConsumerGroupService.java
@@ -161,7 +161,7 @@ private Comparator<ConsumerGroupDescription> getPaginationComparator(ConsumerGro
         };
         return Comparator.comparingInt(statesPriorities);
       case MEMBERS:
-        return Comparator.comparingInt(cg -> -cg.members().size());
+        return Comparator.comparingInt(cg -> cg.members().size());
       default:
         throw new IllegalStateException("Unsupported order by: " + orderBy);
     }


Test patch:
diff --git a/kafka-ui-api/src/test/java/com/provectus/kafka/ui/KafkaConsumerGroupTests.java b/kafka-ui-api/src/test/java/com/provectus/kafka/ui/KafkaConsumerGroupTests.java
index 17e7a19ee7f..98f8394060f 100644
--- a/kafka-ui-api/src/test/java/com/provectus/kafka/ui/KafkaConsumerGroupTests.java
+++ b/kafka-ui-api/src/test/java/com/provectus/kafka/ui/KafkaConsumerGroupTests.java
@@ -14,6 +14,7 @@
 import java.util.stream.Stream;
 import lombok.extern.slf4j.Slf4j;
 import lombok.val;
+import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.kafka.clients.admin.NewTopic;
 import org.apache.kafka.clients.consumer.ConsumerConfig;
 import org.apache.kafka.clients.consumer.KafkaConsumer;
@@ -126,6 +127,21 @@ void shouldReturnConsumerGroupsWithPagination() throws Exception {
               assertThat(page.getConsumerGroups())
                   .isSortedAccordingTo(Comparator.comparing(ConsumerGroupDTO::getGroupId).reversed());
             });
+
+      webTestClient
+          .get()
+          .uri("/api/clusters/{clusterName}/consumer-groups/paged?perPage=10&&search"
+              + "=cgPageTest&orderBy=MEMBERS&sortOrder=DESC", LOCAL)
+          .exchange()
+          .expectStatus()
+          .isOk()
+          .expectBody(ConsumerGroupsPageResponseDTO.class)
+          .value(page -> {
+            assertThat(page.getPageCount()).isEqualTo(1);
+            assertThat(page.getConsumerGroups().size()).isEqualTo(5);
+            assertThat(page.getConsumerGroups())
+                .isSortedAccordingTo(Comparator.comparing(ConsumerGroupDTO::getMembers).reversed());
+          });
     }
   }
 
@@ -133,7 +149,7 @@ private Closeable startConsumerGroups(int count, String consumerGroupPrefix) {
     String topicName = createTopicWithRandomName();
     var consumers =
         Stream.generate(() -> {
-          String groupId = consumerGroupPrefix + UUID.randomUUID();
+          String groupId = consumerGroupPrefix + RandomStringUtils.randomAlphabetic(5);
           val consumer = createTestConsumerWithGroupId(groupId);
           consumer.subscribe(List.of(topicName));
           consumer.poll(Duration.ofMillis(100));
diff --git a/kafka-ui-api/src/test/java/com/provectus/kafka/ui/container/KafkaConnectContainer.java b/kafka-ui-api/src/test/java/com/provectus/kafka/ui/container/KafkaConnectContainer.java
index dd8d5d03c41..b0098b307f5 100644
--- a/kafka-ui-api/src/test/java/com/provectus/kafka/ui/container/KafkaConnectContainer.java
+++ b/kafka-ui-api/src/test/java/com/provectus/kafka/ui/container/KafkaConnectContainer.java
@@ -12,6 +12,7 @@ public class KafkaConnectContainer extends GenericContainer<KafkaConnectContaine
   public KafkaConnectContainer(String version) {
     super("confluentinc/cp-kafka-connect:" + version);
     addExposedPort(CONNECT_PORT);
+
     waitStrategy = Wait.forHttp("/")
         .withStartupTimeout(Duration.ofMinutes(5));
   }
@@ -37,6 +38,7 @@ public KafkaConnectContainer withKafka(Network network, String bootstrapServers)
     withEnv("CONNECT_INTERNAL_KEY_CONVERTER", "org.apache.kafka.connect.json.JsonConverter");
     withEnv("CONNECT_INTERNAL_VALUE_CONVERTER", "org.apache.kafka.connect.json.JsonConverter");
     withEnv("CONNECT_REST_ADVERTISED_HOST_NAME", "kafka-connect");
+    withEnv("CONNECT_REST_PORT", String.valueOf(CONNECT_PORT));
     withEnv("CONNECT_PLUGIN_PATH", "/usr/share/java,/usr/share/confluent-hub-components");
     return self();
   }