Skip to main content

rocketmq_protocol/protocol/header/
get_all_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::RequestHeaderCodec;
17use serde::Deserialize;
18use serde::Serialize;
19
20#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodec)]
21#[serde(rename_all = "camelCase")]
22#[header(
23    type_id = "rocketmq_protocol::protocol::header::get_all_topic_config_request_header::GetAllTopicConfigRequestHeader",
24    java_class = "org.apache.rocketmq.remoting.protocol.header.GetAllTopicConfigRequestHeader"
25)]
26pub struct GetAllTopicConfigRequestHeader {
27    #[header(required)]
28    pub topic_seq: i32,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub data_version: Option<CheetahString>,
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub max_topic_num: Option<i32>,
33}
34
35#[cfg(test)]
36mod tests {
37    use std::collections::HashMap;
38
39    use super::*;
40    use crate::protocol::command_custom_header::CommandCustomHeader;
41    use crate::protocol::command_custom_header::FromMap;
42
43    #[test]
44    fn get_all_topic_config_request_header_maps_java_fields() {
45        let header = GetAllTopicConfigRequestHeader {
46            topic_seq: 11,
47            data_version: Some(CheetahString::from_static_str("dv-topic")),
48            max_topic_num: Some(64),
49        };
50
51        let map = header.to_map().expect("header should encode");
52        assert_eq!(map.get("topicSeq").map(|value| value.as_str()), Some("11"));
53        assert_eq!(map.get("dataVersion").map(|value| value.as_str()), Some("dv-topic"));
54        assert_eq!(map.get("maxTopicNum").map(|value| value.as_str()), Some("64"));
55
56        let decoded = <GetAllTopicConfigRequestHeader as FromMap>::from(&map).expect("header should decode");
57        assert_eq!(decoded.topic_seq, 11);
58        assert_eq!(decoded.data_version.as_deref(), Some("dv-topic"));
59        assert_eq!(decoded.max_topic_num, Some(64));
60    }
61
62    #[test]
63    fn get_all_topic_config_request_header_requires_topic_seq_like_java_header() {
64        let result = <GetAllTopicConfigRequestHeader as FromMap>::from(&HashMap::new());
65
66        assert!(result.is_err());
67    }
68}