rocketmq_remoting/protocol/header/
ack_message_request_header.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use cheetah_string::CheetahString;
18use rocketmq_macros::RequestHeaderCodecV2;
19use serde::Deserialize;
20use serde::Serialize;
21
22use crate::rpc::topic_request_header::TopicRequestHeader;
23
24/// Represents the request header for acknowledging a message.
25#[derive(Debug, Serialize, Deserialize, Clone, RequestHeaderCodecV2)]
26pub struct AckMessageRequestHeader {
27    /// Consumer group name (required)
28    #[serde(rename = "consumerGroup")]
29    #[required]
30    pub consumer_group: CheetahString,
31
32    /// Topic name (required)
33    #[serde(rename = "topic")]
34    #[required]
35    pub topic: CheetahString,
36
37    /// Queue ID (required)
38    #[serde(rename = "queueId")]
39    #[required]
40    pub queue_id: i32,
41
42    /// Extra information (required)
43    #[serde(rename = "extraInfo")]
44    #[required]
45    pub extra_info: CheetahString,
46
47    /// Offset (required)
48    #[serde(rename = "offset")]
49    #[required]
50    pub offset: i64,
51
52    #[serde(flatten)]
53    pub topic_request_header: Option<TopicRequestHeader>,
54}
55
56#[cfg(test)]
57mod tests {
58    use cheetah_string::CheetahString;
59    use serde_json;
60
61    use super::*;
62
63    #[test]
64    fn serialize_ack_message_request_header() {
65        let header = AckMessageRequestHeader {
66            consumer_group: CheetahString::from("test_group"),
67            topic: CheetahString::from("test_topic"),
68            queue_id: 1,
69            extra_info: CheetahString::from("extra_info"),
70            offset: 12345,
71            topic_request_header: None,
72        };
73        let json = serde_json::to_string(&header).unwrap();
74        let expected = r#"{"consumerGroup":"test_group","topic":"test_topic","queueId":1,"extraInfo":"extra_info","offset":12345}"#;
75        assert_eq!(json, expected);
76    }
77
78    #[test]
79    fn deserialize_ack_message_request_header() {
80        let json = r#"{"consumerGroup":"test_group","topic":"test_topic","queueId":1,"extraInfo":"extra_info","offset":12345}"#;
81        let header: AckMessageRequestHeader = serde_json::from_str(json).unwrap();
82        assert_eq!(header.consumer_group, CheetahString::from("test_group"));
83        assert_eq!(header.topic, CheetahString::from("test_topic"));
84        assert_eq!(header.queue_id, 1);
85        assert_eq!(header.extra_info, CheetahString::from("extra_info"));
86        assert_eq!(header.offset, 12345);
87        assert!(header.topic_request_header.is_some());
88    }
89
90    #[test]
91    fn deserialize_ack_message_request_header_with_topic_request_header() {
92        let json = r#"{"consumerGroup":"test_group","topic":"test_topic","queueId":1,"extraInfo":"extra_info","offset":12345,"topicRequestHeader":{"someField":"someValue"}}"#;
93        let header: AckMessageRequestHeader = serde_json::from_str(json).unwrap();
94        assert_eq!(header.consumer_group, CheetahString::from("test_group"));
95        assert_eq!(header.topic, CheetahString::from("test_topic"));
96        assert_eq!(header.queue_id, 1);
97        assert_eq!(header.extra_info, CheetahString::from("extra_info"));
98        assert_eq!(header.offset, 12345);
99        assert!(header.topic_request_header.is_some());
100    }
101
102    #[test]
103    fn serialize_ack_message_request_header_with_topic_request_header() {
104        let header = AckMessageRequestHeader {
105            consumer_group: CheetahString::from("test_group"),
106            topic: CheetahString::from("test_topic"),
107            queue_id: 1,
108            extra_info: CheetahString::from("extra_info"),
109            offset: 12345,
110            topic_request_header: None,
111        };
112        let json = serde_json::to_string(&header).unwrap();
113        let expected = r#"{"consumerGroup":"test_group","topic":"test_topic","queueId":1,"extraInfo":"extra_info","offset":12345}"#;
114        assert_eq!(json, expected);
115    }
116}