rocketmq_remoting/protocol/header/namesrv/
topic_operation_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 */
17
18use cheetah_string::CheetahString;
19use rocketmq_macros::RequestHeaderCodecV2;
20use serde::Deserialize;
21use serde::Serialize;
22
23use crate::rpc::rpc_request_header::RpcRequestHeader;
24
25#[derive(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodecV2)]
26#[serde(rename_all = "camelCase")]
27pub struct DeleteTopicFromNamesrvRequestHeader {
28    #[required]
29    pub topic: CheetahString,
30    pub cluster_name: Option<CheetahString>,
31}
32
33impl DeleteTopicFromNamesrvRequestHeader {
34    pub fn new(
35        topic: impl Into<CheetahString>,
36        cluster_name: Option<impl Into<CheetahString>>,
37    ) -> Self {
38        Self {
39            topic: topic.into(),
40            cluster_name: cluster_name.map(|s| s.into()),
41        }
42    }
43}
44
45#[derive(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodecV2)]
46#[serde(rename_all = "camelCase")]
47pub struct RegisterTopicRequestHeader {
48    #[required]
49    pub topic: CheetahString,
50    #[serde(flatten)]
51    pub topic_request: Option<TopicRequestHeader>,
52}
53
54impl RegisterTopicRequestHeader {
55    pub fn new(topic: impl Into<CheetahString>) -> Self {
56        Self {
57            topic: topic.into(),
58            topic_request: None,
59        }
60    }
61}
62
63#[derive(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodecV2)]
64pub struct GetTopicsByClusterRequestHeader {
65    #[required]
66    pub cluster: CheetahString,
67}
68
69impl GetTopicsByClusterRequestHeader {
70    pub fn new(cluster: impl Into<CheetahString>) -> Self {
71        Self {
72            cluster: cluster.into(),
73        }
74    }
75}
76
77#[derive(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodecV2)]
78#[serde(rename_all = "camelCase")]
79pub struct TopicRequestHeader {
80    pub lo: Option<bool>,
81    #[serde(flatten)]
82    pub rpc: Option<RpcRequestHeader>,
83}
84
85#[cfg(test)]
86mod tests {
87    use cheetah_string::CheetahString;
88
89    use super::*;
90
91    #[test]
92    fn delete_topic_from_namesrv_request_header_new() {
93        let header = DeleteTopicFromNamesrvRequestHeader::new("topic1", Some("cluster1"));
94        assert_eq!(header.topic, CheetahString::from("topic1"));
95        assert_eq!(header.cluster_name, Some(CheetahString::from("cluster1")));
96    }
97
98    #[test]
99    fn delete_topic_from_namesrv_request_header_serialization() {
100        let header = DeleteTopicFromNamesrvRequestHeader::new("topic1", Some("cluster1"));
101        let serialized = serde_json::to_string(&header).unwrap();
102        assert_eq!(serialized, r#"{"topic":"topic1","clusterName":"cluster1"}"#);
103    }
104
105    #[test]
106    fn delete_topic_from_namesrv_request_header_deserialization() {
107        let json = r#"{"topic":"topic1","clusterName":"cluster1"}"#;
108        let deserialized: DeleteTopicFromNamesrvRequestHeader = serde_json::from_str(json).unwrap();
109        assert_eq!(deserialized.topic, CheetahString::from("topic1"));
110        assert_eq!(
111            deserialized.cluster_name,
112            Some(CheetahString::from("cluster1"))
113        );
114    }
115
116    #[test]
117    fn register_topic_request_header_new() {
118        let header = RegisterTopicRequestHeader::new("topic1");
119        assert_eq!(header.topic, CheetahString::from("topic1"));
120        assert!(header.topic_request.is_none());
121    }
122
123    #[test]
124    fn register_topic_request_header_serialization() {
125        let header = RegisterTopicRequestHeader::new("topic1");
126        let serialized = serde_json::to_string(&header).unwrap();
127        assert_eq!(serialized, r#"{"topic":"topic1"}"#);
128    }
129
130    #[test]
131    fn get_topics_by_cluster_request_header_new() {
132        let header = GetTopicsByClusterRequestHeader::new("cluster1");
133        assert_eq!(header.cluster, CheetahString::from("cluster1"));
134    }
135
136    #[test]
137    fn get_topics_by_cluster_request_header_serialization() {
138        let header = GetTopicsByClusterRequestHeader::new("cluster1");
139        let serialized = serde_json::to_string(&header).unwrap();
140        assert_eq!(serialized, r#"{"cluster":"cluster1"}"#);
141    }
142
143    #[test]
144    fn get_topics_by_cluster_request_header_deserialization() {
145        let json = r#"{"cluster":"cluster1"}"#;
146        let deserialized: GetTopicsByClusterRequestHeader = serde_json::from_str(json).unwrap();
147        assert_eq!(deserialized.cluster, CheetahString::from("cluster1"));
148    }
149}