Skip to main content

rocketmq_remoting/protocol/header/controller/
get_next_broker_id_response_header.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use cheetah_string::CheetahString;
16use rocketmq_macros::RequestHeaderCodecV2;
17use serde::Deserialize;
18use serde::Serialize;
19
20#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2)]
21#[serde(rename_all = "camelCase")]
22pub struct GetNextBrokerIdResponseHeader {
23    pub cluster_name: Option<CheetahString>,
24    pub broker_name: Option<CheetahString>,
25    pub next_broker_id: Option<u64>,
26}
27
28#[cfg(test)]
29mod tests {
30    use std::collections::HashMap;
31
32    use super::*;
33    use crate::protocol::command_custom_header::CommandCustomHeader;
34    use crate::protocol::command_custom_header::FromMap;
35
36    #[test]
37    fn get_next_broker_id_response_header_serializes_correctly() {
38        let header = GetNextBrokerIdResponseHeader {
39            cluster_name: Some(CheetahString::from_static_str("test_cluster")),
40            broker_name: Some(CheetahString::from_static_str("test_broker")),
41            next_broker_id: Some(12345),
42        };
43        let map = header.to_map().unwrap();
44        assert_eq!(
45            map.get(&CheetahString::from_static_str("clusterName")).unwrap(),
46            "test_cluster"
47        );
48        assert_eq!(
49            map.get(&CheetahString::from_static_str("brokerName")).unwrap(),
50            "test_broker"
51        );
52        assert_eq!(
53            map.get(&CheetahString::from_static_str("nextBrokerId")).unwrap(),
54            "12345"
55        );
56    }
57
58    #[test]
59    fn get_next_broker_id_response_header_deserializes_correctly() {
60        let mut map = HashMap::new();
61        map.insert(
62            CheetahString::from_static_str("clusterName"),
63            CheetahString::from_static_str("test_cluster"),
64        );
65        map.insert(
66            CheetahString::from_static_str("brokerName"),
67            CheetahString::from_static_str("test_broker"),
68        );
69        map.insert(
70            CheetahString::from_static_str("nextBrokerId"),
71            CheetahString::from_static_str("12345"),
72        );
73        let header = <GetNextBrokerIdResponseHeader as FromMap>::from(&map).unwrap();
74        assert_eq!(
75            header.cluster_name,
76            Some(CheetahString::from_static_str("test_cluster"))
77        );
78        assert_eq!(header.broker_name, Some(CheetahString::from_static_str("test_broker")));
79        assert_eq!(header.next_broker_id, Some(12345));
80    }
81
82    #[test]
83    fn get_next_broker_id_response_header_default() {
84        let header = GetNextBrokerIdResponseHeader::default();
85        assert_eq!(header.cluster_name, None);
86        assert_eq!(header.broker_name, None);
87        assert_eq!(header.next_broker_id, None);
88    }
89
90    #[test]
91    fn get_next_broker_id_response_header_clone() {
92        let header = GetNextBrokerIdResponseHeader {
93            cluster_name: Some(CheetahString::from_static_str("test_cluster")),
94            broker_name: Some(CheetahString::from_static_str("test_broker")),
95            next_broker_id: Some(12345),
96        };
97        let cloned = header.clone();
98        assert_eq!(header.cluster_name, cloned.cluster_name);
99        assert_eq!(header.broker_name, cloned.broker_name);
100        assert_eq!(header.next_broker_id, cloned.next_broker_id);
101    }
102}