Skip to main content

rocketmq_protocol/protocol/static_topic/
topic_config_and_queue_mapping.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 rocketmq_model::topic::TopicConfig;
16use serde::Deserialize;
17use serde::Serialize;
18
19use crate::protocol::static_topic::topic_queue_mapping_detail::TopicQueueMappingDetail;
20
21#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
22pub struct TopicConfigAndQueueMapping {
23    #[serde(flatten)]
24    pub topic_config: TopicConfig,
25
26    #[serde(rename = "mappingDetail")]
27    pub topic_queue_mapping_detail: Option<TopicQueueMappingDetail>,
28}
29
30impl TopicConfigAndQueueMapping {
31    pub fn new(topic_config: TopicConfig, topic_queue_mapping_detail: Option<TopicQueueMappingDetail>) -> Self {
32        Self {
33            topic_config,
34            topic_queue_mapping_detail,
35        }
36    }
37
38    pub fn get_topic_queue_mapping_detail(&self) -> Option<&TopicQueueMappingDetail> {
39        self.topic_queue_mapping_detail.as_ref()
40    }
41
42    pub fn set_topic_queue_mapping_detail(&mut self, topic_queue_mapping_detail: Option<TopicQueueMappingDetail>) {
43        self.topic_queue_mapping_detail = topic_queue_mapping_detail;
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use crate::protocol::static_topic::topic_queue_mapping_info::TopicQueueMappingInfo;
51
52    #[test]
53    fn topic_config_and_queue_mapping_default() {
54        let mapping = TopicConfigAndQueueMapping::default();
55        assert_eq!(mapping.topic_config, TopicConfig::default());
56        assert!(mapping.topic_queue_mapping_detail.is_none());
57    }
58
59    #[test]
60    fn topic_config_and_queue_mapping_serde() {
61        let topic_config = TopicConfig::new("test_topic");
62        let mapping_detail = TopicQueueMappingDetail {
63            topic_queue_mapping_info: TopicQueueMappingInfo {
64                topic: Some("test_topic".into()),
65                ..TopicQueueMappingInfo::default()
66            },
67            ..Default::default()
68        };
69        let mapping = TopicConfigAndQueueMapping::new(topic_config, Some(mapping_detail));
70
71        let json = serde_json::to_string(&mapping).unwrap();
72        let expected = r#"{"topicName":"test_topic","readQueueNums":16,"writeQueueNums":16,"perm":6,"topicFilterType":"SINGLE_TAG","topicSysFlag":0,"order":false,"attributes":{},"mappingDetail":{"topic":"test_topic","scope":"__global__","totalQueues":0,"bname":null,"epoch":0,"dirty":false,"currIdMap":null,"hostedQueues":{}}}"#;
73        assert_eq!(json, expected);
74
75        let deserialized: TopicConfigAndQueueMapping = serde_json::from_str(&json).unwrap();
76        assert_eq!(mapping.topic_config.topic_name, deserialized.topic_config.topic_name);
77        assert_eq!(
78            mapping
79                .topic_queue_mapping_detail
80                .as_ref()
81                .unwrap()
82                .topic_queue_mapping_info
83                .topic,
84            deserialized
85                .topic_queue_mapping_detail
86                .as_ref()
87                .unwrap()
88                .topic_queue_mapping_info
89                .topic
90        );
91    }
92
93    #[test]
94    fn topic_config_and_queue_mapping_new() {
95        let topic_config = TopicConfig::new("test_topic");
96        let mapping_detail = TopicQueueMappingDetail::default();
97        let mapping = TopicConfigAndQueueMapping::new(topic_config.clone(), Some(mapping_detail.clone()));
98        assert_eq!(mapping.topic_config, topic_config);
99        assert_eq!(mapping.topic_queue_mapping_detail.as_ref().unwrap(), &mapping_detail);
100    }
101
102    #[test]
103    fn get_set_topic_queue_mapping_detail() {
104        let mut mapping = TopicConfigAndQueueMapping::default();
105        let mapping_detail = TopicQueueMappingDetail::default();
106        mapping.set_topic_queue_mapping_detail(Some(mapping_detail.clone()));
107        assert_eq!(mapping.get_topic_queue_mapping_detail().unwrap(), &mapping_detail);
108
109        mapping.set_topic_queue_mapping_detail(None);
110        assert!(mapping.get_topic_queue_mapping_detail().is_none());
111    }
112}