Skip to main content

rocketmq_remoting/protocol/header/
get_lite_client_info_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
20fn default_max_count() -> i32 {
21    1000
22}
23
24#[derive(Clone, Debug, Serialize, Deserialize, RequestHeaderCodecV2)]
25#[serde(rename_all = "camelCase")]
26pub struct GetLiteClientInfoRequestHeader {
27    pub parent_topic: Option<CheetahString>,
28    pub group: Option<CheetahString>,
29    pub client_id: Option<CheetahString>,
30
31    #[serde(default = "default_max_count")]
32    pub max_count: i32,
33}
34
35impl Default for GetLiteClientInfoRequestHeader {
36    fn default() -> Self {
37        Self {
38            parent_topic: None,
39            group: None,
40            client_id: None,
41            max_count: default_max_count(),
42        }
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use std::collections::HashMap;
49
50    use super::*;
51    use crate::protocol::command_custom_header::CommandCustomHeader;
52    use crate::protocol::command_custom_header::FromMap;
53
54    #[test]
55    fn get_lite_client_info_request_header_defaults_max_count() {
56        let header = GetLiteClientInfoRequestHeader::default();
57        assert_eq!(header.max_count, 1000);
58        assert!(header.parent_topic.is_none());
59        assert!(header.group.is_none());
60        assert!(header.client_id.is_none());
61    }
62
63    #[test]
64    fn get_lite_client_info_request_header_serializes_to_map() {
65        let header = GetLiteClientInfoRequestHeader {
66            parent_topic: Some("parent".into()),
67            group: Some("group".into()),
68            client_id: Some("client".into()),
69            max_count: 32,
70        };
71
72        let map = header.to_map().unwrap();
73        assert_eq!(
74            map.get(&CheetahString::from_static_str("parentTopic")).unwrap(),
75            "parent"
76        );
77        assert_eq!(map.get(&CheetahString::from_static_str("group")).unwrap(), "group");
78        assert_eq!(map.get(&CheetahString::from_static_str("clientId")).unwrap(), "client");
79        assert_eq!(map.get(&CheetahString::from_static_str("maxCount")).unwrap(), "32");
80    }
81
82    #[test]
83    fn get_lite_client_info_request_header_deserializes_from_map() {
84        let mut map = HashMap::new();
85        map.insert(
86            CheetahString::from_static_str("parentTopic"),
87            CheetahString::from_static_str("parent"),
88        );
89        map.insert(
90            CheetahString::from_static_str("group"),
91            CheetahString::from_static_str("group"),
92        );
93        map.insert(
94            CheetahString::from_static_str("clientId"),
95            CheetahString::from_static_str("client"),
96        );
97        map.insert(
98            CheetahString::from_static_str("maxCount"),
99            CheetahString::from_static_str("16"),
100        );
101
102        let header = <GetLiteClientInfoRequestHeader as FromMap>::from(&map).unwrap();
103        assert_eq!(header.parent_topic, Some("parent".into()));
104        assert_eq!(header.group, Some("group".into()));
105        assert_eq!(header.client_id, Some("client".into()));
106        assert_eq!(header.max_count, 16);
107    }
108}