Skip to main content

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