Skip to main content

rocketmq_protocol/protocol/body/
query_subscription_response_body.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 serde::Deserialize;
17use serde::Serialize;
18
19use crate::protocol::heartbeat::subscription_data::SubscriptionData;
20
21#[derive(Debug, Clone, Serialize, Deserialize, Default)]
22#[serde(rename_all = "camelCase")]
23pub struct QuerySubscriptionResponseBody {
24    pub subscription_data: Option<SubscriptionData>,
25    pub group: CheetahString,
26    pub topic: CheetahString,
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn query_subscription_response_body_serializes_camel_case_fields() {
35        let body = QuerySubscriptionResponseBody {
36            subscription_data: Some(SubscriptionData {
37                topic: CheetahString::from_static_str("topic-a"),
38                sub_string: CheetahString::from_static_str("*"),
39                ..Default::default()
40            }),
41            group: CheetahString::from_static_str("group-a"),
42            topic: CheetahString::from_static_str("topic-a"),
43        };
44
45        let json = serde_json::to_string(&body).expect("serialize query subscription response body");
46        assert!(json.contains("\"subscriptionData\""));
47        assert!(json.contains("\"group\":\"group-a\""));
48        assert!(json.contains("\"topic\":\"topic-a\""));
49    }
50
51    #[test]
52    fn query_subscription_response_body_allows_missing_subscription_data() {
53        let json = r#"{"group":"group-a","topic":"topic-a","subscriptionData":null}"#;
54        let body: QuerySubscriptionResponseBody =
55            serde_json::from_str(json).expect("deserialize query subscription response body");
56
57        assert!(body.subscription_data.is_none());
58        assert_eq!(body.group, "group-a");
59        assert_eq!(body.topic, "topic-a");
60    }
61}