Skip to main content

rocketmq_remoting/protocol/header/
query_subscription_by_consumer_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(Clone, Debug, Serialize, Deserialize, RequestHeaderCodecV2)]
23#[serde(rename_all = "camelCase")]
24pub struct QuerySubscriptionByConsumerRequestHeader {
25    #[required]
26    pub group: CheetahString,
27
28    #[required]
29    pub topic: CheetahString,
30
31    #[serde(flatten)]
32    pub topic_request_header: Option<TopicRequestHeader>,
33}
34
35#[cfg(test)]
36mod tests {
37    use cheetah_string::CheetahString;
38
39    use super::*;
40
41    #[test]
42    fn query_subscription_by_consumer_request_header_serializes_correctly() {
43        let header = QuerySubscriptionByConsumerRequestHeader {
44            group: CheetahString::from_static_str("test_group"),
45            topic: CheetahString::from_static_str("test_topic"),
46            topic_request_header: None,
47        };
48        let serialized = serde_json::to_string(&header).unwrap();
49        let expected = r#"{"group":"test_group","topic":"test_topic"}"#;
50        assert_eq!(serialized, expected);
51    }
52
53    #[test]
54    fn query_subscription_by_consumer_request_header_deserializes_correctly() {
55        let data = r#"{"group":"test_group","topic":"test_topic"}"#;
56        let header: QuerySubscriptionByConsumerRequestHeader = serde_json::from_str(data).unwrap();
57        assert_eq!(header.group, CheetahString::from_static_str("test_group"));
58        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
59        assert!(header.topic_request_header.is_some());
60    }
61
62    #[test]
63    fn query_subscription_by_consumer_request_header_handles_missing_optional_fields() {
64        let data = r#"{"group":"test_group","topic":"test_topic"}"#;
65        let header: QuerySubscriptionByConsumerRequestHeader = serde_json::from_str(data).unwrap();
66        assert_eq!(header.group, CheetahString::from_static_str("test_group"));
67        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
68        assert!(header.topic_request_header.is_some());
69    }
70
71    #[test]
72    fn query_subscription_by_consumer_request_header_handles_invalid_data() {
73        let data = r#"{"group":12345,"topic":"test_topic"}"#;
74        let result: Result<QuerySubscriptionByConsumerRequestHeader, _> = serde_json::from_str(data);
75        assert!(result.is_err());
76    }
77}