Skip to main content

rocketmq_protocol/protocol/header/
get_topic_stats_info_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
20use crate::protocol::header::message_operation_header::TopicRequestHeaderTrait;
21use crate::protocol::header::namesrv::topic_operation_header::TopicRequestHeader;
22use crate::rpc::rpc_request_header::RpcRequestHeader;
23
24#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodec)]
25#[header(
26    type_id = "rocketmq_protocol::protocol::header::get_topic_stats_info_request_header::GetTopicStatsInfoRequestHeader",
27    java_class = "org.apache.rocketmq.remoting.protocol.header.GetTopicStatsInfoRequestHeader"
28)]
29#[serde(rename_all = "camelCase")]
30pub struct GetTopicStatsInfoRequestHeader {
31    #[header(required)]
32    pub topic: CheetahString,
33
34    #[serde(flatten)]
35    #[header(flatten, presence = "always")]
36    pub topic_request_header: Option<TopicRequestHeader>,
37}
38
39impl TopicRequestHeaderTrait for GetTopicStatsInfoRequestHeader {
40    fn set_lo(&mut self, lo: Option<bool>) {
41        self.topic_request_header
42            .get_or_insert_with(TopicRequestHeader::default)
43            .lo = lo;
44    }
45
46    fn lo(&self) -> Option<bool> {
47        self.topic_request_header.as_ref().and_then(|header| header.lo)
48    }
49
50    fn set_topic(&mut self, topic: CheetahString) {
51        self.topic = topic;
52    }
53
54    fn topic(&self) -> &CheetahString {
55        &self.topic
56    }
57
58    fn broker_name(&self) -> Option<&CheetahString> {
59        self.topic_request_header
60            .as_ref()
61            .and_then(|header| header.rpc.as_ref())
62            .and_then(|rpc| rpc.broker_name.as_ref())
63    }
64
65    fn set_broker_name(&mut self, broker_name: CheetahString) {
66        self.topic_request_header
67            .get_or_insert_with(TopicRequestHeader::default)
68            .rpc
69            .get_or_insert_with(RpcRequestHeader::default)
70            .broker_name = Some(broker_name);
71    }
72
73    fn namespace(&self) -> Option<&str> {
74        self.topic_request_header
75            .as_ref()
76            .and_then(|header| header.rpc.as_ref())
77            .and_then(|rpc| rpc.namespace.as_deref())
78    }
79
80    fn set_namespace(&mut self, namespace: CheetahString) {
81        self.topic_request_header
82            .get_or_insert_with(TopicRequestHeader::default)
83            .rpc
84            .get_or_insert_with(RpcRequestHeader::default)
85            .namespace = Some(namespace);
86    }
87
88    fn namespaced(&self) -> Option<bool> {
89        self.topic_request_header
90            .as_ref()
91            .and_then(|header| header.rpc.as_ref())
92            .and_then(|rpc| rpc.namespaced)
93    }
94
95    fn set_namespaced(&mut self, namespaced: bool) {
96        self.topic_request_header
97            .get_or_insert_with(TopicRequestHeader::default)
98            .rpc
99            .get_or_insert_with(RpcRequestHeader::default)
100            .namespaced = Some(namespaced);
101    }
102
103    fn oneway(&self) -> Option<bool> {
104        self.topic_request_header
105            .as_ref()
106            .and_then(|header| header.rpc.as_ref())
107            .and_then(|rpc| rpc.oneway)
108    }
109
110    fn set_oneway(&mut self, oneway: bool) {
111        self.topic_request_header
112            .get_or_insert_with(TopicRequestHeader::default)
113            .rpc
114            .get_or_insert_with(RpcRequestHeader::default)
115            .oneway = Some(oneway);
116    }
117
118    fn queue_id(&self) -> i32 {
119        -1
120    }
121
122    fn set_queue_id(&mut self, _queue_id: i32) {}
123}
124
125#[cfg(test)]
126mod tests {
127    use cheetah_string::CheetahString;
128
129    use super::*;
130
131    #[test]
132    fn get_topic_stats_info_request_header_serializes_correctly() {
133        let header = GetTopicStatsInfoRequestHeader {
134            topic: CheetahString::from_static_str("test_topic"),
135            topic_request_header: None,
136        };
137        let serialized = serde_json::to_string(&header).unwrap();
138        let expected = r#"{"topic":"test_topic"}"#;
139        assert_eq!(serialized, expected);
140    }
141
142    #[test]
143    fn get_topic_stats_info_request_header_deserializes_correctly() {
144        let data = r#"{"topic":"test_topic"}"#;
145        let header: GetTopicStatsInfoRequestHeader = serde_json::from_str(data).unwrap();
146        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
147        assert!(header.topic_request_header.is_some());
148    }
149
150    #[test]
151    fn get_topic_stats_info_request_header_handles_missing_optional_fields() {
152        let data = r#"{"topic":"test_topic"}"#;
153        let header: GetTopicStatsInfoRequestHeader = serde_json::from_str(data).unwrap();
154        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
155        assert!(header.topic_request_header.is_some());
156    }
157
158    #[test]
159    fn get_topic_stats_info_request_header_handles_invalid_data() {
160        let data = r#"{"topic":12345}"#;
161        let result: Result<GetTopicStatsInfoRequestHeader, _> = serde_json::from_str(data);
162        assert!(result.is_err());
163    }
164}