dajac commented on code in PR #13637:
URL: https://github.com/apache/kafka/pull/13637#discussion_r1179075994


##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/consumer/TargetAssignmentBuilderTest.java:
##########
@@ -0,0 +1,679 @@
+/*
+ * 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.coordinator.group.consumer;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.coordinator.group.assignor.AssignmentMemberSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentTopicMetadata;
+import org.apache.kafka.coordinator.group.assignor.GroupAssignment;
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignor;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+import static 
org.apache.kafka.coordinator.group.consumer.AssignmentTestUtil.mkAssignment;
+import static 
org.apache.kafka.coordinator.group.consumer.AssignmentTestUtil.mkTopicAssignment;
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentEpochRecord;
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentRecord;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+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 TargetAssignmentBuilderTest {
+
+    public static class TargetAssignmentBuilderTestContext {
+        private final String groupId;
+        private final int groupEpoch;
+        private final PartitionAssignor assignor = 
mock(PartitionAssignor.class);
+        private final Map<String, ConsumerGroupMember> members = new 
HashMap<>();
+        private final Map<String, TopicMetadata> subscriptionMetadata = new 
HashMap<>();
+        private final Map<String, ConsumerGroupMember> updatedMembers = new 
HashMap<>();
+        private final Map<String, Assignment> targetAssignments = new 
HashMap<>();
+        private final Map<String, 
org.apache.kafka.coordinator.group.assignor.MemberAssignment> assignments = new 
HashMap<>();
+
+        public TargetAssignmentBuilderTestContext(
+            String groupId,
+            int groupEpoch
+        ) {
+            this.groupId = groupId;
+            this.groupEpoch = groupEpoch;
+        }
+
+        public TargetAssignmentBuilderTestContext addGroupMember(
+            String memberId,
+            List<String> subscriptions,
+            Map<Uuid, Set<Integer>> targetPartitions
+        ) {
+            members.put(memberId, new ConsumerGroupMember.Builder(memberId)
+                .setSubscribedTopicNames(subscriptions)
+                .setRebalanceTimeoutMs(5000)
+                .build());
+
+            targetAssignments.put(memberId, new Assignment(
+                (byte) 0,
+                targetPartitions,
+                VersionedMetadata.EMPTY
+            ));
+
+            return this;
+        }
+
+        public TargetAssignmentBuilderTestContext addTopicMetadata(
+            Uuid topicId,
+            String topicName,
+            int numPartitions
+        ) {
+            subscriptionMetadata.put(topicName, new TopicMetadata(
+                topicId,
+                topicName,
+                numPartitions
+            ));
+            return this;
+        }
+
+        public TargetAssignmentBuilderTestContext updateMemberSubscription(
+            String memberId,
+            List<String> subscriptions
+        ) {
+            return updateMemberSubscription(
+                memberId,
+                subscriptions,
+                Optional.empty(),
+                Optional.empty()
+            );
+        }
+
+        public TargetAssignmentBuilderTestContext updateMemberSubscription(
+            String memberId,
+            List<String> subscriptions,
+            Optional<String> instanceId,
+            Optional<String> rackId
+        ) {
+            ConsumerGroupMember existingMember = members.get(memberId);
+            ConsumerGroupMember.Builder builder;
+            if (existingMember != null) {
+                builder = new ConsumerGroupMember.Builder(existingMember);
+            } else {
+                builder = new ConsumerGroupMember.Builder(memberId);
+            }
+            updatedMembers.put(memberId, builder
+                .setSubscribedTopicNames(subscriptions)
+                .maybeUpdateInstanceId(instanceId)
+                .maybeUpdateRackId(rackId)
+                .build());
+            return this;
+        }
+
+        public TargetAssignmentBuilderTestContext removeMemberSubscription(
+            String memberId
+        ) {
+            this.updatedMembers.put(memberId, null);
+            return this;
+        }
+
+        public TargetAssignmentBuilderTestContext prepareMemberAssignment(
+            String memberId,
+            Map<Uuid, Set<Integer>> assignment
+        ) {
+            assignments.put(memberId, new 
org.apache.kafka.coordinator.group.assignor.MemberAssignment(assignment));
+            return this;
+        }
+
+        private AssignmentMemberSpec assignmentMemberSpec(
+            ConsumerGroupMember member,
+            Assignment targetAssignment
+        ) {
+            Set<Uuid> subscribedTopics = new HashSet<>();
+            member.subscribedTopicNames().forEach(topicName -> {
+                TopicMetadata topicMetadata = 
subscriptionMetadata.get(topicName);
+                if (topicMetadata != null) {
+                    subscribedTopics.add(topicMetadata.id());
+                }
+            });
+
+            return new AssignmentMemberSpec(
+                Optional.ofNullable(member.instanceId()),
+                Optional.ofNullable(member.rackId()),
+                subscribedTopics,
+                targetAssignment.partitions()
+            );
+        }
+
+        public TargetAssignmentBuilder.TargetAssignmentResult build() {
+            // Prepare expected member specs.
+            Map<String, AssignmentMemberSpec> memberSpecs = new HashMap<>();
+            members.forEach((memberId, member) -> {
+                memberSpecs.put(memberId, assignmentMemberSpec(
+                    member,
+                    targetAssignments.getOrDefault(memberId, Assignment.EMPTY)
+                ));
+            });
+
+            updatedMembers.forEach((memberId, updatedMemberOrNull) -> {
+                if (updatedMemberOrNull == null) {
+                    memberSpecs.remove(memberId);
+                } else {
+                    memberSpecs.put(memberId, assignmentMemberSpec(
+                        updatedMemberOrNull,
+                        targetAssignments.getOrDefault(memberId, 
Assignment.EMPTY)
+                    ));
+                }
+            });
+
+            Map<Uuid, AssignmentTopicMetadata> topicMetadata = new HashMap<>();
+            subscriptionMetadata.forEach((topicName, metadata) -> {
+                topicMetadata.put(metadata.id(), new 
AssignmentTopicMetadata(metadata.numPartitions()));
+            });
+
+            AssignmentSpec assignmentSpec = new AssignmentSpec(
+                memberSpecs,
+                topicMetadata
+            );
+
+            // We use `any` here to always return an assignment but use 
`verify` later on
+            // to ensure that the input was correct.
+            when(assignor.assign(any())).thenReturn(new 
GroupAssignment(assignments));
+
+            // Create and populate the assignment builder.
+            TargetAssignmentBuilder builder = new 
TargetAssignmentBuilder(groupId, groupEpoch, assignor)
+                .withMembers(members)
+                .withSubscriptionMetadata(subscriptionMetadata)
+                .withTargetAssignments(targetAssignments);
+
+            updatedMembers.forEach((memberId, updatedMemberOrNull) -> {
+                if (updatedMemberOrNull != null) {
+                    builder.withUpdatedMember(memberId, updatedMemberOrNull);
+                } else {
+                    builder.withRemoveMembers(memberId);
+                }
+            });
+
+            TargetAssignmentBuilder.TargetAssignmentResult result = 
builder.build();
+
+            verify(assignor, times(1)).assign(assignmentSpec);
+
+            return result;
+        }
+    }
+
+    @Test
+    public void testEmpty() {
+        TargetAssignmentBuilderTestContext context = new 
TargetAssignmentBuilderTestContext(
+            "my-group",
+            20
+        );
+
+        TargetAssignmentBuilder.TargetAssignmentResult result = 
context.build();
+        assertEquals(newTargetAssignmentEpochRecord(
+            "my-group",
+            20
+        ), result.records().get(result.records().size() - 1));

Review Comment:
   Correct. In this case, it is even better to use `Collections.singletonList` 
and to verify the records list.



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