Skip to main content

rocketmq_remoting/protocol/header/
query_topic_consume_by_who_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 QueryTopicConsumeByWhoRequestHeader {
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 cheetah_string::CheetahString;
37
38    use super::*;
39    use crate::protocol::command_custom_header::CommandCustomHeader;
40    use crate::protocol::command_custom_header::FromMap;
41
42    #[test]
43    fn query_topic_consume_by_who_request_header_serializes_correctly() {
44        let header: QueryTopicConsumeByWhoRequestHeader = QueryTopicConsumeByWhoRequestHeader {
45            topic: CheetahString::from_static_str("test_topic"),
46            topic_request_header: Some(TopicRequestHeader::default()),
47        };
48        let map = header.to_map().unwrap();
49        assert_eq!(map.get(&CheetahString::from_static_str("topic")).unwrap(), "test_topic");
50        assert_eq!(map.get(&CheetahString::from_static_str("topicRequestHeader")), None);
51    }
52
53    #[test]
54    fn query_topic_consume_by_who_request_header_deserializes_correctly() {
55        let mut map = HashMap::new();
56        map.insert(
57            CheetahString::from_static_str("topic"),
58            CheetahString::from_static_str("test_topic"),
59        );
60
61        let header: QueryTopicConsumeByWhoRequestHeader =
62            <QueryTopicConsumeByWhoRequestHeader as FromMap>::from(&map).unwrap();
63        assert_eq!(header.topic, "test_topic");
64    }
65
66    #[test]
67    fn query_topic_consume_by_who_request_header_deserializes_correctly_deserialize_from_json() {
68        // 测试 topic_request_header.lo 为 None 的情况
69        let data = r#"{"topic":"test_topic"}"#;
70        let header: QueryTopicConsumeByWhoRequestHeader = serde_json::from_str(data).unwrap();
71        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
72        assert!(header
73            .topic_request_header
74            .as_ref()
75            .map(|h| h.lo.is_none())
76            .unwrap_or(true));
77
78        let data = r#"{"topic":"test_topic","lo":null}"#;
79        let header: QueryTopicConsumeByWhoRequestHeader = serde_json::from_str(data).unwrap();
80        assert!(header
81            .topic_request_header
82            .as_ref()
83            .map(|h| h.lo.is_none())
84            .unwrap_or(true));
85    }
86}