Skip to main content

rocketmq_protocol/protocol/header/
delete_acl_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
20#[derive(Debug, Clone, Serialize, Deserialize, Default, RequestHeaderCodec)]
21#[serde(rename_all = "camelCase")]
22#[header(
23    type_id = "rocketmq_protocol::protocol::header::delete_acl_request_header::DeleteAclRequestHeader",
24    java_class = "org.apache.rocketmq.remoting.protocol.header.DeleteAclRequestHeader"
25)]
26pub struct DeleteAclRequestHeader {
27    #[header(default, default_semantic = "literal:")]
28    pub subject: CheetahString,
29
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub policy_type: Option<CheetahString>,
32
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub resource: Option<CheetahString>,
35}
36
37impl DeleteAclRequestHeader {
38    pub fn new(subject: CheetahString, resource: Option<CheetahString>) -> Self {
39        Self {
40            subject,
41            policy_type: None,
42            resource,
43        }
44    }
45
46    pub fn with_policy_type(
47        subject: CheetahString,
48        policy_type: Option<CheetahString>,
49        resource: Option<CheetahString>,
50    ) -> Self {
51        Self {
52            subject,
53            policy_type,
54            resource,
55        }
56    }
57
58    pub fn with_subject(subject: CheetahString) -> Self {
59        Self {
60            subject,
61            policy_type: None,
62            resource: None,
63        }
64    }
65
66    pub fn set_subject(&mut self, subject: CheetahString) {
67        self.subject = subject;
68    }
69
70    pub fn set_policy_type(&mut self, policy_type: Option<CheetahString>) {
71        self.policy_type = policy_type;
72    }
73
74    pub fn set_resource(&mut self, resource: Option<CheetahString>) {
75        self.resource = resource;
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use std::collections::HashMap;
82
83    use super::*;
84    use crate::protocol::command_custom_header::CommandCustomHeader;
85    use crate::protocol::command_custom_header::FromMap;
86
87    #[test]
88    fn delete_acl_request_header_serializes_correctly() {
89        let header = DeleteAclRequestHeader::with_policy_type(
90            CheetahString::from_static_str("user:alice"),
91            Some(CheetahString::from_static_str("Custom")),
92            Some(CheetahString::from_static_str("Topic:test")),
93        );
94
95        let map = header.to_map().unwrap();
96        assert_eq!(
97            map.get(&CheetahString::from_static_str("subject")),
98            Some(&CheetahString::from_static_str("user:alice"))
99        );
100        assert_eq!(
101            map.get(&CheetahString::from_static_str("policyType")),
102            Some(&CheetahString::from_static_str("Custom"))
103        );
104        assert_eq!(
105            map.get(&CheetahString::from_static_str("resource")),
106            Some(&CheetahString::from_static_str("Topic:test"))
107        );
108    }
109
110    #[test]
111    fn delete_acl_request_header_deserializes_correctly() {
112        let mut map: HashMap<CheetahString, CheetahString> = HashMap::new();
113        map.insert(
114            CheetahString::from_static_str("subject"),
115            CheetahString::from_static_str("user:alice"),
116        );
117        map.insert(
118            CheetahString::from_static_str("resource"),
119            CheetahString::from_static_str("Topic:test"),
120        );
121        map.insert(
122            CheetahString::from_static_str("policyType"),
123            CheetahString::from_static_str("Default"),
124        );
125
126        let header = <DeleteAclRequestHeader as FromMap>::from(&map).unwrap();
127        assert_eq!(header.subject, CheetahString::from_static_str("user:alice"));
128        assert_eq!(header.policy_type, Some(CheetahString::from_static_str("Default")));
129        assert_eq!(header.resource, Some(CheetahString::from_static_str("Topic:test")));
130    }
131}