Skip to main content

rocketmq_protocol/protocol/header/
query_consumer_offset_response_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 rocketmq_macros::RequestHeaderCodec;
16use serde::Deserialize;
17use serde::Serialize;
18
19#[derive(Debug, Clone, Serialize, Deserialize, RequestHeaderCodec, Default)]
20#[serde(rename_all = "camelCase")]
21#[header(
22    type_id = "rocketmq_protocol::protocol::header::query_consumer_offset_response_header::QueryConsumerOffsetResponseHeader",
23    java_class = "org.apache.rocketmq.remoting.protocol.header.QueryConsumerOffsetResponseHeader"
24)]
25pub struct QueryConsumerOffsetResponseHeader {
26    pub offset: Option<i64>,
27}
28
29#[cfg(test)]
30mod tests {
31    use std::collections::HashMap;
32
33    use cheetah_string::CheetahString;
34
35    use super::*;
36    use crate::protocol::command_custom_header::FromMap;
37
38    #[test]
39    fn query_consumer_offset_response_header_serialization() {
40        let header = QueryConsumerOffsetResponseHeader { offset: Some(12345) };
41        let json = serde_json::to_string(&header).unwrap();
42        assert_eq!(json, r#"{"offset":12345}"#);
43
44        let header_none = QueryConsumerOffsetResponseHeader { offset: None };
45        let json_none = serde_json::to_string(&header_none).unwrap();
46        assert_eq!(json_none, r#"{"offset":null}"#);
47    }
48
49    #[test]
50    fn query_consumer_offset_response_header_deserialization() {
51        let json = r#"{"offset":12345}"#;
52        let header: QueryConsumerOffsetResponseHeader = serde_json::from_str(json).unwrap();
53        assert_eq!(header.offset, Some(12345));
54
55        let json_none = r#"{"offset":null}"#;
56        let header_none: QueryConsumerOffsetResponseHeader = serde_json::from_str(json_none).unwrap();
57        assert!(header_none.offset.is_none());
58    }
59
60    #[test]
61    fn query_consumer_offset_response_header_from_map() {
62        let mut map = HashMap::new();
63        map.insert(CheetahString::from("offset"), CheetahString::from("12345"));
64        let header = <QueryConsumerOffsetResponseHeader as FromMap>::from(&map).unwrap();
65        assert_eq!(header.offset, Some(12345));
66
67        let map_empty = HashMap::new();
68        let header_none = <QueryConsumerOffsetResponseHeader as FromMap>::from(&map_empty).unwrap();
69        assert!(header_none.offset.is_none());
70    }
71}