Skip to main content

rocketmq_protocol/protocol/header/
notification_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::RequestHeaderCodec;
16use serde::Deserialize;
17use serde::Serialize;
18
19#[derive(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodec)]
20#[serde(rename_all = "camelCase")]
21#[header(
22    type_id = "rocketmq_protocol::protocol::header::notification_response_header::NotificationResponseHeader",
23    java_class = "org.apache.rocketmq.remoting.protocol.header.NotificationResponseHeader"
24)]
25pub struct NotificationResponseHeader {
26    #[header(required)]
27    pub has_msg: bool,
28
29    #[serde(default)]
30    #[header(default, default_semantic = "literal:false")]
31    pub polling_full: bool,
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn notification_response_header_serializes_java_camel_case_fields() {
40        let header = NotificationResponseHeader {
41            has_msg: true,
42            polling_full: true,
43        };
44
45        let serialized = serde_json::to_string(&header).unwrap();
46
47        assert_eq!(serialized, r#"{"hasMsg":true,"pollingFull":true}"#);
48        assert!(!serialized.contains("has_msg"));
49        assert!(!serialized.contains("polling_full"));
50    }
51
52    #[test]
53    fn notification_response_header_deserializes_java_camel_case_fields() {
54        let json = r#"{"hasMsg":true,"pollingFull":true}"#;
55
56        let header: NotificationResponseHeader = serde_json::from_str(json).unwrap();
57
58        assert!(header.has_msg);
59        assert!(header.polling_full);
60    }
61
62    #[test]
63    fn notification_response_header_defaults_missing_polling_full_like_java() {
64        let json = r#"{"hasMsg":true}"#;
65
66        let header: NotificationResponseHeader = serde_json::from_str(json).unwrap();
67
68        assert!(header.has_msg);
69        assert!(!header.polling_full);
70    }
71}