Skip to main content

rocketmq_protocol/protocol/header/
lite_subscription_ctl_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(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodec)]
22#[header(
23    type_id = "rocketmq_protocol::protocol::header::lite_subscription_ctl_request_header::LiteSubscriptionCtlRequestHeader",
24    java_class = "org.apache.rocketmq.remoting.protocol.header.LiteSubscriptionCtlRequestHeader"
25)]
26#[serde(rename_all = "camelCase")]
27pub struct LiteSubscriptionCtlRequestHeader {
28    #[serde(flatten)]
29    #[header(flatten, presence = "always")]
30    pub rpc_request_header: Option<RpcRequestHeader>,
31}
32
33#[cfg(test)]
34mod tests {
35    use cheetah_string::CheetahString;
36
37    use super::*;
38    use crate::protocol::command_custom_header::CommandCustomHeader;
39    use crate::protocol::command_custom_header::FromMap;
40
41    #[test]
42    fn lite_subscription_ctl_request_header_maps_rpc_fields_like_java_header() {
43        let header = LiteSubscriptionCtlRequestHeader {
44            rpc_request_header: Some(RpcRequestHeader {
45                broker_name: Some(CheetahString::from_static_str("broker-a")),
46                namespace: Some(CheetahString::from_static_str("ns-a")),
47                namespaced: Some(false),
48                oneway: Some(true),
49            }),
50        };
51
52        let map = header.to_map().expect("header should encode");
53        assert_eq!(map.get("bname").map(|value| value.as_str()), Some("broker-a"));
54        assert_eq!(map.get("ns").map(|value| value.as_str()), Some("ns-a"));
55        assert_eq!(map.get("nsd").map(|value| value.as_str()), Some("false"));
56        assert_eq!(map.get("oway").map(|value| value.as_str()), Some("true"));
57
58        let decoded = <LiteSubscriptionCtlRequestHeader as FromMap>::from(&map).expect("header should decode");
59        let rpc = decoded
60            .rpc_request_header
61            .expect("flattened rpc request header should decode");
62        assert_eq!(rpc.broker_name.as_deref(), Some("broker-a"));
63        assert_eq!(rpc.namespace.as_deref(), Some("ns-a"));
64        assert_eq!(rpc.namespaced, Some(false));
65        assert_eq!(rpc.oneway, Some(true));
66    }
67}