Skip to main content

rocketmq_protocol/protocol/header/
consumer_send_msg_back_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, Default, RequestHeaderCodec)]
23#[header(
24    type_id = "rocketmq_protocol::protocol::header::consumer_send_msg_back_request_header::ConsumerSendMsgBackRequestHeader",
25    java_class = "org.apache.rocketmq.remoting.protocol.header.ConsumerSendMsgBackRequestHeader"
26)]
27#[serde(rename_all = "camelCase")]
28pub struct ConsumerSendMsgBackRequestHeader {
29    #[header(required)]
30    pub offset: i64,
31    #[header(required)]
32    pub group: CheetahString, //consumer group
33    #[header(required)]
34    pub delay_level: i32,
35    pub origin_msg_id: Option<CheetahString>,
36    pub origin_topic: Option<CheetahString>,
37    #[serde(default)]
38    #[header(default, default_semantic = "literal:false")]
39    pub unit_mode: bool,
40    pub max_reconsume_times: Option<i32>,
41    #[serde(flatten)]
42    #[header(flatten, presence = "always")]
43    pub rpc_request_header: Option<RpcRequestHeader>,
44}
45#[cfg(test)]
46mod tests {
47    use std::collections::HashMap;
48
49    use cheetah_string::CheetahString;
50
51    use super::*;
52    use crate::protocol::command_custom_header::CommandCustomHeader;
53    use crate::protocol::command_custom_header::FromMap;
54
55    #[test]
56    fn consumer_send_msg_back_request_header_serializes_correctly() {
57        let header = ConsumerSendMsgBackRequestHeader {
58            offset: 12345,
59            group: CheetahString::from_static_str("test_group"),
60            delay_level: 2,
61            origin_msg_id: Some(CheetahString::from_static_str("msg_id")),
62            origin_topic: Some(CheetahString::from_static_str("topic")),
63            unit_mode: true,
64            max_reconsume_times: Some(3),
65            rpc_request_header: None,
66        };
67        let map = header.to_map().unwrap();
68        assert_eq!(map.get(&CheetahString::from_static_str("offset")).unwrap(), "12345");
69        assert_eq!(map.get(&CheetahString::from_static_str("group")).unwrap(), "test_group");
70        assert_eq!(map.get(&CheetahString::from_static_str("delayLevel")).unwrap(), "2");
71        assert_eq!(
72            map.get(&CheetahString::from_static_str("originMsgId")).unwrap(),
73            "msg_id"
74        );
75        assert_eq!(
76            map.get(&CheetahString::from_static_str("originTopic")).unwrap(),
77            "topic"
78        );
79        assert_eq!(map.get(&CheetahString::from_static_str("unitMode")).unwrap(), "true");
80        assert_eq!(
81            map.get(&CheetahString::from_static_str("maxReconsumeTimes")).unwrap(),
82            "3"
83        );
84    }
85
86    #[test]
87    fn consumer_send_msg_back_request_header_deserializes_correctly() {
88        let mut map = HashMap::new();
89        map.insert(
90            CheetahString::from_static_str("offset"),
91            CheetahString::from_static_str("12345"),
92        );
93        map.insert(
94            CheetahString::from_static_str("group"),
95            CheetahString::from_static_str("test_group"),
96        );
97        map.insert(
98            CheetahString::from_static_str("delayLevel"),
99            CheetahString::from_static_str("2"),
100        );
101        map.insert(
102            CheetahString::from_static_str("originMsgId"),
103            CheetahString::from_static_str("msg_id"),
104        );
105        map.insert(
106            CheetahString::from_static_str("originTopic"),
107            CheetahString::from_static_str("topic"),
108        );
109        map.insert(
110            CheetahString::from_static_str("unitMode"),
111            CheetahString::from_static_str("true"),
112        );
113        map.insert(
114            CheetahString::from_static_str("maxReconsumeTimes"),
115            CheetahString::from_static_str("3"),
116        );
117
118        let header = <ConsumerSendMsgBackRequestHeader as FromMap>::from(&map).unwrap();
119        assert_eq!(header.offset, 12345);
120        assert_eq!(header.group, "test_group");
121        assert_eq!(header.delay_level, 2);
122        assert_eq!(header.origin_msg_id.unwrap(), "msg_id");
123        assert_eq!(header.origin_topic.unwrap(), "topic");
124        assert!(header.unit_mode);
125        assert_eq!(header.max_reconsume_times.unwrap(), 3);
126    }
127
128    #[test]
129    fn consumer_send_msg_back_request_header_handles_missing_optional_fields() {
130        let mut map = HashMap::new();
131        map.insert(
132            CheetahString::from_static_str("offset"),
133            CheetahString::from_static_str("12345"),
134        );
135        map.insert(
136            CheetahString::from_static_str("group"),
137            CheetahString::from_static_str("test_group"),
138        );
139        map.insert(
140            CheetahString::from_static_str("delayLevel"),
141            CheetahString::from_static_str("2"),
142        );
143        map.insert(
144            CheetahString::from_static_str("unitMode"),
145            CheetahString::from_static_str("true"),
146        );
147
148        let header = <ConsumerSendMsgBackRequestHeader as FromMap>::from(&map).unwrap();
149        assert_eq!(header.offset, 12345);
150        assert_eq!(header.group, "test_group");
151        assert_eq!(header.delay_level, 2);
152        assert!(header.origin_msg_id.is_none());
153        assert!(header.origin_topic.is_none());
154        assert!(header.unit_mode);
155        assert!(header.max_reconsume_times.is_none());
156    }
157
158    #[test]
159    fn consumer_send_msg_back_request_header_handles_invalid_data() {
160        let mut map = HashMap::new();
161        map.insert(
162            CheetahString::from_static_str("offset"),
163            CheetahString::from_static_str("invalid"),
164        );
165        map.insert(
166            CheetahString::from_static_str("group"),
167            CheetahString::from_static_str("test_group"),
168        );
169        map.insert(
170            CheetahString::from_static_str("delayLevel"),
171            CheetahString::from_static_str("invalid"),
172        );
173        map.insert(
174            CheetahString::from_static_str("unitMode"),
175            CheetahString::from_static_str("true"),
176        );
177
178        let result = <ConsumerSendMsgBackRequestHeader as FromMap>::from(&map);
179        assert!(result.is_err());
180    }
181}