Skip to main content

rocketmq_remoting/protocol/header/
get_topic_stats_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::topic_request_header::TopicRequestHeader;
21
22#[derive(Serialize, Deserialize, Debug, RequestHeaderCodecV2)]
23pub struct GetTopicStatsRequestHeader {
24    #[required]
25    #[serde(rename = "topic")]
26    pub topic: CheetahString,
27
28    #[serde(flatten)]
29    pub topic_request_header: Option<TopicRequestHeader>,
30}
31
32#[cfg(test)]
33mod tests {
34    use std::collections::HashMap;
35
36    use super::*;
37    use crate::protocol::command_custom_header::FromMap;
38
39    #[test]
40    fn get_topic_stats_request_header_serialization() {
41        let header = GetTopicStatsRequestHeader {
42            topic: CheetahString::from("topic1"),
43            topic_request_header: None,
44        };
45        let json = serde_json::to_string(&header).unwrap();
46        assert!(json.contains("\"topic\":\"topic1\""));
47    }
48
49    #[test]
50    fn get_topic_stats_request_header_deserialization() {
51        let json = r#"{"topic":"topic1"}"#;
52        let header: GetTopicStatsRequestHeader = serde_json::from_str(json).unwrap();
53        assert_eq!(header.topic, "topic1");
54    }
55
56    #[test]
57    fn get_topic_stats_request_header_from_map() {
58        let mut map = HashMap::new();
59        map.insert(CheetahString::from("topic"), CheetahString::from("topic1"));
60        let header = <GetTopicStatsRequestHeader as FromMap>::from(&map).unwrap();
61        assert_eq!(header.topic, "topic1");
62    }
63}