rocketmq_remoting/protocol/header/
query_topic_consume_by_who_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::rpc::topic_request_header::TopicRequestHeader;
23
24#[derive(Serialize, Deserialize, Debug, RequestHeaderCodecV2)]
25pub struct QueryTopicConsumeByWhoRequestHeader {
26    #[required]
27    #[serde(rename = "topic")]
28    pub topic: CheetahString,
29
30    #[serde(flatten)]
31    pub topic_request_header: Option<TopicRequestHeader>,
32}
33
34#[cfg(test)]
35mod tests {
36    use std::collections::HashMap;
37
38    use cheetah_string::CheetahString;
39
40    use super::*;
41    use crate::protocol::command_custom_header::CommandCustomHeader;
42    use crate::protocol::command_custom_header::FromMap;
43
44    #[test]
45    fn query_topic_consume_by_who_request_header_serializes_correctly() {
46        let header: QueryTopicConsumeByWhoRequestHeader = QueryTopicConsumeByWhoRequestHeader {
47            topic: CheetahString::from_static_str("test_topic"),
48            topic_request_header: Some(TopicRequestHeader::default()),
49        };
50        let map = header.to_map().unwrap();
51        assert_eq!(
52            map.get(&CheetahString::from_static_str("topic")).unwrap(),
53            "test_topic"
54        );
55        assert_eq!(
56            map.get(&CheetahString::from_static_str("topicRequestHeader")),
57            None
58        );
59    }
60
61    #[test]
62    fn query_topic_consume_by_who_request_header_deserializes_correctly() {
63        let mut map = HashMap::new();
64        map.insert(
65            CheetahString::from_static_str("topic"),
66            CheetahString::from_static_str("test_topic"),
67        );
68
69        let header: QueryTopicConsumeByWhoRequestHeader =
70            <QueryTopicConsumeByWhoRequestHeader as FromMap>::from(&map).unwrap();
71        assert_eq!(header.topic, "test_topic");
72    }
73
74    #[test]
75    fn query_topic_consume_by_who_request_header_deserializes_correctly_deserialize_from_json() {
76        // 测试 topic_request_header.lo 为 None 的情况
77        let data = r#"{"topic":"test_topic"}"#;
78        let header: QueryTopicConsumeByWhoRequestHeader = serde_json::from_str(data).unwrap();
79        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
80        assert!(header
81            .topic_request_header
82            .as_ref()
83            .map(|h| h.lo.is_none())
84            .unwrap_or(true));
85
86        let data = r#"{"topic":"test_topic","lo":null}"#;
87        let header: QueryTopicConsumeByWhoRequestHeader = serde_json::from_str(data).unwrap();
88        assert!(header
89            .topic_request_header
90            .as_ref()
91            .map(|h| h.lo.is_none())
92            .unwrap_or(true));
93    }
94}