Skip to main content

rocketmq_remoting/protocol/header/
get_topic_config_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 GetTopicConfigRequestHeader {
24    #[required]
25    #[serde(rename = "topic")]
26    pub topic: CheetahString,
27
28    #[serde(flatten)]
29    pub topic_request_header: Option<TopicRequestHeader>,
30}
31
32impl GetTopicConfigRequestHeader {
33    pub fn get_topic(&self) -> &CheetahString {
34        &self.topic
35    }
36    pub fn set_topic(&mut self, topic: CheetahString) {
37        self.topic = topic;
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use std::collections::HashMap;
44
45    use super::*;
46    use crate::protocol::command_custom_header::FromMap;
47
48    #[test]
49    fn get_topic_config_request_header_serialization() {
50        let header = GetTopicConfigRequestHeader {
51            topic: CheetahString::from("topic1"),
52            topic_request_header: None,
53        };
54        let json = serde_json::to_string(&header).unwrap();
55        assert!(json.contains("\"topic\":\"topic1\""));
56    }
57
58    #[test]
59    fn get_topic_config_request_header_deserialization() {
60        let json = r#"{"topic":"topic1"}"#;
61        let header: GetTopicConfigRequestHeader = serde_json::from_str(json).unwrap();
62        assert_eq!(header.topic, "topic1");
63    }
64
65    #[test]
66    fn get_topic_config_request_header_from_map() {
67        let mut map = HashMap::new();
68        map.insert(CheetahString::from("topic"), CheetahString::from("topic1"));
69        let header = <GetTopicConfigRequestHeader as FromMap>::from(&map).unwrap();
70        assert_eq!(header.topic, "topic1");
71    }
72
73    #[test]
74    fn getters_and_setters() {
75        let mut header = GetTopicConfigRequestHeader {
76            topic: CheetahString::from("topic1"),
77            topic_request_header: None,
78        };
79        assert_eq!(header.get_topic(), "topic1");
80        header.set_topic(CheetahString::from("topic2"));
81        assert_eq!(header.get_topic(), "topic2");
82    }
83}