Skip to main content

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