rocketmq_remoting/protocol/header/
get_meta_data_response_header.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use rocketmq_macros::RequestHeaderCodecV2;
18use serde::Deserialize;
19use serde::Serialize;
20
21use crate::protocol::CheetahString;
22
23#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2)]
24#[serde(rename_all = "camelCase")]
25pub struct GetMetaDataResponseHeader {
26    pub group: Option<CheetahString>,
27    pub controller_leader_id: Option<CheetahString>,
28    pub controller_leader_address: Option<CheetahString>,
29    pub is_leader: Option<bool>,
30    pub peers: Option<CheetahString>,
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_meta_data_response_header_serializes_correctly() {
43        let header = GetMetaDataResponseHeader {
44            group: Some(CheetahString::from_static_str("test_group")),
45            controller_leader_id: Some(CheetahString::from_static_str("1")),
46            controller_leader_address: Some(CheetahString::from_static_str("192.168.1.1:9876")),
47            is_leader: Some(true),
48            peers: Some(CheetahString::from_static_str(
49                "192.168.1.1:9876,192.168.1.2:9876",
50            )),
51        };
52        let map = header.to_map().unwrap();
53        assert_eq!(
54            map.get(&CheetahString::from_static_str(
55                GetMetaDataResponseHeader::GROUP
56            ))
57            .unwrap(),
58            "test_group"
59        );
60        assert_eq!(
61            map.get(&CheetahString::from_static_str(
62                GetMetaDataResponseHeader::CONTROLLER_LEADER_ID
63            ))
64            .unwrap(),
65            "1"
66        );
67        assert_eq!(
68            map.get(&CheetahString::from_static_str(
69                GetMetaDataResponseHeader::CONTROLLER_LEADER_ADDRESS
70            ))
71            .unwrap(),
72            "192.168.1.1:9876"
73        );
74        assert_eq!(
75            map.get(&CheetahString::from_static_str(
76                GetMetaDataResponseHeader::IS_LEADER
77            ))
78            .unwrap(),
79            "true"
80        );
81        assert_eq!(
82            map.get(&CheetahString::from_static_str(
83                GetMetaDataResponseHeader::PEERS
84            ))
85            .unwrap(),
86            "192.168.1.1:9876,192.168.1.2:9876"
87        );
88    }
89
90    #[test]
91    fn get_meta_data_response_header_deserializes_correctly() {
92        let mut map = HashMap::new();
93        map.insert(
94            CheetahString::from_static_str(GetMetaDataResponseHeader::GROUP),
95            CheetahString::from("test_group"),
96        );
97        map.insert(
98            CheetahString::from_static_str(GetMetaDataResponseHeader::CONTROLLER_LEADER_ID),
99            CheetahString::from("1"),
100        );
101        map.insert(
102            CheetahString::from_static_str(GetMetaDataResponseHeader::CONTROLLER_LEADER_ADDRESS),
103            CheetahString::from("192.168.1.1:9876"),
104        );
105        map.insert(
106            CheetahString::from_static_str(GetMetaDataResponseHeader::IS_LEADER),
107            CheetahString::from("true"),
108        );
109        map.insert(
110            CheetahString::from_static_str(GetMetaDataResponseHeader::PEERS),
111            CheetahString::from("192.168.1.1:9876,192.168.1.2:9876"),
112        );
113        let header = <GetMetaDataResponseHeader as FromMap>::from(&map).unwrap();
114        assert_eq!(
115            header.group,
116            Some(CheetahString::from_static_str("test_group"))
117        );
118        assert_eq!(
119            header.controller_leader_id,
120            Some(CheetahString::from_static_str("1"))
121        );
122        assert_eq!(
123            header.controller_leader_address,
124            Some(CheetahString::from_static_str("192.168.1.1:9876"))
125        );
126        assert_eq!(header.is_leader, Some(true));
127        assert_eq!(
128            header.peers,
129            Some(CheetahString::from_static_str(
130                "192.168.1.1:9876,192.168.1.2:9876"
131            ))
132        );
133    }
134
135    #[test]
136    fn get_meta_data_response_header_handles_missing_optional_fields() {
137        let map = HashMap::new();
138        let header = <GetMetaDataResponseHeader as FromMap>::from(&map).unwrap();
139        assert!(header.group.is_none());
140        assert!(header.controller_leader_id.is_none());
141        assert!(header.controller_leader_address.is_none());
142        assert!(header.is_leader.is_none());
143        assert!(header.peers.is_none());
144    }
145}