nizhikov commented on code in PR #15248:
URL: https://github.com/apache/kafka/pull/15248#discussion_r1465339215


##########
tools/src/test/java/org/apache/kafka/tools/consumer/group/ConsumerGroupServiceTest.java:
##########
@@ -0,0 +1,299 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.tools.consumer.group;
+
+import kafka.admin.ConsumerGroupCommand;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientTestUtils;
+import org.apache.kafka.clients.admin.ConsumerGroupDescription;
+import org.apache.kafka.clients.admin.DescribeConsumerGroupsResult;
+import org.apache.kafka.clients.admin.DescribeTopicsResult;
+import org.apache.kafka.clients.admin.ListConsumerGroupOffsetsResult;
+import org.apache.kafka.clients.admin.ListConsumerGroupOffsetsSpec;
+import org.apache.kafka.clients.admin.ListOffsetsResult;
+import org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo;
+import org.apache.kafka.clients.admin.MemberAssignment;
+import org.apache.kafka.clients.admin.MemberDescription;
+import org.apache.kafka.clients.admin.OffsetSpec;
+import org.apache.kafka.clients.admin.TopicDescription;
+import org.apache.kafka.clients.consumer.OffsetAndMetadata;
+import org.apache.kafka.clients.consumer.RangeAssignor;
+import org.apache.kafka.common.ConsumerGroupState;
+import org.apache.kafka.common.KafkaFuture;
+import org.apache.kafka.common.Node;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.TopicPartitionInfo;
+import org.apache.kafka.common.internals.KafkaFutureImpl;
+import org.apache.kafka.common.utils.Utils;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentMatcher;
+import org.mockito.ArgumentMatchers;
+import scala.Option;
+import scala.Some;
+import scala.Tuple2;
+import scala.collection.JavaConverters;
+import scala.collection.Seq;
+import scala.collection.immutable.Map$;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class ConsumerGroupServiceTest {
+    public static final String GROUP = "testGroup";
+
+    public static final int NUM_PARTITIONS = 10;
+
+    private static final List<String> TOPICS = IntStream.range(0, 
5).mapToObj(i -> "testTopic" + i).collect(Collectors.toList());
+
+    private static final List<TopicPartition> TOPIC_PARTITIONS = 
TOPICS.stream()
+            .flatMap(topic -> IntStream.range(0, NUM_PARTITIONS).mapToObj(i -> 
new TopicPartition(topic, i)))
+            .collect(Collectors.toList());
+
+    private final Admin admin = mock(Admin.class);
+
+    @Test
+    public void testAdminRequestsForDescribeOffsets() {
+        String[] args = new String[]{"--bootstrap-server", "localhost:9092", 
"--group", GROUP, "--describe", "--offsets"};
+        ConsumerGroupCommand.ConsumerGroupService groupService = 
consumerGroupService(args);
+
+        
when(admin.describeConsumerGroups(ArgumentMatchers.eq(Collections.singletonList(GROUP)),
 any()))
+                .thenReturn(describeGroupsResult(ConsumerGroupState.STABLE));
+        
when(admin.listConsumerGroupOffsets(ArgumentMatchers.eq(listConsumerGroupOffsetsSpec()),
 any()))
+                .thenReturn(listGroupOffsetsResult(GROUP));
+        when(admin.listOffsets(offsetsArgMatcher(), any()))
+                .thenReturn(listOffsetsResult());
+
+        Tuple2<Option<String>, 
Option<Seq<ConsumerGroupCommand.PartitionAssignmentState>>> res = 
groupService.collectGroupOffsets(GROUP);
+        assertEquals(Some.apply("Stable"), res._1);
+        assertTrue(res._2.isDefined());
+        assertEquals(TOPIC_PARTITIONS.size(), res._2.get().size());
+
+        verify(admin, 
times(1)).describeConsumerGroups(ArgumentMatchers.eq(Collections.singletonList(GROUP)),
 any());
+        verify(admin, 
times(1)).listConsumerGroupOffsets(ArgumentMatchers.eq(listConsumerGroupOffsetsSpec()),
 any());
+        verify(admin, times(1)).listOffsets(offsetsArgMatcher(), any());
+    }
+
+    @Test
+    public void testAdminRequestsForDescribeNegativeOffsets() {
+        String[] args = new String[]{"--bootstrap-server", "localhost:9092", 
"--group", GROUP, "--describe", "--offsets"};
+        ConsumerGroupCommand.ConsumerGroupService groupService = 
consumerGroupService(args);
+
+        TopicPartition testTopicPartition0 = new TopicPartition("testTopic1", 
0);
+        TopicPartition testTopicPartition1 = new TopicPartition("testTopic1", 
1);
+        TopicPartition testTopicPartition2 = new TopicPartition("testTopic1", 
2);
+        TopicPartition testTopicPartition3 = new TopicPartition("testTopic2", 
0);
+        TopicPartition testTopicPartition4 = new TopicPartition("testTopic2", 
1);
+        TopicPartition testTopicPartition5 = new TopicPartition("testTopic2", 
2);
+
+        // Some topic's partitions gets valid OffsetAndMetadata values, other 
gets nulls values (negative integers) and others aren't defined

Review Comment:
   Actually, I just copied it from the scala version of test.
   Removed mention about negative values.



-- 
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