Skip to main content

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