|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use rocketmq_macros::RequestHeaderCodecV2; |
| 19 | + |
| 20 | +#[derive( |
| 21 | + Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq, Default, RequestHeaderCodecV2, |
| 22 | +)] |
| 23 | +#[serde(rename_all = "camelCase")] |
| 24 | +pub struct PollingInfoResponseHeader { |
| 25 | + #[required] |
| 26 | + pub polling_num: i32, |
| 27 | +} |
| 28 | + |
| 29 | +#[cfg(test)] |
| 30 | +mod tests { |
| 31 | + use serde_json; |
| 32 | + |
| 33 | + use super::*; |
| 34 | + |
| 35 | + #[test] |
| 36 | + fn test_polling_info_response_header_default() { |
| 37 | + let header = PollingInfoResponseHeader::default(); |
| 38 | + assert_eq!(header.polling_num, 0); |
| 39 | + } |
| 40 | + |
| 41 | + #[test] |
| 42 | + fn test_polling_info_response_header_new_with_positive_value() { |
| 43 | + let header = PollingInfoResponseHeader { polling_num: 100 }; |
| 44 | + assert_eq!(header.polling_num, 100); |
| 45 | + } |
| 46 | + |
| 47 | + #[test] |
| 48 | + fn test_polling_info_response_header_new_with_negative_value() { |
| 49 | + let header = PollingInfoResponseHeader { polling_num: -1 }; |
| 50 | + assert_eq!(header.polling_num, -1); |
| 51 | + } |
| 52 | + |
| 53 | + #[test] |
| 54 | + fn test_polling_info_response_header_new_with_zero() { |
| 55 | + let header = PollingInfoResponseHeader { polling_num: 0 }; |
| 56 | + assert_eq!(header.polling_num, 0); |
| 57 | + } |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn test_polling_info_response_header_new_with_max_value() { |
| 61 | + let header = PollingInfoResponseHeader { |
| 62 | + polling_num: i32::MAX, |
| 63 | + }; |
| 64 | + assert_eq!(header.polling_num, i32::MAX); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_polling_info_response_header_new_with_min_value() { |
| 69 | + let header = PollingInfoResponseHeader { |
| 70 | + polling_num: i32::MIN, |
| 71 | + }; |
| 72 | + assert_eq!(header.polling_num, i32::MIN); |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn test_polling_info_response_header_clone() { |
| 77 | + let header1 = PollingInfoResponseHeader { polling_num: 42 }; |
| 78 | + let header2 = header1.clone(); |
| 79 | + assert_eq!(header1, header2); |
| 80 | + assert_eq!(header2.polling_num, 42); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_polling_info_response_header_equality() { |
| 85 | + let header1 = PollingInfoResponseHeader { polling_num: 100 }; |
| 86 | + let header2 = PollingInfoResponseHeader { polling_num: 100 }; |
| 87 | + let header3 = PollingInfoResponseHeader { polling_num: 200 }; |
| 88 | + |
| 89 | + assert_eq!(header1, header2); |
| 90 | + assert_ne!(header1, header3); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn test_polling_info_response_header_debug() { |
| 95 | + let header = PollingInfoResponseHeader { polling_num: 123 }; |
| 96 | + let debug_str = format!("{:?}", header); |
| 97 | + assert!(debug_str.contains("PollingInfoResponseHeader")); |
| 98 | + assert!(debug_str.contains("123")); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_polling_info_response_header_serialize_to_json() { |
| 103 | + let header = PollingInfoResponseHeader { polling_num: 99 }; |
| 104 | + let json = serde_json::to_string(&header).unwrap(); |
| 105 | + assert_eq!(json, r#"{"pollingNum":99}"#); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_polling_info_response_header_serialize_with_zero() { |
| 110 | + let header = PollingInfoResponseHeader { polling_num: 0 }; |
| 111 | + let json = serde_json::to_string(&header).unwrap(); |
| 112 | + assert_eq!(json, r#"{"pollingNum":0}"#); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn test_polling_info_response_header_serialize_with_negative() { |
| 117 | + let header = PollingInfoResponseHeader { polling_num: -50 }; |
| 118 | + let json = serde_json::to_string(&header).unwrap(); |
| 119 | + assert_eq!(json, r#"{"pollingNum":-50}"#); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn test_polling_info_response_header_deserialize_from_json() { |
| 124 | + let json = r#"{"pollingNum":88}"#; |
| 125 | + let header: PollingInfoResponseHeader = serde_json::from_str(json).unwrap(); |
| 126 | + assert_eq!(header.polling_num, 88); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn test_polling_info_response_header_deserialize_with_zero() { |
| 131 | + let json = r#"{"pollingNum":0}"#; |
| 132 | + let header: PollingInfoResponseHeader = serde_json::from_str(json).unwrap(); |
| 133 | + assert_eq!(header.polling_num, 0); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn test_polling_info_response_header_deserialize_with_negative() { |
| 138 | + let json = r#"{"pollingNum":-100}"#; |
| 139 | + let header: PollingInfoResponseHeader = serde_json::from_str(json).unwrap(); |
| 140 | + assert_eq!(header.polling_num, -100); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn test_polling_info_response_header_serialize_deserialize_roundtrip() { |
| 145 | + let original = PollingInfoResponseHeader { polling_num: 777 }; |
| 146 | + let json = serde_json::to_string(&original).unwrap(); |
| 147 | + let deserialized: PollingInfoResponseHeader = serde_json::from_str(&json).unwrap(); |
| 148 | + assert_eq!(original, deserialized); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn test_polling_info_response_header_serialize_deserialize_max_value() { |
| 153 | + let original = PollingInfoResponseHeader { |
| 154 | + polling_num: i32::MAX, |
| 155 | + }; |
| 156 | + let json = serde_json::to_string(&original).unwrap(); |
| 157 | + let deserialized: PollingInfoResponseHeader = serde_json::from_str(&json).unwrap(); |
| 158 | + assert_eq!(original, deserialized); |
| 159 | + } |
| 160 | + |
| 161 | + #[test] |
| 162 | + fn test_polling_info_response_header_serialize_deserialize_min_value() { |
| 163 | + let original = PollingInfoResponseHeader { |
| 164 | + polling_num: i32::MIN, |
| 165 | + }; |
| 166 | + let json = serde_json::to_string(&original).unwrap(); |
| 167 | + let deserialized: PollingInfoResponseHeader = serde_json::from_str(&json).unwrap(); |
| 168 | + assert_eq!(original, deserialized); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn test_polling_info_response_header_camel_case_field_name() { |
| 173 | + let header = PollingInfoResponseHeader { polling_num: 50 }; |
| 174 | + let json = serde_json::to_string(&header).unwrap(); |
| 175 | + // 验证字段名是 camelCase 格式(pollingNum 而不是 polling_num) |
| 176 | + assert!(json.contains("pollingNum")); |
| 177 | + assert!(!json.contains("polling_num")); |
| 178 | + } |
| 179 | + |
| 180 | + #[test] |
| 181 | + fn test_polling_info_response_header_deserialize_invalid_json() { |
| 182 | + let invalid_json = r#"{"invalidField":123}"#; |
| 183 | + let result: Result<PollingInfoResponseHeader, _> = serde_json::from_str(invalid_json); |
| 184 | + // 应该失败,因为缺少必需的 pollingNum 字段 |
| 185 | + assert!(result.is_err()); |
| 186 | + } |
| 187 | + |
| 188 | + #[test] |
| 189 | + fn test_polling_info_response_header_deserialize_wrong_type() { |
| 190 | + let invalid_json = r#"{"pollingNum":"not_a_number"}"#; |
| 191 | + let result: Result<PollingInfoResponseHeader, _> = serde_json::from_str(invalid_json); |
| 192 | + // 应该失败,因为类型不匹配 |
| 193 | + assert!(result.is_err()); |
| 194 | + } |
| 195 | + |
| 196 | + #[test] |
| 197 | + fn test_polling_info_response_header_multiple_instances() { |
| 198 | + let headers = [ |
| 199 | + PollingInfoResponseHeader { polling_num: 1 }, |
| 200 | + PollingInfoResponseHeader { polling_num: 2 }, |
| 201 | + PollingInfoResponseHeader { polling_num: 3 }, |
| 202 | + ]; |
| 203 | + |
| 204 | + assert_eq!(headers.len(), 3); |
| 205 | + assert_eq!(headers[0].polling_num, 1); |
| 206 | + assert_eq!(headers[1].polling_num, 2); |
| 207 | + assert_eq!(headers[2].polling_num, 3); |
| 208 | + } |
| 209 | + |
| 210 | + #[test] |
| 211 | + fn test_polling_info_response_header_mutation() { |
| 212 | + let mut header = PollingInfoResponseHeader { polling_num: 10 }; |
| 213 | + assert_eq!(header.polling_num, 10); |
| 214 | + |
| 215 | + header.polling_num = 20; |
| 216 | + assert_eq!(header.polling_num, 20); |
| 217 | + |
| 218 | + header.polling_num += 5; |
| 219 | + assert_eq!(header.polling_num, 25); |
| 220 | + } |
| 221 | + |
| 222 | + #[test] |
| 223 | + fn test_polling_info_response_header_pretty_print_json() { |
| 224 | + let header = PollingInfoResponseHeader { polling_num: 456 }; |
| 225 | + let json = serde_json::to_string_pretty(&header).unwrap(); |
| 226 | + assert!(json.contains("pollingNum")); |
| 227 | + assert!(json.contains("456")); |
| 228 | + } |
| 229 | + |
| 230 | + #[test] |
| 231 | + fn test_polling_info_response_header_from_value() { |
| 232 | + let json_value = serde_json::json!({ |
| 233 | + "pollingNum": 888 |
| 234 | + }); |
| 235 | + let header: PollingInfoResponseHeader = serde_json::from_value(json_value).unwrap(); |
| 236 | + assert_eq!(header.polling_num, 888); |
| 237 | + } |
| 238 | + |
| 239 | + #[test] |
| 240 | + fn test_polling_info_response_header_to_value() { |
| 241 | + let header = PollingInfoResponseHeader { polling_num: 999 }; |
| 242 | + let json_value = serde_json::to_value(&header).unwrap(); |
| 243 | + assert_eq!(json_value["pollingNum"], 999); |
| 244 | + } |
| 245 | + |
| 246 | + #[test] |
| 247 | + fn test_polling_info_response_header_typical_use_cases() { |
| 248 | + // 测试典型的轮询场景 |
| 249 | + let no_polling = PollingInfoResponseHeader { polling_num: 0 }; |
| 250 | + let low_polling = PollingInfoResponseHeader { polling_num: 5 }; |
| 251 | + let medium_polling = PollingInfoResponseHeader { polling_num: 50 }; |
| 252 | + let high_polling = PollingInfoResponseHeader { polling_num: 500 }; |
| 253 | + |
| 254 | + assert_eq!(no_polling.polling_num, 0); |
| 255 | + assert_eq!(low_polling.polling_num, 5); |
| 256 | + assert_eq!(medium_polling.polling_num, 50); |
| 257 | + assert_eq!(high_polling.polling_num, 500); |
| 258 | + } |
| 259 | +} |
0 commit comments