Skip to main content

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