Skip to main content

rocketmq_protocol/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::RequestHeaderCodec;
17use serde::Deserialize;
18use serde::Serialize;
19
20#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodec)]
21#[serde(rename_all = "camelCase")]
22#[header(
23    type_id = "rocketmq_protocol::protocol::header::controller::get_next_broker_id_response_header::GetNextBrokerIdResponseHeader",
24    java_class = "org.apache.rocketmq.remoting.protocol.header.controller.register.GetNextBrokerIdResponseHeader"
25)]
26pub struct GetNextBrokerIdResponseHeader {
27    pub cluster_name: Option<CheetahString>,
28    pub broker_name: Option<CheetahString>,
29    #[header(range = "i64")]
30    pub next_broker_id: Option<u64>,
31}
32
33#[cfg(test)]
34mod tests {
35    use std::collections::HashMap;
36
37    use super::*;
38    use crate::protocol::command_custom_header::CommandCustomHeader;
39    use crate::protocol::command_custom_header::FromMap;
40
41    #[test]
42    fn get_next_broker_id_response_header_serializes_correctly() {
43        let header = GetNextBrokerIdResponseHeader {
44            cluster_name: Some(CheetahString::from_static_str("test_cluster")),
45            broker_name: Some(CheetahString::from_static_str("test_broker")),
46            next_broker_id: Some(12345),
47        };
48        let map = header.to_map().unwrap();
49        assert_eq!(
50            map.get(&CheetahString::from_static_str("clusterName")).unwrap(),
51            "test_cluster"
52        );
53        assert_eq!(
54            map.get(&CheetahString::from_static_str("brokerName")).unwrap(),
55            "test_broker"
56        );
57        assert_eq!(
58            map.get(&CheetahString::from_static_str("nextBrokerId")).unwrap(),
59            "12345"
60        );
61    }
62
63    #[test]
64    fn get_next_broker_id_response_header_deserializes_correctly() {
65        let mut map = HashMap::new();
66        map.insert(
67            CheetahString::from_static_str("clusterName"),
68            CheetahString::from_static_str("test_cluster"),
69        );
70        map.insert(
71            CheetahString::from_static_str("brokerName"),
72            CheetahString::from_static_str("test_broker"),
73        );
74        map.insert(
75            CheetahString::from_static_str("nextBrokerId"),
76            CheetahString::from_static_str("12345"),
77        );
78        let header = <GetNextBrokerIdResponseHeader as FromMap>::from(&map).unwrap();
79        assert_eq!(
80            header.cluster_name,
81            Some(CheetahString::from_static_str("test_cluster"))
82        );
83        assert_eq!(header.broker_name, Some(CheetahString::from_static_str("test_broker")));
84        assert_eq!(header.next_broker_id, Some(12345));
85    }
86}