Skip to main content

rocketmq_protocol/protocol/header/
delete_topic_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::delete_topic_request_header::DeleteTopicRequestHeader",
25    java_class = "org.apache.rocketmq.remoting.protocol.header.DeleteTopicRequestHeader"
26)]
27pub struct DeleteTopicRequestHeader {
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)]
38impl DeleteTopicRequestHeader {
39    const TOPIC: &'static str = "topic";
40}
41
42#[cfg(test)]
43mod tests {
44    use std::collections::HashMap;
45
46    use super::*;
47    use crate::protocol::command_custom_header::CommandCustomHeader;
48    use crate::protocol::command_custom_header::FromMap;
49
50    #[test]
51    fn delete_topic_request_header_to_map() {
52        let header = DeleteTopicRequestHeader {
53            topic: CheetahString::from("test_topic"),
54            topic_request_header: None,
55        };
56
57        let map = header.to_map().unwrap();
58        assert_eq!(
59            map.get(&CheetahString::from_static_str(DeleteTopicRequestHeader::TOPIC))
60                .unwrap(),
61            &CheetahString::from("test_topic")
62        );
63    }
64
65    #[test]
66    fn delete_topic_request_header_to_map_with_topic_request_header() {
67        let topic_request_header = TopicRequestHeader {
68            // Initialize fields as needed
69            rpc_request_header: None,
70            lo: None,
71        };
72        let header = DeleteTopicRequestHeader {
73            topic: CheetahString::from("test_topic"),
74            topic_request_header: Some(topic_request_header),
75        };
76
77        let map = header.to_map().unwrap();
78        assert_eq!(
79            map.get(&CheetahString::from_static_str(DeleteTopicRequestHeader::TOPIC))
80                .unwrap(),
81            &CheetahString::from("test_topic")
82        );
83        // Add assertions for fields from topic_request_header
84    }
85
86    #[test]
87    fn delete_topic_request_header_from_map() {
88        let mut map = HashMap::new();
89        map.insert(
90            CheetahString::from_static_str(DeleteTopicRequestHeader::TOPIC),
91            CheetahString::from("test_topic"),
92        );
93
94        let header = <DeleteTopicRequestHeader as FromMap>::from(&map).unwrap();
95        assert_eq!(header.topic, CheetahString::from("test_topic"));
96        assert!(header.topic_request_header.is_some());
97    }
98
99    #[test]
100    fn delete_topic_request_header_from_map_with_topic_request_header() {
101        let mut map = HashMap::new();
102        map.insert(
103            CheetahString::from_static_str(DeleteTopicRequestHeader::TOPIC),
104            CheetahString::from("test_topic"),
105        );
106        // Add entries for fields from topic_request_header
107
108        let header = <DeleteTopicRequestHeader as FromMap>::from(&map).unwrap();
109        assert_eq!(header.topic, CheetahString::from("test_topic"));
110        assert!(header.topic_request_header.is_some());
111        // Add assertions for fields from topic_request_header
112    }
113}