Skip to main content

rocketmq_remoting/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::RequestHeaderCodecV2;
17use serde::Deserialize;
18use serde::Serialize;
19
20use crate::rpc::topic_request_header::TopicRequestHeader;
21
22#[derive(Debug, Serialize, Deserialize, RequestHeaderCodecV2)]
23pub struct GetConsumeStatsRequestHeader {
24    #[serde(rename = "consumerGroup")]
25    pub consumer_group: CheetahString,
26    #[serde(rename = "topic")]
27    pub topic: CheetahString,
28    #[serde(flatten)]
29    pub topic_request_header: Option<TopicRequestHeader>,
30}
31
32impl GetConsumeStatsRequestHeader {
33    pub fn get_consumer_group(&self) -> &CheetahString {
34        &self.consumer_group
35    }
36    pub fn set_consumer_group(&mut self, consumer_group: CheetahString) {
37        self.consumer_group = consumer_group;
38    }
39
40    pub fn get_topic(&self) -> &CheetahString {
41        &self.topic
42    }
43    pub fn set_topic(&mut self, topic: CheetahString) {
44        self.topic = topic;
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn getters_and_setters() {
54        let mut header = GetConsumeStatsRequestHeader {
55            consumer_group: CheetahString::from("testGroup"),
56            topic: CheetahString::from("testTopic"),
57            topic_request_header: None,
58        };
59
60        assert_eq!(header.get_consumer_group(), "testGroup");
61        assert_eq!(header.get_topic(), "testTopic");
62
63        header.set_consumer_group(CheetahString::from("newGroup"));
64        header.set_topic(CheetahString::from("newTopic"));
65
66        assert_eq!(header.get_consumer_group(), "newGroup");
67        assert_eq!(header.get_topic(), "newTopic");
68    }
69    #[test]
70    fn get_consume_stats_request_header_serde() {
71        let header = GetConsumeStatsRequestHeader {
72            consumer_group: CheetahString::from("testGroup"),
73            topic: CheetahString::from("testTopic"),
74            topic_request_header: None,
75        };
76
77        let json = serde_json::to_string(&header).unwrap();
78
79        let deserialized: GetConsumeStatsRequestHeader = serde_json::from_str(&json).unwrap();
80        assert_eq!(deserialized.get_consumer_group(), "testGroup");
81        assert_eq!(deserialized.get_topic(), "testTopic");
82    }
83
84    #[test]
85    fn get_consume_stats_request_header_deserialize_with_extra_fields() {
86        let json = r#"
87        {
88            "consumerGroup": "testGroup",
89            "topic": "testTopic",
90            "extraField1": "extraValue1",
91            "extraField2": "extraValue2"
92        }
93        "#;
94
95        let deserialized: GetConsumeStatsRequestHeader = serde_json::from_str(json).unwrap();
96        assert_eq!(deserialized.get_consumer_group(), "testGroup");
97        assert_eq!(deserialized.get_topic(), "testTopic");
98    }
99
100    #[test]
101    fn get_consume_stats_request_header_with_topic_request_header_some() {
102        let topic_header = TopicRequestHeader::default();
103
104        let header = GetConsumeStatsRequestHeader {
105            consumer_group: CheetahString::from("testGroup"),
106            topic: CheetahString::from("testTopic"),
107            topic_request_header: Some(topic_header),
108        };
109
110        let json = serde_json::to_string(&header).unwrap();
111        println!("Serialized JSON with topic_request_header: {}", json);
112
113        let deserialized: GetConsumeStatsRequestHeader = serde_json::from_str(&json).unwrap();
114        assert_eq!(deserialized.get_consumer_group(), "testGroup");
115        assert_eq!(deserialized.get_topic(), "testTopic");
116        assert!(deserialized.topic_request_header.is_some());
117    }
118}