Skip to main content

rocketmq_protocol/protocol/body/
check_client_request_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 serde::Deserialize;
16use serde::Serialize;
17
18use crate::protocol::heartbeat::subscription_data::SubscriptionData;
19
20#[derive(Serialize, Deserialize, Debug)]
21#[serde(rename_all = "camelCase")]
22pub struct CheckClientRequestBody {
23    pub client_id: String,
24    pub group: String,
25    pub subscription_data: SubscriptionData,
26    pub namespace: Option<String>,
27}
28
29impl CheckClientRequestBody {
30    pub fn new(client_id: String, group: String, subscription_data: SubscriptionData) -> Self {
31        Self {
32            client_id,
33            group,
34            subscription_data,
35            namespace: None,
36        }
37    }
38
39    pub fn get_client_id(&self) -> &String {
40        &self.client_id
41    }
42
43    pub fn set_client_id(&mut self, client_id: String) {
44        self.client_id = client_id;
45    }
46
47    pub fn get_group(&self) -> &String {
48        &self.group
49    }
50
51    pub fn set_group(&mut self, group: String) {
52        self.group = group;
53    }
54
55    pub fn get_subscription_data(&self) -> &SubscriptionData {
56        &self.subscription_data
57    }
58
59    pub fn set_subscription_data(&mut self, subscription_data: SubscriptionData) {
60        self.subscription_data = subscription_data;
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    fn create_mock_subscription() -> SubscriptionData {
69        SubscriptionData {
70            topic: "test-topic".into(),
71            sub_string: "*".into(),
72            ..Default::default()
73        }
74    }
75
76    #[test]
77    fn test_serialization_camel_case() {
78        let sub_data = create_mock_subscription();
79        let body = CheckClientRequestBody::new("c1".into(), "g1".into(), sub_data);
80
81        let json = serde_json::to_string(&body).unwrap();
82
83        assert!(json.contains("\"clientId\":\"c1\""));
84        assert!(json.contains("\"subscriptionData\":"));
85    }
86
87    #[test]
88    fn test_deserialization() {
89        let json = r#"{
90            "clientId": "c2",
91            "group": "g2",
92            "subscriptionData": {
93                "classFilterMode": false,
94                "topic": "top",
95                "subString": "*",
96                "tagsSet": [],
97                "codeSet": [],
98                "subVersion": 1,
99                "expressionType": "TAG"
100            }
101        }"#;
102
103        let decoded: CheckClientRequestBody = serde_json::from_str(json).expect("Failed to decode");
104
105        assert_eq!(decoded.client_id, "c2");
106        assert_eq!(decoded.subscription_data.sub_version, 1);
107    }
108}