Repository: apache/kafka
File: raft/src/test/java/org/apache/kafka/raft/RaftUtilTest.java (Java)

Code before:
                0
        );
        JsonNode json = DescribeQuorumResponseDataJsonConverter.write(describeQuorumResponseData, version);
        assertEquals(expectedJson, json.toString());
    }

    @Test
    public void testAddRaftVoterRequestAckWhenCommittedNotIgnorable() {
        AddRaftVoterRequestData request = new AddRaftVoterRequestData()
            .setClusterId("test-cluster")
            .setTimeoutMs(1000)
            .setVoterId(1)
            .setVoterDirectoryId(Uuid.randomUuid())
            .setAckWhenCommitted(false); // field only exists in v1+

        // Attempt to serialize to version 0, which does not support ackWhenCommitted
        assertThrows(
            UnsupportedVersionException.class,
            () -> AddRaftVoterRequestDataJsonConverter.write(request, (short) 0)
        );
    }

    private Records createRecords() {
        ByteBuffer allocate = ByteBuffer.allocate(1024);

        try (MemoryRecordsBuilder builder = MemoryRecords.builder(allocate,
                Compression.NONE,
                TimestampType.CREATE_TIME,
                0)) {
            for (int i = 0; i < 10; ++i) {
                builder.append(0L, "key".getBytes(), ("value-" + i).getBytes());
            }
            return builder.build();
        }
    }

    private static class FetchRequestTestCase {
        private final Uuid replicaDirectoryId;
        private final short version;
        private final short lastFetchedEpoch;
        private final String expectedJson;

        private FetchRequestTestCase(Uuid replicaDirectoryId, short version,
                                     short lastFetchedEpoch, String expectedJson) {
            this.replicaDirectoryId = replicaDirectoryId;
            this.version = version;
            this.lastFetchedEpoch = lastFetchedEpoch;
            this.expectedJson = expectedJson;
        }
    }


Review comment (suggestion):
Iterating over all errors should be unnecessary, but it's fine since it runs very quickly

Code after:
                List.of(replicaState),
                0
        );
        JsonNode json = DescribeQuorumResponseDataJsonConverter.write(describeQuorumResponseData, version);
        assertEquals(expectedJson, json.toString());
    }

    @Test
    public void testAddRaftVoterRequestAckWhenCommittedNotIgnorable() {
        AddRaftVoterRequestData request = new AddRaftVoterRequestData()
            .setClusterId("test-cluster")
            .setTimeoutMs(1000)
            .setVoterId(1)
            .setVoterDirectoryId(Uuid.randomUuid())
            .setAckWhenCommitted(false); // field only exists in v1+

        // Attempt to serialize to version 0, which does not support ackWhenCommitted
        assertThrows(
            UnsupportedVersionException.class,
            () -> AddRaftVoterRequestDataJsonConverter.write(request, (short) 0)
        );
    }

    @Test
    public void testAddVoterResponse() {
        for (Errors error : Errors.values()) {
            AddRaftVoterResponseData addRaftVoterResponseData = RaftUtil.addVoterResponse(error, null);
            assertEquals(error.code(), addRaftVoterResponseData.errorCode());
            if (Errors.NONE.equals(error))
                assertNull(addRaftVoterResponseData.errorMessage());
            else
                assertEquals(error.message(), addRaftVoterResponseData.errorMessage());
        }
    }

    @Test
    public void testRemoveVoterResponse() {
        for (Errors error : Errors.values()) {
            RemoveRaftVoterResponseData removeRaftVoterResponseData = RaftUtil.removeVoterResponse(error, null);
            assertEquals(error.code(), removeRaftVoterResponseData.errorCode());
            if (Errors.NONE.equals(error))
                assertNull(removeRaftVoterResponseData.errorMessage());
            else
                assertEquals(error.message(), removeRaftVoterResponseData.errorMessage());
        }
    }

    private Records createRecords() {
        ByteBuffer allocate = ByteBuffer.allocate(1024);

        try (MemoryRecordsBuilder builder = MemoryRecords.builder(allocate,