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(flatten)]
51    pub topic_request_header: Option<TopicRequestHeader>,
52}
53
54#[cfg(test)]
55mod tests {
56    use cheetah_string::CheetahString;
57    use serde_json;
58
59    use super::*;
60
61    #[test]
62    fn serialize_ack_message_request_header() {
63        let header = AckMessageRequestHeader {
64            consumer_group: CheetahString::from("test_group"),
65            topic: CheetahString::from("test_topic"),
66            queue_id: 1,
67            extra_info: CheetahString::from("extra_info"),
68            offset: 12345,
69            topic_request_header: None,
70        };
71        let json = serde_json::to_string(&header).unwrap();
72        let expected = r#"{"consumerGroup":"test_group","topic":"test_topic","queueId":1,"extraInfo":"extra_info","offset":12345}"#;
73        assert_eq!(json, expected);
74    }
75
76    #[test]
77    fn deserialize_ack_message_request_header() {
78        let json = r#"{"consumerGroup":"test_group","topic":"test_topic","queueId":1,"extraInfo":"extra_info","offset":12345}"#;
79        let header: AckMessageRequestHeader = serde_json::from_str(json).unwrap();
80        assert_eq!(header.consumer_group, CheetahString::from("test_group"));
81        assert_eq!(header.topic, CheetahString::from("test_topic"));
82        assert_eq!(header.queue_id, 1);
83        assert_eq!(header.extra_info, CheetahString::from("extra_info"));
84        assert_eq!(header.offset, 12345);
85        assert!(header.topic_request_header.is_some());
86    }
87
88    #[test]
89    fn deserialize_ack_message_request_header_with_topic_request_header() {
90        let json = r#"{"consumerGroup":"test_group","topic":"test_topic","queueId":1,"extraInfo":"extra_info","offset":12345,"topicRequestHeader":{"someField":"someValue"}}"#;
91        let header: AckMessageRequestHeader = serde_json::from_str(json).unwrap();
92        assert_eq!(header.consumer_group, CheetahString::from("test_group"));
93        assert_eq!(header.topic, CheetahString::from("test_topic"));
94        assert_eq!(header.queue_id, 1);
95        assert_eq!(header.extra_info, CheetahString::from("extra_info"));
96        assert_eq!(header.offset, 12345);
97        assert!(header.topic_request_header.is_some());
98    }
99
100    #[test]
101    fn serialize_ack_message_request_header_with_topic_request_header() {
102        let header = AckMessageRequestHeader {
103            consumer_group: CheetahString::from("test_group"),
104            topic: CheetahString::from("test_topic"),
105            queue_id: 1,
106            extra_info: CheetahString::from("extra_info"),
107            offset: 12345,
108            topic_request_header: None,
109        };
110        let json = serde_json::to_string(&header).unwrap();
111        let expected = r#"{"consumerGroup":"test_group","topic":"test_topic","queueId":1,"extraInfo":"extra_info","offset":12345}"#;
112        assert_eq!(json, expected);
113    }
114}