Skip to main content

rocketmq_remoting/protocol/header/controller/
alter_sync_state_set_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 AlterSyncStateSetRequestHeader {
23    pub broker_name: CheetahString,
24    pub master_broker_id: i64,
25    pub master_epoch: i32,
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 alter_sync_state_set_request_header_serializes_correctly() {
38        let header = AlterSyncStateSetRequestHeader {
39            broker_name: CheetahString::from_static_str("test_broker"),
40            master_broker_id: 1234567890,
41            master_epoch: 5,
42        };
43        let map = header.to_map().unwrap();
44        assert_eq!(
45            map.get(&CheetahString::from_static_str("brokerName")).unwrap(),
46            "test_broker"
47        );
48        assert_eq!(
49            map.get(&CheetahString::from_static_str("masterBrokerId")).unwrap(),
50            "1234567890"
51        );
52        assert_eq!(map.get(&CheetahString::from_static_str("masterEpoch")).unwrap(), "5");
53    }
54
55    #[test]
56    fn alter_sync_state_set_request_header_deserializes_correctly() {
57        let mut map = HashMap::new();
58        map.insert(
59            CheetahString::from_static_str("brokerName"),
60            CheetahString::from_static_str("test_broker"),
61        );
62        map.insert(
63            CheetahString::from_static_str("masterBrokerId"),
64            CheetahString::from_static_str("1234567890"),
65        );
66        map.insert(
67            CheetahString::from_static_str("masterEpoch"),
68            CheetahString::from_static_str("5"),
69        );
70
71        let header = <AlterSyncStateSetRequestHeader as FromMap>::from(&map).unwrap();
72        assert_eq!(header.broker_name, "test_broker");
73        assert_eq!(header.master_broker_id, 1234567890);
74        assert_eq!(header.master_epoch, 5);
75    }
76
77    #[test]
78    fn alter_sync_state_set_request_header_default() {
79        let header = AlterSyncStateSetRequestHeader::default();
80        assert_eq!(header.broker_name, "");
81        assert_eq!(header.master_broker_id, 0);
82        assert_eq!(header.master_epoch, 0);
83    }
84
85    #[test]
86    fn alter_sync_state_set_request_header_clone() {
87        let header = AlterSyncStateSetRequestHeader {
88            broker_name: CheetahString::from_static_str("test_broker"),
89            master_broker_id: 1234567890,
90            master_epoch: 5,
91        };
92        let cloned = header.clone();
93        assert_eq!(header.broker_name, cloned.broker_name);
94        assert_eq!(header.master_broker_id, cloned.master_broker_id);
95        assert_eq!(header.master_epoch, cloned.master_epoch);
96    }
97}