Skip to main content

rocketmq_remoting/protocol/header/
get_topic_stats_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::protocol::header::namesrv::topic_operation_header::TopicRequestHeader;
21
22#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2)]
23#[serde(rename_all = "camelCase")]
24pub struct GetTopicStatsInfoRequestHeader {
25    #[required]
26    pub topic: CheetahString,
27
28    #[serde(flatten)]
29    pub topic_request_header: Option<TopicRequestHeader>,
30}
31
32#[cfg(test)]
33mod tests {
34    use cheetah_string::CheetahString;
35
36    use super::*;
37
38    #[test]
39    fn get_topic_stats_info_request_header_serializes_correctly() {
40        let header = GetTopicStatsInfoRequestHeader {
41            topic: CheetahString::from_static_str("test_topic"),
42            topic_request_header: None,
43        };
44        let serialized = serde_json::to_string(&header).unwrap();
45        let expected = r#"{"topic":"test_topic"}"#;
46        assert_eq!(serialized, expected);
47    }
48
49    #[test]
50    fn get_topic_stats_info_request_header_deserializes_correctly() {
51        let data = r#"{"topic":"test_topic"}"#;
52        let header: GetTopicStatsInfoRequestHeader = serde_json::from_str(data).unwrap();
53        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
54        assert!(header.topic_request_header.is_some());
55    }
56
57    #[test]
58    fn get_topic_stats_info_request_header_handles_missing_optional_fields() {
59        let data = r#"{"topic":"test_topic"}"#;
60        let header: GetTopicStatsInfoRequestHeader = serde_json::from_str(data).unwrap();
61        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
62        assert!(header.topic_request_header.is_some());
63    }
64
65    #[test]
66    fn get_topic_stats_info_request_header_handles_invalid_data() {
67        let data = r#"{"topic":12345}"#;
68        let result: Result<GetTopicStatsInfoRequestHeader, _> = serde_json::from_str(data);
69        assert!(result.is_err());
70    }
71}