Skip to main content

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