use cheetah_string::CheetahString;
use rocketmq_macros::RequestHeaderCodecV2;
use serde::Deserialize;
use serde::Serialize;
use crate::protocol::header::namesrv::topic_operation_header::TopicRequestHeader;
#[derive(Debug, Serialize, Deserialize, RequestHeaderCodecV2)]
pub struct NotificationRequestHeader {
#[serde(rename = "consumerGroup")]
#[required]
pub consumer_group: CheetahString,
#[serde(rename = "topic")]
#[required]
pub topic: CheetahString,
#[serde(rename = "queueId")]
#[required]
pub queue_id: i32,
#[serde(rename = "pollTime")]
#[required]
pub poll_time: i64,
#[serde(rename = "bornTime")]
#[required]
pub born_time: i64,
#[serde(default)]
pub order: bool,
#[serde(rename = "attemptId", skip_serializing_if = "Option::is_none")]
pub attempt_id: Option<CheetahString>,
#[serde(rename = "expType", skip_serializing_if = "Option::is_none")]
pub exp_type: Option<CheetahString>,
#[serde(rename = "exp", skip_serializing_if = "Option::is_none")]
pub exp: Option<CheetahString>,
#[serde(flatten)]
pub topic_request_header: Option<TopicRequestHeader>,
}
#[cfg(test)]
mod tests {
use serde_json;
use super::*;
#[test]
fn test_notification_request_header_serialization() {
let header = NotificationRequestHeader {
consumer_group: CheetahString::from("consumer_group_1"),
topic: CheetahString::from("test_topic"),
queue_id: 10,
poll_time: 1234567890,
born_time: 1234567891,
order: true,
attempt_id: Some(CheetahString::from("attempt_1")),
exp_type: Some(CheetahString::from("TAG")),
exp: Some(CheetahString::from("tag-a")),
topic_request_header: None,
};
let serialized = serde_json::to_string(&header).expect("Failed to serialize header");
assert!(serialized.contains("\"expType\":\"TAG\""));
assert!(serialized.contains("\"exp\":\"tag-a\""));
let deserialized: NotificationRequestHeader =
serde_json::from_str(&serialized).expect("Failed to deserialize header");
assert_eq!(header.queue_id, deserialized.queue_id);
assert_eq!(deserialized.exp_type.as_deref(), Some("TAG"));
assert_eq!(deserialized.exp.as_deref(), Some("tag-a"));
}
#[test]
fn test_notification_request_header_default_order() {
let header = NotificationRequestHeader {
consumer_group: CheetahString::from("consumer_group_1"),
topic: CheetahString::from("test_topic"),
queue_id: 10,
poll_time: 1234567890,
born_time: 1234567891,
order: false, attempt_id: None,
exp_type: None,
exp: None,
topic_request_header: None,
};
let serialized = serde_json::to_string(&header).expect("Failed to serialize header");
let deserialized: NotificationRequestHeader =
serde_json::from_str(&serialized).expect("Failed to deserialize header");
assert_eq!(header.queue_id, deserialized.queue_id);
}
}