Skip to main content

rocketmq_remoting/protocol/header/
check_transaction_state_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::rpc_request_header::RpcRequestHeader;
21
22#[derive(Serialize, Deserialize, Debug, Default, RequestHeaderCodecV2)]
23#[serde(rename_all = "camelCase")]
24pub struct CheckTransactionStateRequestHeader {
25    pub topic: Option<CheetahString>,
26    #[required]
27    pub tran_state_table_offset: i64,
28    #[required]
29    pub commit_log_offset: i64,
30    pub msg_id: Option<CheetahString>,
31    pub transaction_id: Option<CheetahString>,
32    pub offset_msg_id: Option<CheetahString>,
33    #[serde(flatten)]
34    pub rpc_request_header: Option<RpcRequestHeader>,
35}
36
37#[cfg(test)]
38mod tests {
39    use std::collections::HashMap;
40
41    use cheetah_string::CheetahString;
42
43    use super::*;
44    use crate::protocol::command_custom_header::CommandCustomHeader;
45    use crate::protocol::command_custom_header::FromMap;
46
47    #[test]
48    fn check_transaction_state_request_header_serializes_correctly() {
49        let header = CheckTransactionStateRequestHeader {
50            topic: Some(CheetahString::from_static_str("test_topic")),
51            tran_state_table_offset: 123,
52            commit_log_offset: 456,
53            msg_id: Some(CheetahString::from_static_str("test_msg_id")),
54            transaction_id: Some(CheetahString::from_static_str("test_transaction_id")),
55            offset_msg_id: Some(CheetahString::from_static_str("test_offset_msg_id")),
56            rpc_request_header: None,
57        };
58        let map = header.to_map().unwrap();
59        assert_eq!(map.get(&CheetahString::from_static_str("topic")).unwrap(), "test_topic");
60        assert_eq!(
61            map.get(&CheetahString::from_static_str("tranStateTableOffset"))
62                .unwrap(),
63            "123"
64        );
65        assert_eq!(
66            map.get(&CheetahString::from_static_str("commitLogOffset")).unwrap(),
67            "456"
68        );
69        assert_eq!(
70            map.get(&CheetahString::from_static_str("msgId")).unwrap(),
71            "test_msg_id"
72        );
73        assert_eq!(
74            map.get(&CheetahString::from_static_str("transactionId")).unwrap(),
75            "test_transaction_id"
76        );
77        assert_eq!(
78            map.get(&CheetahString::from_static_str("offsetMsgId")).unwrap(),
79            "test_offset_msg_id"
80        );
81    }
82
83    #[test]
84    fn check_transaction_state_request_header_deserializes_correctly() {
85        let mut map = HashMap::new();
86        map.insert(
87            CheetahString::from_static_str("topic"),
88            CheetahString::from_static_str("test_topic"),
89        );
90        map.insert(
91            CheetahString::from_static_str("tranStateTableOffset"),
92            CheetahString::from_static_str("123"),
93        );
94        map.insert(
95            CheetahString::from_static_str("commitLogOffset"),
96            CheetahString::from_static_str("456"),
97        );
98        map.insert(
99            CheetahString::from_static_str("msgId"),
100            CheetahString::from_static_str("test_msg_id"),
101        );
102        map.insert(
103            CheetahString::from_static_str("transactionId"),
104            CheetahString::from_static_str("test_transaction_id"),
105        );
106        map.insert(
107            CheetahString::from_static_str("offsetMsgId"),
108            CheetahString::from_static_str("test_offset_msg_id"),
109        );
110
111        let header = <CheckTransactionStateRequestHeader as FromMap>::from(&map).unwrap();
112        assert_eq!(header.topic.unwrap(), "test_topic");
113        assert_eq!(header.tran_state_table_offset, 123);
114        assert_eq!(header.commit_log_offset, 456);
115        assert_eq!(header.msg_id.unwrap(), "test_msg_id");
116        assert_eq!(header.transaction_id.unwrap(), "test_transaction_id");
117        assert_eq!(header.offset_msg_id.unwrap(), "test_offset_msg_id");
118    }
119
120    #[test]
121    fn check_transaction_state_request_header_handles_missing_optional_fields() {
122        let mut map = HashMap::new();
123        map.insert(
124            CheetahString::from_static_str("tranStateTableOffset"),
125            CheetahString::from_static_str("123"),
126        );
127        map.insert(
128            CheetahString::from_static_str("commitLogOffset"),
129            CheetahString::from_static_str("456"),
130        );
131
132        let header = <CheckTransactionStateRequestHeader as FromMap>::from(&map).unwrap();
133        assert!(header.topic.is_none());
134        assert_eq!(header.tran_state_table_offset, 123);
135        assert_eq!(header.commit_log_offset, 456);
136        assert!(header.msg_id.is_none());
137        assert!(header.transaction_id.is_none());
138        assert!(header.offset_msg_id.is_none());
139    }
140
141    #[test]
142    fn check_transaction_state_request_header_handles_invalid_data() {
143        let mut map = HashMap::new();
144        map.insert(
145            CheetahString::from_static_str("tranStateTableOffset"),
146            CheetahString::from_static_str("invalid"),
147        );
148        map.insert(
149            CheetahString::from_static_str("commitLogOffset"),
150            CheetahString::from_static_str("invalid"),
151        );
152
153        let result = <CheckTransactionStateRequestHeader as FromMap>::from(&map);
154        assert!(result.is_err());
155    }
156}