rocketmq_remoting/protocol/header/
query_subscription_by_consumer_request_header.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use cheetah_string::CheetahString;
18use rocketmq_macros::RequestHeaderCodecV2;
19use serde::Deserialize;
20use serde::Serialize;
21
22use crate::rpc::topic_request_header::TopicRequestHeader;
23
24#[derive(Clone, Debug, Serialize, Deserialize, RequestHeaderCodecV2)]
25#[serde(rename_all = "camelCase")]
26pub struct QuerySubscriptionByConsumerRequestHeader {
27    #[required]
28    pub group: CheetahString,
29
30    #[required]
31    pub topic: CheetahString,
32
33    #[serde(flatten)]
34    pub topic_request_header: Option<TopicRequestHeader>,
35}
36
37#[cfg(test)]
38mod tests {
39    use cheetah_string::CheetahString;
40
41    use super::*;
42
43    #[test]
44    fn query_subscription_by_consumer_request_header_serializes_correctly() {
45        let header = QuerySubscriptionByConsumerRequestHeader {
46            group: CheetahString::from_static_str("test_group"),
47            topic: CheetahString::from_static_str("test_topic"),
48            topic_request_header: None,
49        };
50        let serialized = serde_json::to_string(&header).unwrap();
51        let expected = r#"{"group":"test_group","topic":"test_topic"}"#;
52        assert_eq!(serialized, expected);
53    }
54
55    #[test]
56    fn query_subscription_by_consumer_request_header_deserializes_correctly() {
57        let data = r#"{"group":"test_group","topic":"test_topic"}"#;
58        let header: QuerySubscriptionByConsumerRequestHeader = serde_json::from_str(data).unwrap();
59        assert_eq!(header.group, CheetahString::from_static_str("test_group"));
60        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
61        assert!(header.topic_request_header.is_some());
62    }
63
64    #[test]
65    fn query_subscription_by_consumer_request_header_handles_missing_optional_fields() {
66        let data = r#"{"group":"test_group","topic":"test_topic"}"#;
67        let header: QuerySubscriptionByConsumerRequestHeader = serde_json::from_str(data).unwrap();
68        assert_eq!(header.group, CheetahString::from_static_str("test_group"));
69        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
70        assert!(header.topic_request_header.is_some());
71    }
72
73    #[test]
74    fn query_subscription_by_consumer_request_header_handles_invalid_data() {
75        let data = r#"{"group":12345,"topic":"test_topic"}"#;
76        let result: Result<QuerySubscriptionByConsumerRequestHeader, _> =
77            serde_json::from_str(data);
78        assert!(result.is_err());
79    }
80}