rocketmq_remoting/protocol/header/
create_topic_request_header.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use cheetah_string::CheetahString;
18use rocketmq_macros::RequestHeaderCodecV2;
19use serde::Deserialize;
20use serde::Serialize;
21
22use crate::rpc::topic_request_header::TopicRequestHeader;
23
24#[derive(Serialize, Deserialize, Debug, RequestHeaderCodecV2)]
25pub struct CreateTopicRequestHeader {
26    #[required]
27    #[serde(rename = "topic")]
28    pub topic: CheetahString,
29
30    #[required]
31    #[serde(rename = "defaultTopic")]
32    pub default_topic: CheetahString,
33
34    #[required]
35    #[serde(rename = "readQueueNums")]
36    pub read_queue_nums: i32,
37
38    #[required]
39    #[serde(rename = "writeQueueNums")]
40    pub write_queue_nums: i32,
41
42    #[required]
43    #[serde(rename = "perm")]
44    pub perm: i32,
45
46    #[required]
47    #[serde(rename = "topicFilterType")]
48    pub topic_filter_type: CheetahString,
49
50    #[serde(rename = "topicSysFlag")]
51    pub topic_sys_flag: Option<i32>,
52
53    #[required]
54    #[serde(rename = "order")]
55    pub order: bool,
56
57    #[serde(rename = "attributes")]
58    pub attributes: Option<CheetahString>,
59
60    #[serde(rename = "force")]
61    pub force: Option<bool>,
62
63    #[serde(flatten)]
64    pub topic_request_header: Option<TopicRequestHeader>,
65}
66
67#[cfg(test)]
68mod tests {
69    use std::collections::HashMap;
70
71    use super::*;
72    use crate::protocol::command_custom_header::CommandCustomHeader;
73    use crate::protocol::command_custom_header::FromMap;
74
75    #[test]
76    fn create_topic_request_header_to_map() {
77        let header = CreateTopicRequestHeader {
78            topic: CheetahString::from("test_topic"),
79            default_topic: CheetahString::from("default_topic"),
80            read_queue_nums: 4,
81            write_queue_nums: 4,
82            perm: 6,
83            topic_filter_type: CheetahString::from("filter_type"),
84            topic_sys_flag: Some(1),
85            order: true,
86            attributes: Some(CheetahString::from("attributes")),
87            force: Some(true),
88            topic_request_header: None,
89        };
90
91        let map = header.to_map().unwrap();
92        assert_eq!(
93            map.get(&CheetahString::from_static_str(
94                CreateTopicRequestHeader::TOPIC
95            ))
96            .unwrap(),
97            &CheetahString::from("test_topic")
98        );
99        assert_eq!(
100            map.get(&CheetahString::from_static_str(
101                CreateTopicRequestHeader::DEFAULT_TOPIC
102            ))
103            .unwrap(),
104            &CheetahString::from("default_topic")
105        );
106        assert_eq!(
107            map.get(&CheetahString::from_static_str(
108                CreateTopicRequestHeader::READ_QUEUE_NUMS
109            ))
110            .unwrap(),
111            &CheetahString::from("4")
112        );
113        assert_eq!(
114            map.get(&CheetahString::from_static_str(
115                CreateTopicRequestHeader::WRITE_QUEUE_NUMS
116            ))
117            .unwrap(),
118            &CheetahString::from("4")
119        );
120        assert_eq!(
121            map.get(&CheetahString::from_static_str(
122                CreateTopicRequestHeader::PERM
123            ))
124            .unwrap(),
125            &CheetahString::from("6")
126        );
127        assert_eq!(
128            map.get(&CheetahString::from_static_str(
129                CreateTopicRequestHeader::TOPIC_FILTER_TYPE
130            ))
131            .unwrap(),
132            &CheetahString::from("filter_type")
133        );
134        assert_eq!(
135            map.get(&CheetahString::from_static_str(
136                CreateTopicRequestHeader::TOPIC_SYS_FLAG
137            ))
138            .unwrap(),
139            &CheetahString::from("1")
140        );
141        assert_eq!(
142            map.get(&CheetahString::from_static_str(
143                CreateTopicRequestHeader::ORDER
144            ))
145            .unwrap(),
146            &CheetahString::from("true")
147        );
148        assert_eq!(
149            map.get(&CheetahString::from_static_str(
150                CreateTopicRequestHeader::ATTRIBUTES
151            ))
152            .unwrap(),
153            &CheetahString::from("attributes")
154        );
155        assert_eq!(
156            map.get(&CheetahString::from_static_str(
157                CreateTopicRequestHeader::FORCE
158            ))
159            .unwrap(),
160            &CheetahString::from("true")
161        );
162    }
163
164    #[test]
165    fn create_topic_request_header_from_map() {
166        let mut map = HashMap::new();
167        map.insert(
168            CheetahString::from_static_str(CreateTopicRequestHeader::TOPIC),
169            CheetahString::from("test_topic"),
170        );
171        map.insert(
172            CheetahString::from_static_str(CreateTopicRequestHeader::DEFAULT_TOPIC),
173            CheetahString::from("default_topic"),
174        );
175        map.insert(
176            CheetahString::from_static_str(CreateTopicRequestHeader::READ_QUEUE_NUMS),
177            CheetahString::from("4"),
178        );
179        map.insert(
180            CheetahString::from_static_str(CreateTopicRequestHeader::WRITE_QUEUE_NUMS),
181            CheetahString::from("4"),
182        );
183        map.insert(
184            CheetahString::from_static_str(CreateTopicRequestHeader::PERM),
185            CheetahString::from("6"),
186        );
187        map.insert(
188            CheetahString::from_static_str(CreateTopicRequestHeader::TOPIC_FILTER_TYPE),
189            CheetahString::from("filter_type"),
190        );
191        map.insert(
192            CheetahString::from_static_str(CreateTopicRequestHeader::TOPIC_SYS_FLAG),
193            CheetahString::from("1"),
194        );
195        map.insert(
196            CheetahString::from_static_str(CreateTopicRequestHeader::ORDER),
197            CheetahString::from("true"),
198        );
199        map.insert(
200            CheetahString::from_static_str(CreateTopicRequestHeader::ATTRIBUTES),
201            CheetahString::from("attributes"),
202        );
203        map.insert(
204            CheetahString::from_static_str(CreateTopicRequestHeader::FORCE),
205            CheetahString::from("true"),
206        );
207
208        let header = <CreateTopicRequestHeader as FromMap>::from(&map).unwrap();
209        assert_eq!(header.topic, CheetahString::from("test_topic"));
210        assert_eq!(header.default_topic, CheetahString::from("default_topic"));
211        assert_eq!(header.read_queue_nums, 4);
212        assert_eq!(header.write_queue_nums, 4);
213        assert_eq!(header.perm, 6);
214        assert_eq!(header.topic_filter_type, CheetahString::from("filter_type"));
215        assert_eq!(header.topic_sys_flag, Some(1));
216        assert!(header.order);
217        assert_eq!(header.attributes, Some(CheetahString::from("attributes")));
218        assert_eq!(header.force, Some(true));
219    }
220
221    #[test]
222    fn create_topic_request_header_from_map_missing_optional_fields() {
223        let mut map = HashMap::new();
224        map.insert(
225            CheetahString::from_static_str(CreateTopicRequestHeader::TOPIC),
226            CheetahString::from("test_topic"),
227        );
228        map.insert(
229            CheetahString::from_static_str(CreateTopicRequestHeader::DEFAULT_TOPIC),
230            CheetahString::from("default_topic"),
231        );
232        map.insert(
233            CheetahString::from_static_str(CreateTopicRequestHeader::READ_QUEUE_NUMS),
234            CheetahString::from("4"),
235        );
236        map.insert(
237            CheetahString::from_static_str(CreateTopicRequestHeader::WRITE_QUEUE_NUMS),
238            CheetahString::from("4"),
239        );
240        map.insert(
241            CheetahString::from_static_str(CreateTopicRequestHeader::PERM),
242            CheetahString::from("6"),
243        );
244        map.insert(
245            CheetahString::from_static_str(CreateTopicRequestHeader::TOPIC_FILTER_TYPE),
246            CheetahString::from("filter_type"),
247        );
248        map.insert(
249            CheetahString::from_static_str(CreateTopicRequestHeader::ORDER),
250            CheetahString::from("true"),
251        );
252
253        let header = <CreateTopicRequestHeader as FromMap>::from(&map).unwrap();
254        assert_eq!(header.topic, CheetahString::from("test_topic"));
255        assert_eq!(header.default_topic, CheetahString::from("default_topic"));
256        assert_eq!(header.read_queue_nums, 4);
257        assert_eq!(header.write_queue_nums, 4);
258        assert_eq!(header.perm, 6);
259        assert_eq!(header.topic_filter_type, CheetahString::from("filter_type"));
260        assert_eq!(header.topic_sys_flag, None);
261        assert!(header.order);
262        assert_eq!(header.attributes, None);
263        assert_eq!(header.force, None);
264    }
265}