Skip to main content

rocketmq_protocol/protocol/header/
reset_offset_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#[derive(Clone, Debug, Serialize, Deserialize, RequestHeaderCodec)]
23#[header(
24    type_id = "rocketmq_protocol::protocol::header::reset_offset_request_header::ResetOffsetRequestHeader",
25    java_class = "org.apache.rocketmq.remoting.protocol.header.ResetOffsetRequestHeader"
26)]
27#[serde(rename_all = "camelCase")]
28pub struct ResetOffsetRequestHeader {
29    #[header(required)]
30    pub topic: CheetahString,
31    #[header(required)]
32    pub group: CheetahString,
33    #[serde(default = "default_queue_id")]
34    #[header(default_with = "default_queue_id", default_semantic = "literal:-1")]
35    pub queue_id: i32,
36    pub offset: Option<i64>,
37    #[header(required)]
38    pub timestamp: i64,
39    #[header(required)]
40    pub is_force: bool,
41
42    #[serde(flatten)]
43    #[header(flatten, presence = "always")]
44    pub topic_request_header: Option<TopicRequestHeader>,
45}
46
47fn default_queue_id() -> i32 {
48    -1
49}
50
51impl Default for ResetOffsetRequestHeader {
52    fn default() -> Self {
53        ResetOffsetRequestHeader {
54            topic: CheetahString::empty(),
55            group: CheetahString::empty(),
56            queue_id: default_queue_id(),
57            offset: None,
58            timestamp: 0,
59            is_force: false,
60            topic_request_header: None,
61        }
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use cheetah_string::CheetahString;
68
69    use super::*;
70
71    #[test]
72    fn reset_offset_request_header_serializes_correctly() {
73        let header = ResetOffsetRequestHeader {
74            topic: CheetahString::from_static_str("test_topic"),
75            group: CheetahString::from_static_str("test_group"),
76            queue_id: 1,
77            offset: Some(100),
78            timestamp: 1234567890,
79            is_force: true,
80            topic_request_header: None,
81        };
82        let serialized = serde_json::to_string(&header).unwrap();
83        let expected = r#"{"topic":"test_topic","group":"test_group","queueId":1,"offset":100,"timestamp":1234567890,"isForce":true}"#;
84        assert_eq!(serialized, expected);
85    }
86
87    #[test]
88    fn reset_offset_request_header_deserializes_correctly() {
89        let data = r#"{"topic":"test_topic","group":"test_group","queueId":1,"offset":100,"timestamp":1234567890,"isForce":true}"#;
90        let header: ResetOffsetRequestHeader = serde_json::from_str(data).unwrap();
91        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
92        assert_eq!(header.group, CheetahString::from_static_str("test_group"));
93        assert_eq!(header.queue_id, 1);
94        assert_eq!(header.offset.unwrap(), 100);
95        assert_eq!(header.timestamp, 1234567890);
96        assert!(header.is_force);
97    }
98
99    #[test]
100    fn reset_offset_request_header_handles_missing_optional_fields() {
101        let data = r#"{"topic":"test_topic","group":"test_group","queueId":1,"timestamp":1234567890,"isForce":true}"#;
102        let header: ResetOffsetRequestHeader = serde_json::from_str(data).unwrap();
103        assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
104        assert_eq!(header.group, CheetahString::from_static_str("test_group"));
105        assert_eq!(header.queue_id, 1);
106        assert!(header.offset.is_none());
107        assert_eq!(header.timestamp, 1234567890);
108        assert!(header.is_force);
109    }
110
111    #[test]
112    fn reset_offset_request_header_handles_invalid_data() {
113        let data = r#"{"topic":12345,"group":"test_group","queueId":1,"timestamp":1234567890,"isForce":true}"#;
114        let result: Result<ResetOffsetRequestHeader, _> = serde_json::from_str(data);
115        assert!(result.is_err());
116    }
117
118    #[test]
119    fn reset_offset_request_header_default_values() {
120        let header = ResetOffsetRequestHeader::default();
121        assert_eq!(header.topic, CheetahString::empty());
122        assert_eq!(header.group, CheetahString::empty());
123        assert_eq!(header.queue_id, -1);
124        assert!(header.offset.is_none());
125        assert_eq!(header.timestamp, 0);
126        assert!(!header.is_force);
127    }
128}