Skip to main content

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