Skip to main content

rocketmq_remoting/protocol/header/
get_lite_group_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
20use crate::rpc::rpc_request_header::RpcRequestHeader;
21
22#[derive(Clone, Debug, Serialize, Deserialize, RequestHeaderCodecV2)]
23#[serde(rename_all = "camelCase")]
24pub struct GetLiteGroupInfoRequestHeader {
25    #[required]
26    pub group: CheetahString,
27
28    #[required]
29    pub lite_topic: CheetahString,
30
31    #[required]
32    pub top_k: i32,
33
34    #[serde(flatten)]
35    pub rpc: Option<RpcRequestHeader>,
36}
37
38#[cfg(test)]
39mod tests {
40    use std::collections::HashMap;
41
42    use super::*;
43    use crate::protocol::command_custom_header::CommandCustomHeader;
44    use crate::protocol::command_custom_header::FromMap;
45
46    #[test]
47    fn get_lite_group_info_request_header_creation() {
48        let header = GetLiteGroupInfoRequestHeader {
49            group: CheetahString::from("test_group"),
50            lite_topic: CheetahString::from("test_lite_topic"),
51            top_k: 10,
52            rpc: None,
53        };
54
55        assert_eq!(header.group, CheetahString::from("test_group"));
56        assert_eq!(header.lite_topic, CheetahString::from("test_lite_topic"));
57        assert_eq!(header.top_k, 10);
58        assert!(header.rpc.is_none());
59    }
60
61    #[test]
62    fn get_lite_group_info_request_header_serializes_to_map() {
63        let header = GetLiteGroupInfoRequestHeader {
64            group: CheetahString::from("my_group"),
65            lite_topic: CheetahString::from("my_lite_topic"),
66            top_k: 5,
67            rpc: None,
68        };
69
70        let map = header.to_map().unwrap();
71        assert_eq!(map.get(&CheetahString::from_static_str("group")).unwrap(), "my_group");
72        assert_eq!(
73            map.get(&CheetahString::from_static_str("liteTopic")).unwrap(),
74            "my_lite_topic"
75        );
76        assert_eq!(map.get(&CheetahString::from_static_str("topK")).unwrap(), "5");
77    }
78
79    #[test]
80    fn get_lite_group_info_request_header_deserializes_from_map() {
81        let mut map = HashMap::new();
82        map.insert(
83            CheetahString::from_static_str("group"),
84            CheetahString::from("deserialized_group"),
85        );
86        map.insert(
87            CheetahString::from_static_str("liteTopic"),
88            CheetahString::from("deserialized_lite"),
89        );
90        map.insert(CheetahString::from_static_str("topK"), CheetahString::from("20"));
91
92        let header = <GetLiteGroupInfoRequestHeader as FromMap>::from(&map).unwrap();
93        assert_eq!(header.group, CheetahString::from("deserialized_group"));
94        assert_eq!(header.lite_topic, CheetahString::from("deserialized_lite"));
95        assert_eq!(header.top_k, 20);
96    }
97
98    #[test]
99    fn get_lite_group_info_request_header_clone() {
100        let header = GetLiteGroupInfoRequestHeader {
101            group: CheetahString::from("group"),
102            lite_topic: CheetahString::from("lite"),
103            top_k: 3,
104            rpc: None,
105        };
106
107        let cloned = header.clone();
108        assert_eq!(header.group, cloned.group);
109        assert_eq!(header.lite_topic, cloned.lite_topic);
110        assert_eq!(header.top_k, cloned.top_k);
111    }
112
113    #[test]
114    fn get_lite_group_info_request_header_debug() {
115        let header = GetLiteGroupInfoRequestHeader {
116            group: CheetahString::from("group"),
117            lite_topic: CheetahString::from("lite"),
118            top_k: 7,
119            rpc: None,
120        };
121
122        let debug_str = format!("{:?}", header);
123        assert!(debug_str.contains("group"));
124        assert!(debug_str.contains("lite"));
125        assert!(debug_str.contains("7"));
126    }
127
128    #[test]
129    fn get_lite_group_info_request_header_with_different_top_k_values() {
130        let header_zero = GetLiteGroupInfoRequestHeader {
131            group: CheetahString::from("g"),
132            lite_topic: CheetahString::from("t"),
133            top_k: 0,
134            rpc: None,
135        };
136        assert_eq!(header_zero.top_k, 0);
137
138        let header_negative = GetLiteGroupInfoRequestHeader {
139            group: CheetahString::from("g"),
140            lite_topic: CheetahString::from("t"),
141            top_k: -1,
142            rpc: None,
143        };
144        assert_eq!(header_negative.top_k, -1);
145
146        let header_large = GetLiteGroupInfoRequestHeader {
147            group: CheetahString::from("g"),
148            lite_topic: CheetahString::from("t"),
149            top_k: i32::MAX,
150            rpc: None,
151        };
152        assert_eq!(header_large.top_k, i32::MAX);
153    }
154}