Skip to main content

rocketmq_protocol/protocol/header/
unlock_batch_mq_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 rocketmq_macros::RequestHeaderCodec;
16use serde::Deserialize;
17use serde::Serialize;
18
19use crate::rpc::rpc_request_header::RpcRequestHeader;
20
21#[derive(Serialize, Deserialize, Debug, Default, RequestHeaderCodec)]
22#[header(
23    type_id = "rocketmq_protocol::protocol::header::unlock_batch_mq_request_header::UnlockBatchMqRequestHeader",
24    java_class = "org.apache.rocketmq.remoting.protocol.header.UnlockBatchMqRequestHeader"
25)]
26#[serde(rename_all = "camelCase")]
27pub struct UnlockBatchMqRequestHeader {
28    #[serde(flatten)]
29    #[header(flatten, presence = "always")]
30    pub rpc_request_header: Option<RpcRequestHeader>,
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36    use serde_json;
37
38    #[test]
39    fn test_unlock_batch_mq_request_header_serialization() {
40        let rpc_header = RpcRequestHeader {
41            namespace: Some("test_ns".into()),
42            broker_name: Some("broker_a".into()),
43            oneway: Some(true),
44            ..Default::default()
45        };
46
47        let header = UnlockBatchMqRequestHeader {
48            rpc_request_header: Some(rpc_header),
49        };
50
51        let json = serde_json::to_string(&header).unwrap();
52
53        assert!(json.contains("\"ns\":\"test_ns\""));
54        assert!(json.contains("\"bname\":\"broker_a\""));
55        assert!(json.contains("\"oway\":true"));
56    }
57
58    #[test]
59    fn test_unlock_batch_mq_request_header_deserialization() {
60        let json = r#"{
61            "namespace": "standard_ns",
62            "namespaced": true,
63            "brokerName": "rocketmq_broker",
64            "oneway": false
65        }"#;
66
67        let decoded: UnlockBatchMqRequestHeader = serde_json::from_str(json).unwrap();
68        let rpc = decoded.rpc_request_header.expect("RpcRequestHeader should be present");
69
70        assert_eq!(rpc.namespace.unwrap().as_str(), "standard_ns");
71        assert_eq!(rpc.namespaced, Some(true));
72        assert_eq!(rpc.broker_name.unwrap().as_str(), "rocketmq_broker");
73        assert_eq!(rpc.oneway, Some(false));
74    }
75
76    #[test]
77    fn test_partial_fields_deserialization() {
78        let json = r#"{"brokerName": "only_broker"}"#;
79        let decoded: UnlockBatchMqRequestHeader = serde_json::from_str(json).unwrap();
80
81        let rpc = decoded.rpc_request_header.unwrap();
82
83        assert_eq!(rpc.broker_name.unwrap().as_str(), "only_broker");
84        assert!(rpc.namespace.is_none());
85        assert!(rpc.oneway.is_none());
86    }
87}