rocketmq_remoting/protocol/header/
query_consume_time_span_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 QueryConsumeTimeSpanRequestHeader {
27    #[required]
28    pub topic: CheetahString,
29
30    #[required]
31    pub group: CheetahString,
32
33    #[serde(flatten)]
34    pub topic_request_header: Option<TopicRequestHeader>,
35}
36
37#[cfg(test)]
38mod tests {
39    use cheetah_string::CheetahString;
40
41    use super::*;
42
43    #[test]
44    fn query_consume_time_span_request_header_serializes_correctly() {
45        let header = QueryConsumeTimeSpanRequestHeader {
46            topic: CheetahString::from_static_str("test_topic"),
47            group: CheetahString::from_static_str("test_group"),
48            topic_request_header: None,
49        };
50        let serialized = serde_json::to_string(&header).unwrap();
51        let expected = r#"{"topic":"test_topic","group":"test_group"}"#;
52        assert_eq!(serialized, expected);
53    }
54
55    #[test]
56    fn query_consume_time_span_request_header_deserializes_correctly() {
57        let data = r#"{"topic":"test_topic","group":"test_group"}"#;
58        let header: QueryConsumeTimeSpanRequestHeader = serde_json::from_str(data).unwrap();
59        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
60        assert_eq!(header.group, CheetahString::from_static_str("test_group"));
61        assert!(header.topic_request_header.is_some());
62    }
63
64    #[test]
65    fn query_consume_time_span_request_header_handles_missing_optional_fields() {
66        let data = r#"{"topic":"test_topic","group":"test_group"}"#;
67        let header: QueryConsumeTimeSpanRequestHeader = serde_json::from_str(data).unwrap();
68        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
69        assert_eq!(header.group, CheetahString::from_static_str("test_group"));
70        assert!(header.topic_request_header.is_some());
71    }
72
73    #[test]
74    fn query_consume_time_span_request_header_handles_invalid_data() {
75        let data = r#"{"topic":12345,"group":"test_group"}"#;
76        let result: Result<QueryConsumeTimeSpanRequestHeader, _> = serde_json::from_str(data);
77        assert!(result.is_err());
78    }
79}