Skip to main content

rocketmq_protocol/protocol/body/request/
lock_batch_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 std::collections::HashSet;
16use std::fmt::Display;
17
18use cheetah_string::CheetahString;
19use rocketmq_model::message::MessageQueue;
20use serde::Deserialize;
21use serde::Serialize;
22
23#[derive(Serialize, Deserialize, Debug, Default, Clone)]
24#[serde(rename_all = "camelCase")]
25pub struct LockBatchRequestBody {
26    pub consumer_group: Option<CheetahString>,
27    pub client_id: Option<CheetahString>,
28    pub only_this_broker: bool,
29    pub mq_set: HashSet<MessageQueue>,
30}
31
32impl Display for LockBatchRequestBody {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        write!(
35            f,
36            "LockBatchRequestBody [consumer_group={}, client_id={}, only_this_broker={}, mq_set={:?}]",
37            self.consumer_group.as_ref().unwrap_or(&"".into()),
38            self.client_id.as_ref().unwrap_or(&"".into()),
39            self.only_this_broker,
40            self.mq_set
41        )
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_lock_batch_request_body_serialization() {
51        let mut mq_set = HashSet::new();
52        mq_set.insert(MessageQueue::from_parts("topic", "broker", 1));
53        let body = LockBatchRequestBody {
54            consumer_group: Some(CheetahString::from("group")),
55            client_id: Some(CheetahString::from("client")),
56            only_this_broker: true,
57            mq_set,
58        };
59        let json = serde_json::to_string(&body).unwrap();
60        assert!(json.contains("consumerGroup"));
61        assert!(json.contains("clientId"));
62        assert!(json.contains("onlyThisBroker"));
63        assert!(json.contains("mqSet"));
64    }
65
66    #[test]
67    fn test_lock_batch_request_body_deserialization() {
68        let json = r#"{"consumerGroup":"group","clientId":"client","onlyThisBroker":true,"mqSet":[{"topic":"topic","brokerName":"broker","queueId":1}]}"#;
69        let body: LockBatchRequestBody = serde_json::from_str(json).unwrap();
70        assert_eq!(body.consumer_group.unwrap(), "group");
71        assert_eq!(body.client_id.unwrap(), "client");
72        assert!(body.only_this_broker);
73        assert_eq!(body.mq_set.len(), 1);
74    }
75
76    #[test]
77    fn test_lock_batch_request_body_display() {
78        let body = LockBatchRequestBody {
79            consumer_group: Some(CheetahString::from("group")),
80            client_id: Some(CheetahString::from("client")),
81            only_this_broker: true,
82            mq_set: HashSet::new(),
83        };
84        let display = format!("{}", body);
85        let expected =
86            "LockBatchRequestBody [consumer_group=group, client_id=client, only_this_broker=true, mq_set={}]";
87        assert_eq!(display, expected);
88    }
89}