Skip to main content

rocketmq_protocol/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::RequestHeaderCodec;
17use serde::Deserialize;
18use serde::Serialize;
19
20fn default_max_count() -> i32 {
21    1000
22}
23
24#[doc = "REQUEST_HEADER_CODEC_INCREMENTAL_PROBE: 0"]
25#[derive(Clone, Debug, Serialize, Deserialize, RequestHeaderCodec)]
26#[header(
27    type_id = "rocketmq_protocol::protocol::header::get_lite_client_info_request_header::GetLiteClientInfoRequestHeader",
28    java_class = "org.apache.rocketmq.remoting.protocol.header.GetLiteClientInfoRequestHeader",
29    validate = "Self::validate",
30    fast
31)]
32#[serde(rename_all = "camelCase")]
33pub struct GetLiteClientInfoRequestHeader {
34    pub parent_topic: Option<CheetahString>,
35    pub group: Option<CheetahString>,
36    pub client_id: Option<CheetahString>,
37
38    #[serde(default = "default_max_count")]
39    #[header(default_with = "default_max_count", default_semantic = "literal:1000")]
40    pub max_count: i32,
41}
42
43impl GetLiteClientInfoRequestHeader {
44    fn validate(&self) -> Result<(), crate::ProtocolContractViolation> {
45        if self.max_count > 0 {
46            return Ok(());
47        }
48        Err(crate::ProtocolContractViolation::Validation {
49            header: "rocketmq_protocol::protocol::header::get_lite_client_info_request_header::GetLiteClientInfoRequestHeader",
50            rule: "max_count_positive",
51        })
52    }
53}
54
55impl Default for GetLiteClientInfoRequestHeader {
56    fn default() -> Self {
57        Self {
58            parent_topic: None,
59            group: None,
60            client_id: None,
61            max_count: default_max_count(),
62        }
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use std::collections::HashMap;
69
70    use super::*;
71    use crate::protocol::command_custom_header::CommandCustomHeader;
72    use crate::protocol::command_custom_header::FromMap;
73
74    #[test]
75    fn get_lite_client_info_request_header_serializes_to_map() {
76        let header = GetLiteClientInfoRequestHeader {
77            parent_topic: Some("parent".into()),
78            group: Some("group".into()),
79            client_id: Some("client".into()),
80            max_count: 32,
81        };
82
83        let map = header.to_map().unwrap();
84        assert_eq!(
85            map.get(&CheetahString::from_static_str("parentTopic")).unwrap(),
86            "parent"
87        );
88        assert_eq!(map.get(&CheetahString::from_static_str("group")).unwrap(), "group");
89        assert_eq!(map.get(&CheetahString::from_static_str("clientId")).unwrap(), "client");
90        assert_eq!(map.get(&CheetahString::from_static_str("maxCount")).unwrap(), "32");
91    }
92
93    #[test]
94    fn get_lite_client_info_request_header_deserializes_from_map() {
95        let mut map = HashMap::new();
96        map.insert(
97            CheetahString::from_static_str("parentTopic"),
98            CheetahString::from_static_str("parent"),
99        );
100        map.insert(
101            CheetahString::from_static_str("group"),
102            CheetahString::from_static_str("group"),
103        );
104        map.insert(
105            CheetahString::from_static_str("clientId"),
106            CheetahString::from_static_str("client"),
107        );
108        map.insert(
109            CheetahString::from_static_str("maxCount"),
110            CheetahString::from_static_str("16"),
111        );
112
113        let header = <GetLiteClientInfoRequestHeader as FromMap>::from(&map).unwrap();
114        assert_eq!(header.parent_topic, Some("parent".into()));
115        assert_eq!(header.group, Some("group".into()));
116        assert_eq!(header.client_id, Some("client".into()));
117        assert_eq!(header.max_count, 16);
118    }
119
120    #[test]
121    fn default_paths_use_java_max_count() {
122        let default = GetLiteClientInfoRequestHeader::default();
123        assert!(default.parent_topic.is_none());
124        assert!(default.group.is_none());
125        assert!(default.client_id.is_none());
126        assert_eq!(default.max_count, 1000);
127
128        let header = <GetLiteClientInfoRequestHeader as FromMap>::from(&HashMap::new()).unwrap();
129        assert_eq!(header.max_count, 1000);
130    }
131
132    #[test]
133    fn malformed_or_non_positive_max_count_is_rejected() {
134        for value in ["invalid", "0", "-1"] {
135            let map = HashMap::from([("maxCount".into(), value.into())]);
136            assert!(<GetLiteClientInfoRequestHeader as FromMap>::from(&map).is_err());
137        }
138    }
139}