Repository: apache/kafka
File: metadata/src/test/java/org/apache/kafka/controller/PartitionChangeBuilderTest.java (Java)

Code before:
                        Uuid.fromString("Y25PaCAmRfyGIKxAThhBAw")
                })
                .setIsr(new int[] {})
                .setElr(new int[] {3})
                .setLastKnownElr(new int[] {1})
                .setLeader(-1)
                .setLeaderRecoveryState(LeaderRecoveryState.RECOVERED)
                .setLeaderEpoch(100)
                .setPartitionEpoch(200)
                .build();
        Uuid topicId = Uuid.fromString("FbrrdcfiR-KC2CPSTHaJrg");

        PartitionChangeBuilder builder = new PartitionChangeBuilder(partition, topicId, 0, r -> r != 3,
                metadataVersionForPartitionChangeRecordVersion(version), 3)
            .setElection(Election.PREFERRED)
            .setEligibleLeaderReplicasEnabled(true)
            .setDefaultDirProvider(DEFAULT_DIR_PROVIDER)
            .setUseLastKnownLeaderInBalancedRecovery(true);

        builder.setTargetIsr(List.of());

        // No change to the partition.
        assertEquals(Optional.empty(), builder.build());
    }

    @Test
    public void testEligibleLeaderReplicas_NotEligibleLastKnownLeader() {
        short version = 2;
        PartitionRegistration partition = new PartitionRegistration.Builder()
                .setReplicas(new int[] {1, 2, 3, 4})
                .setDirectories(new Uuid[]{
                        Uuid.fromString("zANDdMukTEqefOvHpmniMg"),
                        Uuid.fromString("Ui2Eq8rbRiuW7m7uiPTRyg"),
                        Uuid.fromString("MhgJOZrrTsKNcGM0XKK4aA"),
                        Uuid.fromString("Y25PaCAmRfyGIKxAThhBAw")
                })
                .setIsr(new int[] {})
                .setElr(new int[] {})
                .setLastKnownElr(new int[] {1})
                .setLeader(-1)
                .setLeaderRecoveryState(LeaderRecoveryState.RECOVERED)
                .setLeaderEpoch(100)
                .setPartitionEpoch(200)
                .build();
        Uuid topicId = Uuid.fromString("FbrrdcfiR-KC2CPSTHaJrg");

        PartitionChangeBuilder builder = new PartitionChangeBuilder(partition, topicId, 0, r -> false,
                metadataVersionForPartitionChangeRecordVersion(version), 3)
            .setElection(Election.PREFERRED)
            .setEligibleLeaderReplicasEnabled(true)
            .setDefaultDirProvider(DEFAULT_DIR_PROVIDER)

Review comment (suggestion):
```suggestion
    @EnumSource(value = Election.class, names = {"PREFERRED", "UNCLEAN"})
    @ParameterizedTest
    public void testEligibleLeaderReplicas_NotEligibleLastKnownLeader(Election election) {
```

Please also change `.setElection(Election.PREFERRED)` to `.setElection(election)`

In this way, we don't need another similar test.

Code after:
                        Uuid.fromString("MhgJOZrrTsKNcGM0XKK4aA"),
                        Uuid.fromString("Y25PaCAmRfyGIKxAThhBAw")
                })
                .setIsr(new int[] {})
                .setElr(new int[] {3})
                .setLastKnownElr(new int[] {1})
                .setLeader(-1)
                .setLeaderRecoveryState(LeaderRecoveryState.RECOVERED)
                .setLeaderEpoch(100)
                .setPartitionEpoch(200)
                .build();
        Uuid topicId = Uuid.fromString("FbrrdcfiR-KC2CPSTHaJrg");

        PartitionChangeBuilder builder = new PartitionChangeBuilder(partition, topicId, 0, r -> r != 3,
                metadataVersionForPartitionChangeRecordVersion(version), 3)
            .setElection(Election.PREFERRED)
            .setEligibleLeaderReplicasEnabled(true)
            .setDefaultDirProvider(DEFAULT_DIR_PROVIDER)
            .setUseLastKnownLeaderInBalancedRecovery(true);

        builder.setTargetIsr(List.of());

        // No change to the partition.
        assertEquals(Optional.empty(), builder.build());
    }

    @ParameterizedTest
    @EnumSource(value = Election.class)
    public void testEligibleLeaderReplicas_NotEligibleLastKnownLeader(Election type) {
        short version = 2;
        PartitionRegistration partition = new PartitionRegistration.Builder()
                .setReplicas(new int[] {1, 2, 3, 4})
                .setDirectories(new Uuid[]{
                        Uuid.fromString("zANDdMukTEqefOvHpmniMg"),
                        Uuid.fromString("Ui2Eq8rbRiuW7m7uiPTRyg"),
                        Uuid.fromString("MhgJOZrrTsKNcGM0XKK4aA"),
                        Uuid.fromString("Y25PaCAmRfyGIKxAThhBAw")
                })
                .setIsr(new int[] {})
                .setElr(new int[] {})
                .setLastKnownElr(new int[] {1})
                .setLeader(-1)
                .setLeaderRecoveryState(LeaderRecoveryState.RECOVERED)
                .setLeaderEpoch(100)
                .setPartitionEpoch(200)
                .build();
        Uuid topicId = Uuid.fromString("FbrrdcfiR-KC2CPSTHaJrg");

        PartitionChangeBuilder builder = new PartitionChangeBuilder(partition, topicId, 0, r -> false,
                metadataVersionForPartitionChangeRecordVersion(version), 3)
            .setElection(type)