Skip to main content

rocketmq_protocol/protocol/header/controller/
get_replica_info_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 rocketmq_macros::RequestHeaderCodec;
16use serde::Deserialize;
17use serde::Serialize;
18
19#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodec)]
20#[serde(rename_all = "camelCase")]
21#[header(
22    type_id = "rocketmq_protocol::protocol::header::controller::get_replica_info_response_header::GetReplicaInfoResponseHeader",
23    java_class = "org.apache.rocketmq.remoting.protocol.header.controller.GetReplicaInfoResponseHeader"
24)]
25pub struct GetReplicaInfoResponseHeader {
26    pub master_broker_id: Option<i64>,
27    pub master_address: Option<String>,
28    pub master_epoch: Option<i32>,
29}
30
31#[cfg(test)]
32mod tests {
33    use std::collections::HashMap;
34
35    use cheetah_string::CheetahString;
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_replica_info_response_header_serializes_correctly() {
43        let header = GetReplicaInfoResponseHeader {
44            master_broker_id: Some(123),
45            master_address: Some("127.0.0.1:10911".to_string()),
46            master_epoch: Some(5),
47        };
48        let map = header.to_map().unwrap();
49        assert_eq!(
50            map.get(&CheetahString::from_static_str("masterBrokerId")).unwrap(),
51            "123"
52        );
53        assert_eq!(
54            map.get(&CheetahString::from_static_str("masterAddress")).unwrap(),
55            "127.0.0.1:10911"
56        );
57        assert_eq!(map.get(&CheetahString::from_static_str("masterEpoch")).unwrap(), "5");
58    }
59
60    #[test]
61    fn get_replica_info_response_header_deserializes_correctly() {
62        let mut map = HashMap::new();
63        map.insert(
64            CheetahString::from_static_str("masterBrokerId"),
65            CheetahString::from_static_str("123"),
66        );
67        map.insert(
68            CheetahString::from_static_str("masterAddress"),
69            CheetahString::from_static_str("127.0.0.1:10911"),
70        );
71        map.insert(
72            CheetahString::from_static_str("masterEpoch"),
73            CheetahString::from_static_str("5"),
74        );
75
76        let header = <GetReplicaInfoResponseHeader as FromMap>::from(&map).unwrap();
77        assert_eq!(header.master_broker_id, Some(123));
78        assert_eq!(header.master_address, Some("127.0.0.1:10911".to_string()));
79        assert_eq!(header.master_epoch, Some(5));
80    }
81}