Skip to main content

rocketmq_remoting/protocol/header/
create_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::RequestHeaderCodecV2;
17use serde::Deserialize;
18use serde::Serialize;
19
20use crate::rpc::topic_request_header::TopicRequestHeader;
21
22#[derive(Serialize, Deserialize, Debug, RequestHeaderCodecV2)]
23pub struct CreateTopicRequestHeader {
24    #[required]
25    #[serde(rename = "topic")]
26    pub topic: CheetahString,
27
28    #[required]
29    #[serde(rename = "defaultTopic")]
30    pub default_topic: CheetahString,
31
32    #[required]
33    #[serde(rename = "readQueueNums")]
34    pub read_queue_nums: i32,
35
36    #[required]
37    #[serde(rename = "writeQueueNums")]
38    pub write_queue_nums: i32,
39
40    #[required]
41    #[serde(rename = "perm")]
42    pub perm: i32,
43
44    #[required]
45    #[serde(rename = "topicFilterType")]
46    pub topic_filter_type: CheetahString,
47
48    #[serde(rename = "topicSysFlag")]
49    pub topic_sys_flag: Option<i32>,
50
51    #[required]
52    #[serde(rename = "order")]
53    pub order: bool,
54
55    #[serde(rename = "attributes")]
56    pub attributes: Option<CheetahString>,
57
58    #[serde(rename = "force")]
59    pub force: Option<bool>,
60
61    #[serde(flatten)]
62    pub topic_request_header: Option<TopicRequestHeader>,
63}
64
65#[cfg(test)]
66mod tests {
67    use std::collections::HashMap;
68
69    use super::*;
70    use crate::protocol::command_custom_header::CommandCustomHeader;
71    use crate::protocol::command_custom_header::FromMap;
72
73    #[test]
74    fn create_topic_request_header_to_map() {
75        let header = CreateTopicRequestHeader {
76            topic: CheetahString::from("test_topic"),
77            default_topic: CheetahString::from("default_topic"),
78            read_queue_nums: 4,
79            write_queue_nums: 4,
80            perm: 6,
81            topic_filter_type: CheetahString::from("filter_type"),
82            topic_sys_flag: Some(1),
83            order: true,
84            attributes: Some(CheetahString::from("attributes")),
85            force: Some(true),
86            topic_request_header: None,
87        };
88
89        let map = header.to_map().unwrap();
90        assert_eq!(
91            map.get(&CheetahString::from_static_str(CreateTopicRequestHeader::TOPIC))
92                .unwrap(),
93            &CheetahString::from("test_topic")
94        );
95        assert_eq!(
96            map.get(&CheetahString::from_static_str(CreateTopicRequestHeader::DEFAULT_TOPIC))
97                .unwrap(),
98            &CheetahString::from("default_topic")
99        );
100        assert_eq!(
101            map.get(&CheetahString::from_static_str(
102                CreateTopicRequestHeader::READ_QUEUE_NUMS
103            ))
104            .unwrap(),
105            &CheetahString::from("4")
106        );
107        assert_eq!(
108            map.get(&CheetahString::from_static_str(
109                CreateTopicRequestHeader::WRITE_QUEUE_NUMS
110            ))
111            .unwrap(),
112            &CheetahString::from("4")
113        );
114        assert_eq!(
115            map.get(&CheetahString::from_static_str(CreateTopicRequestHeader::PERM))
116                .unwrap(),
117            &CheetahString::from("6")
118        );
119        assert_eq!(
120            map.get(&CheetahString::from_static_str(
121                CreateTopicRequestHeader::TOPIC_FILTER_TYPE
122            ))
123            .unwrap(),
124            &CheetahString::from("filter_type")
125        );
126        assert_eq!(
127            map.get(&CheetahString::from_static_str(
128                CreateTopicRequestHeader::TOPIC_SYS_FLAG
129            ))
130            .unwrap(),
131            &CheetahString::from("1")
132        );
133        assert_eq!(
134            map.get(&CheetahString::from_static_str(CreateTopicRequestHeader::ORDER))
135                .unwrap(),
136            &CheetahString::from("true")
137        );
138        assert_eq!(
139            map.get(&CheetahString::from_static_str(CreateTopicRequestHeader::ATTRIBUTES))
140                .unwrap(),
141            &CheetahString::from("attributes")
142        );
143        assert_eq!(
144            map.get(&CheetahString::from_static_str(CreateTopicRequestHeader::FORCE))
145                .unwrap(),
146            &CheetahString::from("true")
147        );
148    }
149
150    #[test]
151    fn create_topic_request_header_from_map() {
152        let mut map = HashMap::new();
153        map.insert(
154            CheetahString::from_static_str(CreateTopicRequestHeader::TOPIC),
155            CheetahString::from("test_topic"),
156        );
157        map.insert(
158            CheetahString::from_static_str(CreateTopicRequestHeader::DEFAULT_TOPIC),
159            CheetahString::from("default_topic"),
160        );
161        map.insert(
162            CheetahString::from_static_str(CreateTopicRequestHeader::READ_QUEUE_NUMS),
163            CheetahString::from("4"),
164        );
165        map.insert(
166            CheetahString::from_static_str(CreateTopicRequestHeader::WRITE_QUEUE_NUMS),
167            CheetahString::from("4"),
168        );
169        map.insert(
170            CheetahString::from_static_str(CreateTopicRequestHeader::PERM),
171            CheetahString::from("6"),
172        );
173        map.insert(
174            CheetahString::from_static_str(CreateTopicRequestHeader::TOPIC_FILTER_TYPE),
175            CheetahString::from("filter_type"),
176        );
177        map.insert(
178            CheetahString::from_static_str(CreateTopicRequestHeader::TOPIC_SYS_FLAG),
179            CheetahString::from("1"),
180        );
181        map.insert(
182            CheetahString::from_static_str(CreateTopicRequestHeader::ORDER),
183            CheetahString::from("true"),
184        );
185        map.insert(
186            CheetahString::from_static_str(CreateTopicRequestHeader::ATTRIBUTES),
187            CheetahString::from("attributes"),
188        );
189        map.insert(
190            CheetahString::from_static_str(CreateTopicRequestHeader::FORCE),
191            CheetahString::from("true"),
192        );
193
194        let header = <CreateTopicRequestHeader as FromMap>::from(&map).unwrap();
195        assert_eq!(header.topic, CheetahString::from("test_topic"));
196        assert_eq!(header.default_topic, CheetahString::from("default_topic"));
197        assert_eq!(header.read_queue_nums, 4);
198        assert_eq!(header.write_queue_nums, 4);
199        assert_eq!(header.perm, 6);
200        assert_eq!(header.topic_filter_type, CheetahString::from("filter_type"));
201        assert_eq!(header.topic_sys_flag, Some(1));
202        assert!(header.order);
203        assert_eq!(header.attributes, Some(CheetahString::from("attributes")));
204        assert_eq!(header.force, Some(true));
205    }
206
207    #[test]
208    fn create_topic_request_header_from_map_missing_optional_fields() {
209        let mut map = HashMap::new();
210        map.insert(
211            CheetahString::from_static_str(CreateTopicRequestHeader::TOPIC),
212            CheetahString::from("test_topic"),
213        );
214        map.insert(
215            CheetahString::from_static_str(CreateTopicRequestHeader::DEFAULT_TOPIC),
216            CheetahString::from("default_topic"),
217        );
218        map.insert(
219            CheetahString::from_static_str(CreateTopicRequestHeader::READ_QUEUE_NUMS),
220            CheetahString::from("4"),
221        );
222        map.insert(
223            CheetahString::from_static_str(CreateTopicRequestHeader::WRITE_QUEUE_NUMS),
224            CheetahString::from("4"),
225        );
226        map.insert(
227            CheetahString::from_static_str(CreateTopicRequestHeader::PERM),
228            CheetahString::from("6"),
229        );
230        map.insert(
231            CheetahString::from_static_str(CreateTopicRequestHeader::TOPIC_FILTER_TYPE),
232            CheetahString::from("filter_type"),
233        );
234        map.insert(
235            CheetahString::from_static_str(CreateTopicRequestHeader::ORDER),
236            CheetahString::from("true"),
237        );
238
239        let header = <CreateTopicRequestHeader as FromMap>::from(&map).unwrap();
240        assert_eq!(header.topic, CheetahString::from("test_topic"));
241        assert_eq!(header.default_topic, CheetahString::from("default_topic"));
242        assert_eq!(header.read_queue_nums, 4);
243        assert_eq!(header.write_queue_nums, 4);
244        assert_eq!(header.perm, 6);
245        assert_eq!(header.topic_filter_type, CheetahString::from("filter_type"));
246        assert_eq!(header.topic_sys_flag, None);
247        assert!(header.order);
248        assert_eq!(header.attributes, None);
249        assert_eq!(header.force, None);
250    }
251}