Skip to main content

rocketmq_protocol/protocol/header/controller/
register_broker_to_controller_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::register_broker_to_controller_response_header::RegisterBrokerToControllerResponseHeader",
24    java_class = "org.apache.rocketmq.remoting.protocol.header.controller.register.RegisterBrokerToControllerResponseHeader"
25)]
26pub struct RegisterBrokerToControllerResponseHeader {
27    pub cluster_name: Option<CheetahString>,
28    pub broker_name: Option<CheetahString>,
29    pub master_broker_id: Option<i64>,
30    pub master_address: Option<CheetahString>,
31    pub master_epoch: Option<i32>,
32    pub sync_state_set_epoch: Option<i32>,
33}
34
35#[cfg(test)]
36mod tests {
37    use std::collections::HashMap;
38
39    use super::*;
40    use crate::protocol::command_custom_header::CommandCustomHeader;
41    use crate::protocol::command_custom_header::FromMap;
42
43    #[test]
44    fn register_broker_to_controller_response_header_serializes_correctly() {
45        let header = RegisterBrokerToControllerResponseHeader {
46            cluster_name: Some(CheetahString::from_static_str("test_cluster")),
47            broker_name: Some(CheetahString::from_static_str("test_broker")),
48            master_broker_id: Some(123),
49            master_address: Some(CheetahString::from_static_str("127.0.0.1:10911")),
50            master_epoch: Some(5),
51            sync_state_set_epoch: Some(10),
52        };
53        let map = header.to_map().unwrap();
54        assert_eq!(
55            map.get(&CheetahString::from_static_str("clusterName")).unwrap(),
56            "test_cluster"
57        );
58        assert_eq!(
59            map.get(&CheetahString::from_static_str("brokerName")).unwrap(),
60            "test_broker"
61        );
62        assert_eq!(
63            map.get(&CheetahString::from_static_str("masterBrokerId")).unwrap(),
64            "123"
65        );
66        assert_eq!(
67            map.get(&CheetahString::from_static_str("masterAddress")).unwrap(),
68            "127.0.0.1:10911"
69        );
70        assert_eq!(map.get(&CheetahString::from_static_str("masterEpoch")).unwrap(), "5");
71        assert_eq!(
72            map.get(&CheetahString::from_static_str("syncStateSetEpoch")).unwrap(),
73            "10"
74        );
75    }
76
77    #[test]
78    fn register_broker_to_controller_response_header_deserializes_correctly() {
79        let mut map = HashMap::new();
80        map.insert(
81            CheetahString::from_static_str("clusterName"),
82            CheetahString::from_static_str("test_cluster"),
83        );
84        map.insert(
85            CheetahString::from_static_str("brokerName"),
86            CheetahString::from_static_str("test_broker"),
87        );
88        map.insert(
89            CheetahString::from_static_str("masterBrokerId"),
90            CheetahString::from_static_str("123"),
91        );
92        map.insert(
93            CheetahString::from_static_str("masterAddress"),
94            CheetahString::from_static_str("127.0.0.1:10911"),
95        );
96        map.insert(
97            CheetahString::from_static_str("masterEpoch"),
98            CheetahString::from_static_str("5"),
99        );
100        map.insert(
101            CheetahString::from_static_str("syncStateSetEpoch"),
102            CheetahString::from_static_str("10"),
103        );
104
105        let header = <RegisterBrokerToControllerResponseHeader as FromMap>::from(&map).unwrap();
106        assert_eq!(
107            header.cluster_name,
108            Some(CheetahString::from_static_str("test_cluster"))
109        );
110        assert_eq!(header.broker_name, Some(CheetahString::from_static_str("test_broker")));
111        assert_eq!(header.master_broker_id, Some(123));
112        assert_eq!(
113            header.master_address,
114            Some(CheetahString::from_static_str("127.0.0.1:10911"))
115        );
116        assert_eq!(header.master_epoch, Some(5));
117        assert_eq!(header.sync_state_set_epoch, Some(10));
118    }
119}