Skip to main content

rocketmq_protocol/protocol/header/
get_consume_stats_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::rpc::topic_request_header::TopicRequestHeader;
21
22#[derive(Debug, Serialize, Deserialize, RequestHeaderCodec)]
23#[header(
24    type_id = "rocketmq_protocol::protocol::header::get_consume_stats_request_header::GetConsumeStatsRequestHeader",
25    java_class = "org.apache.rocketmq.remoting.protocol.header.GetConsumeStatsRequestHeader"
26)]
27pub struct GetConsumeStatsRequestHeader {
28    #[serde(rename = "consumerGroup")]
29    #[header(required)]
30    pub consumer_group: CheetahString,
31    #[serde(default)]
32    #[serde(rename = "topic")]
33    #[header(default, default_semantic = "literal:")]
34    pub topic: CheetahString,
35    #[serde(rename = "topicList")]
36    pub topic_list: Option<CheetahString>,
37    #[serde(flatten)]
38    #[header(flatten, presence = "always")]
39    pub topic_request_header: Option<TopicRequestHeader>,
40}
41
42impl GetConsumeStatsRequestHeader {
43    pub fn get_consumer_group(&self) -> &CheetahString {
44        &self.consumer_group
45    }
46    pub fn set_consumer_group(&mut self, consumer_group: CheetahString) {
47        self.consumer_group = consumer_group;
48    }
49
50    pub fn get_topic(&self) -> &CheetahString {
51        &self.topic
52    }
53    pub fn set_topic(&mut self, topic: CheetahString) {
54        self.topic = topic;
55    }
56
57    pub fn fetch_topic_list(&self) -> Vec<CheetahString> {
58        self.topic_list
59            .as_deref()
60            .map(|topics| {
61                topics
62                    .split(';')
63                    .filter(|topic| !topic.is_empty())
64                    .map(CheetahString::from)
65                    .collect()
66            })
67            .unwrap_or_default()
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn get_consume_stats_request_header_deserialize_with_extra_fields() {
77        let json = r#"
78        {
79            "consumerGroup": "testGroup",
80            "topic": "testTopic",
81            "extraField1": "extraValue1",
82            "extraField2": "extraValue2"
83        }
84        "#;
85
86        let deserialized: GetConsumeStatsRequestHeader = serde_json::from_str(json).unwrap();
87        assert_eq!(deserialized.get_consumer_group(), "testGroup");
88        assert_eq!(deserialized.get_topic(), "testTopic");
89    }
90
91    #[test]
92    fn get_consume_stats_request_header_with_topic_request_header_some() {
93        let topic_header = TopicRequestHeader::default();
94
95        let header = GetConsumeStatsRequestHeader {
96            consumer_group: CheetahString::from("testGroup"),
97            topic: CheetahString::from("testTopic"),
98            topic_list: None,
99            topic_request_header: Some(topic_header),
100        };
101
102        let json = serde_json::to_string(&header).unwrap();
103        println!("Serialized JSON with topic_request_header: {}", json);
104
105        let deserialized: GetConsumeStatsRequestHeader = serde_json::from_str(&json).unwrap();
106        assert_eq!(deserialized.get_consumer_group(), "testGroup");
107        assert_eq!(deserialized.get_topic(), "testTopic");
108        assert!(deserialized.topic_request_header.is_some());
109    }
110}