Skip to main content

rocketmq_protocol/protocol/header/
notify_unsubscribe_lite_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 cheetah_string::CheetahString;
16use rocketmq_macros::RequestHeaderCodec;
17use serde::Deserialize;
18use serde::Serialize;
19
20use crate::rpc::rpc_request_header::RpcRequestHeader;
21
22#[derive(Clone, Debug, Serialize, Deserialize, RequestHeaderCodec)]
23#[header(
24    type_id = "rocketmq_protocol::protocol::header::notify_unsubscribe_lite_request_header::NotifyUnsubscribeLiteRequestHeader",
25    java_class = "org.apache.rocketmq.remoting.protocol.header.NotifyUnsubscribeLiteRequestHeader"
26)]
27#[serde(rename_all = "camelCase")]
28pub struct NotifyUnsubscribeLiteRequestHeader {
29    #[header(required)]
30    pub lite_topic: CheetahString,
31
32    #[header(required)]
33    pub consumer_group: CheetahString,
34
35    #[header(required)]
36    pub client_id: CheetahString,
37
38    #[serde(flatten)]
39    #[header(flatten, presence = "always")]
40    pub rpc_request_header: Option<RpcRequestHeader>,
41}
42
43#[cfg(test)]
44mod tests {
45    use std::collections::HashMap;
46
47    use super::*;
48    use crate::protocol::command_custom_header::CommandCustomHeader;
49    use crate::protocol::command_custom_header::FromMap;
50
51    #[test]
52    fn notify_unsubscribe_lite_request_header_round_trips_ext_fields() {
53        let header = NotifyUnsubscribeLiteRequestHeader {
54            lite_topic: CheetahString::from_static_str("lite-topic"),
55            consumer_group: CheetahString::from_static_str("consumer-group"),
56            client_id: CheetahString::from_static_str("client-id"),
57            rpc_request_header: None,
58        };
59
60        let map = header.to_map().expect("header should encode to ext fields");
61        assert_eq!(
62            map.get(&CheetahString::from_static_str("liteTopic")),
63            Some(&CheetahString::from_static_str("lite-topic"))
64        );
65        assert_eq!(
66            map.get(&CheetahString::from_static_str("consumerGroup")),
67            Some(&CheetahString::from_static_str("consumer-group"))
68        );
69        assert_eq!(
70            map.get(&CheetahString::from_static_str("clientId")),
71            Some(&CheetahString::from_static_str("client-id"))
72        );
73
74        let decoded =
75            <NotifyUnsubscribeLiteRequestHeader as FromMap>::from(&map).expect("header should decode from ext fields");
76        assert_eq!(decoded.lite_topic, "lite-topic");
77        assert_eq!(decoded.consumer_group, "consumer-group");
78        assert_eq!(decoded.client_id, "client-id");
79    }
80
81    #[test]
82    fn notify_unsubscribe_lite_request_header_requires_java_fields() {
83        let mut map = HashMap::new();
84        map.insert(
85            CheetahString::from_static_str("liteTopic"),
86            CheetahString::from_static_str("lite-topic"),
87        );
88        map.insert(
89            CheetahString::from_static_str("consumerGroup"),
90            CheetahString::from_static_str("consumer-group"),
91        );
92
93        assert!(
94            <NotifyUnsubscribeLiteRequestHeader as FromMap>::from(&map).is_err(),
95            "clientId is required by the Java header contract"
96        );
97    }
98}