Skip to main content

rocketmq_remoting/protocol/header/
get_consumer_running_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::RequestHeaderCodecV2;
17use serde::Deserialize;
18use serde::Serialize;
19
20use crate::rpc::rpc_request_header::RpcRequestHeader;
21
22#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2)]
23#[serde(rename_all = "camelCase")]
24pub struct GetConsumerRunningInfoRequestHeader {
25    #[required]
26    pub consumer_group: CheetahString,
27
28    #[required]
29    pub client_id: CheetahString,
30
31    pub jstack_enable: bool,
32
33    #[serde(flatten)]
34    pub rpc_request_header: Option<RpcRequestHeader>,
35}
36
37#[cfg(test)]
38mod tests {
39    use cheetah_string::CheetahString;
40
41    use super::*;
42
43    #[test]
44    fn get_consumer_running_info_request_header_serializes_correctly() {
45        let header = GetConsumerRunningInfoRequestHeader {
46            consumer_group: CheetahString::from_static_str("test_group"),
47            client_id: CheetahString::from_static_str("client_id"),
48            jstack_enable: true,
49            rpc_request_header: None,
50        };
51        let serialized = serde_json::to_string(&header).unwrap();
52        let expected = r#"{"consumerGroup":"test_group","clientId":"client_id","jstackEnable":true}"#;
53        assert_eq!(serialized, expected);
54    }
55
56    #[test]
57    fn get_consumer_running_info_request_header_deserializes_correctly() {
58        let data = r#"{"consumerGroup":"test_group","clientId":"client_id","jstackEnable":true}"#;
59        let header: GetConsumerRunningInfoRequestHeader = serde_json::from_str(data).unwrap();
60        assert_eq!(header.consumer_group, CheetahString::from_static_str("test_group"));
61        assert_eq!(header.client_id, CheetahString::from_static_str("client_id"));
62        assert!(header.jstack_enable);
63        assert!(header.rpc_request_header.is_some());
64    }
65
66    #[test]
67    fn get_consumer_running_info_request_header_handles_missing_optional_fields() {
68        let data = r#"{"consumerGroup":"test_group","clientId":"client_id","jstackEnable":false}"#;
69        let header: GetConsumerRunningInfoRequestHeader = serde_json::from_str(data).unwrap();
70        assert_eq!(header.consumer_group, CheetahString::from_static_str("test_group"));
71        assert_eq!(header.client_id, CheetahString::from_static_str("client_id"));
72        assert!(!header.jstack_enable);
73        assert!(header.rpc_request_header.is_some());
74    }
75
76    #[test]
77    fn get_consumer_running_info_request_header_handles_invalid_data() {
78        let data = r#"{"consumerGroup":12345,"clientId":"client_id"}"#;
79        let result: Result<GetConsumerRunningInfoRequestHeader, _> = serde_json::from_str(data);
80        assert!(result.is_err());
81    }
82}