static AssignReplicasToDirsRequestData buildRequestData(
        int nodeId,
        long brokerEpoch,
        Map<TopicIdPartition, Assignment> assignments
    ) {
        Map<Uuid, DirectoryData> directoryMap = new HashMap<>();
        Map<Uuid, Map<Uuid, TopicData>> topicMap = new HashMap<>();
        for (Map.Entry<TopicIdPartition, Assignment> entry : assignments.entrySet()) {
            TopicIdPartition topicPartition = entry.getKey();
            Uuid directoryId = entry.getValue().directoryId();
            DirectoryData directory = directoryMap.computeIfAbsent(directoryId, d -> new DirectoryData().setId(directoryId));
            TopicData topic = topicMap.computeIfAbsent(directoryId, d -> new HashMap<>())
                    .computeIfAbsent(topicPartition.topicId(), topicId -> {
                        TopicData data = new TopicData().setTopicId(topicId);
                        directory.topics().add(data);
                        return data;
                    });
            PartitionData partition = new PartitionData().setPartitionIndex(topicPartition.partitionId());
            topic.partitions().add(partition);
        }
        return new AssignReplicasToDirsRequestData()
                .setBrokerId(nodeId)
                .setBrokerEpoch(brokerEpoch)
                .setDirectories(new ArrayList<>(directoryMap.values()));
    }
@Test
    void testBuildRequestData() {
        Map<TopicIdPartition, Uuid> assignments = new LinkedHashMap<>();
        assignments.put(new TopicIdPartition(TOPIC_1, 1), DIR_1);
        assignments.put(new TopicIdPartition(TOPIC_1, 2), DIR_2);
        assignments.put(new TopicIdPartition(TOPIC_1, 3), DIR_3);
        assignments.put(new TopicIdPartition(TOPIC_1, 4), DIR_1);
        assignments.put(new TopicIdPartition(TOPIC_2, 5), DIR_2);
        Map<TopicIdPartition, Assignment> targetAssignments = new LinkedHashMap<>();
        assignments.entrySet().forEach(e -> targetAssignments.put(e.getKey(),
            new Assignment(e.getKey(), e.getValue(), 0, () -> { })));
        AssignReplicasToDirsRequestData built =
            AssignmentsManager.buildRequestData(8, 100L, targetAssignments);
        AssignReplicasToDirsRequestData expected = new AssignReplicasToDirsRequestData().
            setBrokerId(8).
            setBrokerEpoch(100L).
            setDirectories(Arrays.asList(
                new AssignReplicasToDirsRequestData.DirectoryData().
                    setId(DIR_2).
                    setTopics(Arrays.asList(
                        new AssignReplicasToDirsRequestData.TopicData().
                            setTopicId(TOPIC_1).
                            setPartitions(Collections.singletonList(
                                new AssignReplicasToDirsRequestData.PartitionData().
                                    setPartitionIndex(2))),
                new AssignReplicasToDirsRequestData.TopicData().
                    setTopicId(TOPIC_2).
                    setPartitions(Collections.singletonList(
                        new AssignReplicasToDirsRequestData.PartitionData().
                            setPartitionIndex(5))))),
                new AssignReplicasToDirsRequestData.DirectoryData().
                    setId(DIR_3).
                    setTopics(Collections.singletonList(
                        new AssignReplicasToDirsRequestData.TopicData().
                            setTopicId(TOPIC_1).
                            setPartitions(Collections.singletonList(
                                new AssignReplicasToDirsRequestData.PartitionData().
                                    setPartitionIndex(3))))),
                new AssignReplicasToDirsRequestData.DirectoryData().
                    setId(DIR_1).
                    setTopics(Collections.singletonList(
                        new AssignReplicasToDirsRequestData.TopicData().
                            setTopicId(TOPIC_1).
                            setPartitions(Arrays.asList(
                                new AssignReplicasToDirsRequestData.PartitionData().
                                    setPartitionIndex(1),
                                new AssignReplicasToDirsRequestData.PartitionData().
                                    setPartitionIndex(4)))))));
        assertEquals(expected, built);
    }