Repository: apache/kafka
File: clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DescribeProducersWithBrokerIdTest.java (Java)

Code before:

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;


@ClusterTestDefaults(
    brokers = 3
)
class DescribeProducersWithBrokerIdTest {
    private static final String TOPIC_NAME = "test-topic";
    private static final int NUM_PARTITIONS = 1;
    private static final short REPLICATION_FACTOR = 3;

    private final ClusterInstance clusterInstance;
    private final TopicPartition topicPartition;

    public DescribeProducersWithBrokerIdTest(ClusterInstance clusterInstance) {
        this.clusterInstance = clusterInstance;
        this.topicPartition = new TopicPartition(TOPIC_NAME, 0);
    }

    private static void sendTestRecords(Producer<byte[], byte[]> producer) {
        producer.send(new ProducerRecord<>(TOPIC_NAME, 0, "key-0".getBytes(), "value-0".getBytes()));
        producer.flush();
    }
    
    @BeforeEach
    void setUp() throws InterruptedException {
        clusterInstance.createTopic(TOPIC_NAME, NUM_PARTITIONS, REPLICATION_FACTOR);
    }

    private List<Integer> getReplicaBrokerIds(Admin admin, TopicPartition topicPartition) throws Exception {
        var topicDescription = admin.describeTopics(List.of(topicPartition.topic())).allTopicNames().get().get(topicPartition.topic());
        return topicDescription.partitions().get(topicPartition.partition()).replicas().stream()
            .map(Node::id)
            .toList();
    }
    
    private int getNonReplicaBrokerId(Admin admin, TopicPartition topicPartition) throws Exception {
        var replicaBrokerIds = getReplicaBrokerIds(admin, topicPartition);
        return clusterInstance.brokerIds().stream()
            .filter(id -> !replicaBrokerIds.contains(id))
            .findFirst()
            .orElseThrow(() -> new IllegalStateException("No non-replica broker found"));
    }
    
    private int getFollowerBrokerId(Admin admin, TopicPartition topicPartition) throws Exception {
        var replicaBrokerIds = getReplicaBrokerIds(admin, topicPartition);

Review comment (suggestion):
`0` could be replaced by the static `topicPartition#partition`.

Code after:

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;


@ClusterTestDefaults(
    brokers = 3
)
class DescribeProducersWithBrokerIdTest {
    private static final String TOPIC_NAME = "test-topic";
    private static final int NUM_PARTITIONS = 1;
    private static final short REPLICATION_FACTOR = 3;

    private static final TopicPartition TOPIC_PARTITION = new TopicPartition(TOPIC_NAME, 0);
    
    private final ClusterInstance clusterInstance;

    public DescribeProducersWithBrokerIdTest(ClusterInstance clusterInstance) {
        this.clusterInstance = clusterInstance;
    }

    private static void sendTestRecords(Producer<byte[], byte[]> producer) {
        producer.send(new ProducerRecord<>(TOPIC_NAME, TOPIC_PARTITION.partition(), "key-0".getBytes(), "value-0".getBytes()));
        producer.flush();
    }
    
    @BeforeEach
    void setUp() throws InterruptedException {
        clusterInstance.createTopic(TOPIC_NAME, NUM_PARTITIONS, REPLICATION_FACTOR);
    }

    private List<Integer> getReplicaBrokerIds(Admin admin) throws Exception {
        var topicDescription = admin.describeTopics(List.of(TOPIC_PARTITION.topic())).allTopicNames().get().get(TOPIC_PARTITION.topic());
        return topicDescription.partitions().get(TOPIC_PARTITION.partition()).replicas().stream()
            .map(Node::id)
            .toList();
    }
    
    private int getNonReplicaBrokerId(Admin admin) throws Exception {
        var replicaBrokerIds = getReplicaBrokerIds(admin);
        return clusterInstance.brokerIds().stream()
            .filter(id -> !replicaBrokerIds.contains(id))
            .findFirst()
            .orElseThrow(() -> new IllegalStateException("No non-replica broker found"));
    }
    
    private int getFollowerBrokerId(Admin admin) throws Exception {
        var replicaBrokerIds = getReplicaBrokerIds(admin);