Skip to main content

rocketmq_remoting/protocol/header/controller/
apply_broker_id_request_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 ApplyBrokerIdRequestHeader {
23    pub cluster_name: CheetahString,
24    pub broker_name: CheetahString,
25    pub applied_broker_id: i64,
26    pub register_check_code: CheetahString,
27}
28
29#[cfg(test)]
30mod tests {
31    use std::collections::HashMap;
32
33    use super::*;
34    use crate::protocol::command_custom_header::CommandCustomHeader;
35    use crate::protocol::command_custom_header::FromMap;
36
37    #[test]
38    fn apply_broker_id_request_header_serializes_correctly() {
39        let header = ApplyBrokerIdRequestHeader {
40            cluster_name: CheetahString::from_static_str("test_cluster"),
41            broker_name: CheetahString::from_static_str("test_broker"),
42            applied_broker_id: 9876543210,
43            register_check_code: CheetahString::from_static_str("check_code_123"),
44        };
45        let map = header.to_map().unwrap();
46        assert_eq!(
47            map.get(&CheetahString::from_static_str("clusterName")).unwrap(),
48            "test_cluster"
49        );
50        assert_eq!(
51            map.get(&CheetahString::from_static_str("brokerName")).unwrap(),
52            "test_broker"
53        );
54        assert_eq!(
55            map.get(&CheetahString::from_static_str("appliedBrokerId")).unwrap(),
56            "9876543210"
57        );
58        assert_eq!(
59            map.get(&CheetahString::from_static_str("registerCheckCode")).unwrap(),
60            "check_code_123"
61        );
62    }
63
64    #[test]
65    fn apply_broker_id_request_header_deserializes_correctly() {
66        let mut map = HashMap::new();
67        map.insert(
68            CheetahString::from_static_str("clusterName"),
69            CheetahString::from_static_str("test_cluster"),
70        );
71        map.insert(
72            CheetahString::from_static_str("brokerName"),
73            CheetahString::from_static_str("test_broker"),
74        );
75        map.insert(
76            CheetahString::from_static_str("appliedBrokerId"),
77            CheetahString::from_static_str("9876543210"),
78        );
79        map.insert(
80            CheetahString::from_static_str("registerCheckCode"),
81            CheetahString::from_static_str("check_code_123"),
82        );
83
84        let header = <ApplyBrokerIdRequestHeader as FromMap>::from(&map).unwrap();
85        assert_eq!(header.cluster_name, "test_cluster");
86        assert_eq!(header.broker_name, "test_broker");
87        assert_eq!(header.applied_broker_id, 9876543210);
88        assert_eq!(header.register_check_code, "check_code_123");
89    }
90
91    #[test]
92    fn apply_broker_id_request_header_default() {
93        let header = ApplyBrokerIdRequestHeader::default();
94        assert_eq!(header.cluster_name, "");
95        assert_eq!(header.broker_name, "");
96        assert_eq!(header.applied_broker_id, 0);
97        assert_eq!(header.register_check_code, "");
98    }
99
100    #[test]
101    fn apply_broker_id_request_header_clone() {
102        let header = ApplyBrokerIdRequestHeader {
103            cluster_name: CheetahString::from_static_str("test_cluster"),
104            broker_name: CheetahString::from_static_str("test_broker"),
105            applied_broker_id: 9876543210,
106            register_check_code: CheetahString::from_static_str("check_code_123"),
107        };
108        let cloned = header.clone();
109        assert_eq!(header.cluster_name, cloned.cluster_name);
110        assert_eq!(header.broker_name, cloned.broker_name);
111        assert_eq!(header.applied_broker_id, cloned.applied_broker_id);
112        assert_eq!(header.register_check_code, cloned.register_check_code);
113    }
114}