Skip to main content

rocketmq_remoting/protocol/header/
query_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#[derive(Debug, Clone, Serialize, Deserialize, RequestHeaderCodecV2)]
23#[serde(rename_all = "camelCase")]
24pub struct QueryMessageRequestHeader {
25    #[required]
26    pub topic: CheetahString,
27
28    #[required]
29    pub key: CheetahString,
30
31    #[required]
32    pub max_num: i32,
33
34    #[required]
35    pub begin_timestamp: i64,
36
37    #[required]
38    pub end_timestamp: i64,
39
40    pub index_type: Option<CheetahString>,
41
42    pub last_key: Option<CheetahString>,
43
44    #[serde(flatten)]
45    pub topic_request_header: Option<TopicRequestHeader>,
46}
47
48#[cfg(test)]
49mod query_message_request_header_tests {
50    use std::collections::HashMap;
51
52    use super::*;
53    use crate::protocol::command_custom_header::CommandCustomHeader;
54    use crate::protocol::command_custom_header::FromMap;
55
56    #[test]
57    fn creating_from_map_with_all_fields_populates_struct_correctly() {
58        let mut map = HashMap::new();
59        map.insert("topic".into(), "test_topic".into());
60        map.insert("key".into(), "test_key".into());
61        map.insert("maxNum".into(), "10".into());
62        map.insert("beginTimestamp".into(), "1000".into());
63        map.insert("endTimestamp".into(), "2000".into());
64
65        let header: QueryMessageRequestHeader = <QueryMessageRequestHeader as FromMap>::from(&map).unwrap();
66
67        assert_eq!(header.topic, "test_topic");
68        assert_eq!(header.key, "test_key");
69        assert_eq!(header.max_num, 10);
70        assert_eq!(header.begin_timestamp, 1000);
71        assert_eq!(header.end_timestamp, 2000);
72        assert!(header.index_type.is_none());
73        assert!(header.last_key.is_none());
74    }
75
76    #[test]
77    fn creating_from_map_with_optional_index_fields_populates_struct_correctly() {
78        let mut map = HashMap::new();
79        map.insert("topic".into(), "test_topic".into());
80        map.insert("key".into(), "test_key".into());
81        map.insert("maxNum".into(), "10".into());
82        map.insert("beginTimestamp".into(), "1000".into());
83        map.insert("endTimestamp".into(), "2000".into());
84        map.insert("indexType".into(), "U".into());
85        map.insert("lastKey".into(), "next-index-key".into());
86
87        let header: QueryMessageRequestHeader = <QueryMessageRequestHeader as FromMap>::from(&map).unwrap();
88
89        assert_eq!(header.topic, "test_topic");
90        assert_eq!(header.key, "test_key");
91        assert_eq!(header.max_num, 10);
92        assert_eq!(header.index_type.as_deref(), Some("U"));
93        assert_eq!(header.last_key.as_deref(), Some("next-index-key"));
94    }
95
96    #[test]
97    fn creating_from_map_with_invalid_number_fields_returns_none() {
98        let mut map = HashMap::new();
99        map.insert("topic".into(), "test_topic".into());
100        map.insert("key".into(), "test_key".into());
101        map.insert("maxNum".into(), "invalid".into());
102
103        let header: Result<QueryMessageRequestHeader, rocketmq_error::RocketMQError> =
104            <QueryMessageRequestHeader as FromMap>::from(&map);
105
106        assert!(header.is_err());
107    }
108
109    #[test]
110    fn to_map_includes_all_fields() {
111        let header = QueryMessageRequestHeader {
112            topic: "test_topic".into(),
113            key: "test_key".into(),
114            max_num: 10,
115            begin_timestamp: 1000,
116            end_timestamp: 2000,
117            index_type: Some("K".into()),
118            last_key: Some("last-key".into()),
119            topic_request_header: None,
120        };
121
122        let map: HashMap<CheetahString, CheetahString> = header.to_map().unwrap();
123
124        assert_eq!(map.get("topic").unwrap(), "test_topic");
125        assert_eq!(map.get("key").unwrap(), "test_key");
126        assert_eq!(map.get("maxNum").unwrap(), "10");
127        assert_eq!(map.get("beginTimestamp").unwrap(), "1000");
128        assert_eq!(map.get("endTimestamp").unwrap(), "2000");
129        assert_eq!(map.get("indexType").unwrap(), "K");
130        assert_eq!(map.get("lastKey").unwrap(), "last-key");
131    }
132
133    #[test]
134    fn to_map_with_topic_request_header_includes_nested_fields() {
135        let topic_request_header = TopicRequestHeader::default();
136        let header = QueryMessageRequestHeader {
137            topic: "test_topic".into(),
138            key: "test_key".into(),
139            max_num: 10,
140            begin_timestamp: 1000,
141            end_timestamp: 2000,
142            index_type: None,
143            last_key: None,
144            topic_request_header: Some(topic_request_header),
145        };
146
147        let map = header.to_map().unwrap();
148
149        // Verify that nested fields are included, assuming specific fields in TopicRequestHeader
150        // This is a placeholder assertion; actual fields and checks depend on TopicRequestHeader's
151        // structure
152        assert!(!map.contains_key("nestedField"));
153    }
154}