Skip to main content

rocketmq_protocol/protocol/header/
ack_message_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::topic_request_header::TopicRequestHeader;
21
22/// Represents the request header for acknowledging a message.
23#[derive(Debug, Serialize, Deserialize, Clone, RequestHeaderCodec)]
24#[header(
25    type_id = "rocketmq_protocol::protocol::header::ack_message_request_header::AckMessageRequestHeader",
26    java_class = "org.apache.rocketmq.remoting.protocol.header.AckMessageRequestHeader"
27)]
28pub struct AckMessageRequestHeader {
29    /// Consumer group name (required)
30    #[serde(rename = "consumerGroup")]
31    #[header(required)]
32    pub consumer_group: CheetahString,
33
34    /// Topic name (required)
35    #[serde(rename = "topic")]
36    #[header(required)]
37    pub topic: CheetahString,
38
39    /// Queue ID (required)
40    #[serde(rename = "queueId")]
41    #[header(required)]
42    pub queue_id: i32,
43
44    /// Extra information (required)
45    #[serde(rename = "extraInfo")]
46    #[header(required)]
47    pub extra_info: CheetahString,
48
49    /// Offset (required)
50    #[serde(rename = "offset")]
51    #[header(required)]
52    pub offset: i64,
53
54    #[serde(rename = "liteTopic", skip_serializing_if = "Option::is_none")]
55    pub lite_topic: Option<CheetahString>,
56
57    #[serde(flatten)]
58    #[header(flatten, presence = "always")]
59    pub topic_request_header: Option<TopicRequestHeader>,
60}
61
62#[cfg(test)]
63mod tests {
64    use cheetah_string::CheetahString;
65    use serde_json;
66
67    use super::*;
68    use crate::protocol::command_custom_header::CommandCustomHeader;
69    use crate::protocol::command_custom_header::FromMap;
70
71    #[test]
72    fn serialize_ack_message_request_header() {
73        let header = AckMessageRequestHeader {
74            consumer_group: CheetahString::from("test_group"),
75            topic: CheetahString::from("test_topic"),
76            queue_id: 1,
77            extra_info: CheetahString::from("extra_info"),
78            offset: 12345,
79            lite_topic: Some(CheetahString::from("lite_topic")),
80            topic_request_header: None,
81        };
82        let json = serde_json::to_string(&header).unwrap();
83        let expected = r#"{"consumerGroup":"test_group","topic":"test_topic","queueId":1,"extraInfo":"extra_info","offset":12345,"liteTopic":"lite_topic"}"#;
84        assert_eq!(json, expected);
85    }
86
87    #[test]
88    fn deserialize_ack_message_request_header() {
89        let json = r#"{"consumerGroup":"test_group","topic":"test_topic","queueId":1,"extraInfo":"extra_info","offset":12345,"liteTopic":"lite_topic"}"#;
90        let header: AckMessageRequestHeader = serde_json::from_str(json).unwrap();
91        assert_eq!(header.consumer_group, CheetahString::from("test_group"));
92        assert_eq!(header.topic, CheetahString::from("test_topic"));
93        assert_eq!(header.queue_id, 1);
94        assert_eq!(header.extra_info, CheetahString::from("extra_info"));
95        assert_eq!(header.offset, 12345);
96        assert_eq!(header.lite_topic.as_deref(), Some("lite_topic"));
97        assert!(header.topic_request_header.is_some());
98    }
99
100    #[test]
101    fn deserialize_ack_message_request_header_with_topic_request_header() {
102        let json = r#"{"consumerGroup":"test_group","topic":"test_topic","queueId":1,"extraInfo":"extra_info","offset":12345,"topicRequestHeader":{"someField":"someValue"}}"#;
103        let header: AckMessageRequestHeader = serde_json::from_str(json).unwrap();
104        assert_eq!(header.consumer_group, CheetahString::from("test_group"));
105        assert_eq!(header.topic, CheetahString::from("test_topic"));
106        assert_eq!(header.queue_id, 1);
107        assert_eq!(header.extra_info, CheetahString::from("extra_info"));
108        assert_eq!(header.offset, 12345);
109        assert!(header.lite_topic.is_none());
110        assert!(header.topic_request_header.is_some());
111    }
112
113    #[test]
114    fn serialize_ack_message_request_header_with_topic_request_header() {
115        let header = AckMessageRequestHeader {
116            consumer_group: CheetahString::from("test_group"),
117            topic: CheetahString::from("test_topic"),
118            queue_id: 1,
119            extra_info: CheetahString::from("extra_info"),
120            offset: 12345,
121            lite_topic: None,
122            topic_request_header: None,
123        };
124        let json = serde_json::to_string(&header).unwrap();
125        let expected = r#"{"consumerGroup":"test_group","topic":"test_topic","queueId":1,"extraInfo":"extra_info","offset":12345}"#;
126        assert_eq!(json, expected);
127    }
128
129    #[test]
130    fn ack_message_request_header_maps_lite_topic_like_java_header() {
131        let header = AckMessageRequestHeader {
132            consumer_group: CheetahString::from("test_group"),
133            topic: CheetahString::from("test_topic"),
134            queue_id: 1,
135            extra_info: CheetahString::from("extra_info"),
136            offset: 12345,
137            lite_topic: Some(CheetahString::from("lite_topic")),
138            topic_request_header: None,
139        };
140
141        let map = header.to_map().unwrap();
142        assert_eq!(map.get("liteTopic").map(|value| value.as_str()), Some("lite_topic"));
143
144        let decoded = <AckMessageRequestHeader as FromMap>::from(&map).unwrap();
145        assert_eq!(decoded.lite_topic.as_deref(), Some("lite_topic"));
146    }
147}