rocketmq_remoting/protocol/header/
get_topic_stats_info_request_header.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use cheetah_string::CheetahString;
18use rocketmq_macros::RequestHeaderCodecV2;
19use serde::Deserialize;
20use serde::Serialize;
21
22use crate::protocol::header::namesrv::topic_operation_header::TopicRequestHeader;
23
24#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2)]
25#[serde(rename_all = "camelCase")]
26pub struct GetTopicStatsInfoRequestHeader {
27    #[required]
28    pub topic: CheetahString,
29
30    #[serde(flatten)]
31    pub topic_request_header: Option<TopicRequestHeader>,
32}
33
34#[cfg(test)]
35mod tests {
36    use cheetah_string::CheetahString;
37
38    use super::*;
39
40    #[test]
41    fn get_topic_stats_info_request_header_serializes_correctly() {
42        let header = GetTopicStatsInfoRequestHeader {
43            topic: CheetahString::from_static_str("test_topic"),
44            topic_request_header: None,
45        };
46        let serialized = serde_json::to_string(&header).unwrap();
47        let expected = r#"{"topic":"test_topic"}"#;
48        assert_eq!(serialized, expected);
49    }
50
51    #[test]
52    fn get_topic_stats_info_request_header_deserializes_correctly() {
53        let data = r#"{"topic":"test_topic"}"#;
54        let header: GetTopicStatsInfoRequestHeader = serde_json::from_str(data).unwrap();
55        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
56        assert!(header.topic_request_header.is_some());
57    }
58
59    #[test]
60    fn get_topic_stats_info_request_header_handles_missing_optional_fields() {
61        let data = r#"{"topic":"test_topic"}"#;
62        let header: GetTopicStatsInfoRequestHeader = serde_json::from_str(data).unwrap();
63        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
64        assert!(header.topic_request_header.is_some());
65    }
66
67    #[test]
68    fn get_topic_stats_info_request_header_handles_invalid_data() {
69        let data = r#"{"topic":12345}"#;
70        let result: Result<GetTopicStatsInfoRequestHeader, _> = serde_json::from_str(data);
71        assert!(result.is_err());
72    }
73}