Skip to main content

rocketmq_remoting/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::RequestHeaderCodecV2;
17use serde::Deserialize;
18use serde::Serialize;
19
20#[derive(Debug, Clone, Serialize, Deserialize, Default, RequestHeaderCodecV2)]
21#[serde(rename_all = "camelCase")]
22pub struct DeleteAclRequestHeader {
23    pub subject: CheetahString,
24
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub policy_type: Option<CheetahString>,
27
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub resource: Option<CheetahString>,
30}
31
32impl DeleteAclRequestHeader {
33    pub fn new(subject: CheetahString, resource: Option<CheetahString>) -> Self {
34        Self {
35            subject,
36            policy_type: None,
37            resource,
38        }
39    }
40
41    pub fn with_policy_type(
42        subject: CheetahString,
43        policy_type: Option<CheetahString>,
44        resource: Option<CheetahString>,
45    ) -> Self {
46        Self {
47            subject,
48            policy_type,
49            resource,
50        }
51    }
52
53    pub fn with_subject(subject: CheetahString) -> Self {
54        Self {
55            subject,
56            policy_type: None,
57            resource: None,
58        }
59    }
60
61    pub fn set_subject(&mut self, subject: CheetahString) {
62        self.subject = subject;
63    }
64
65    pub fn set_policy_type(&mut self, policy_type: Option<CheetahString>) {
66        self.policy_type = policy_type;
67    }
68
69    pub fn set_resource(&mut self, resource: Option<CheetahString>) {
70        self.resource = resource;
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use std::collections::HashMap;
77
78    use super::*;
79    use crate::protocol::command_custom_header::CommandCustomHeader;
80    use crate::protocol::command_custom_header::FromMap;
81
82    #[test]
83    fn delete_acl_request_header_new() {
84        let subject = CheetahString::from_static_str("user:alice");
85        let resource = Some(CheetahString::from_static_str("Topic:test-topic"));
86        let header = DeleteAclRequestHeader::new(subject.clone(), resource.clone());
87
88        assert_eq!(header.subject, subject);
89        assert!(header.policy_type.is_none());
90        assert_eq!(header.resource, resource);
91    }
92
93    #[test]
94    fn delete_acl_request_header_with_policy_type() {
95        let subject = CheetahString::from_static_str("user:alice");
96        let policy_type = Some(CheetahString::from_static_str("Default"));
97        let resource = Some(CheetahString::from_static_str("Topic:test-topic"));
98        let header = DeleteAclRequestHeader::with_policy_type(subject.clone(), policy_type.clone(), resource.clone());
99
100        assert_eq!(header.subject, subject);
101        assert_eq!(header.policy_type, policy_type);
102        assert_eq!(header.resource, resource);
103    }
104
105    #[test]
106    fn delete_acl_request_header_with_subject() {
107        let subject = CheetahString::from_static_str("user:bob");
108        let header = DeleteAclRequestHeader::with_subject(subject.clone());
109
110        assert_eq!(header.subject, subject);
111        assert!(header.resource.is_none());
112    }
113
114    #[test]
115    fn delete_acl_request_header_default() {
116        let header = DeleteAclRequestHeader::default();
117
118        assert!(header.subject.is_empty());
119        assert!(header.policy_type.is_none());
120        assert!(header.resource.is_none());
121    }
122
123    #[test]
124    fn delete_acl_request_header_set_methods() {
125        let mut header = DeleteAclRequestHeader::default();
126        let subject = CheetahString::from_static_str("user:charlie");
127        let policy_type = Some(CheetahString::from_static_str("Custom"));
128        let resource = Some(CheetahString::from_static_str("Group:consumer-group"));
129
130        header.set_subject(subject.clone());
131        header.set_policy_type(policy_type.clone());
132        header.set_resource(resource.clone());
133
134        assert_eq!(header.subject, subject);
135        assert_eq!(header.policy_type, policy_type);
136        assert_eq!(header.resource, resource);
137    }
138
139    #[test]
140    fn delete_acl_request_header_serializes_correctly() {
141        let header = DeleteAclRequestHeader::with_policy_type(
142            CheetahString::from_static_str("user:alice"),
143            Some(CheetahString::from_static_str("Custom")),
144            Some(CheetahString::from_static_str("Topic:test")),
145        );
146
147        let map = header.to_map().unwrap();
148        assert_eq!(
149            map.get(&CheetahString::from_static_str("subject")),
150            Some(&CheetahString::from_static_str("user:alice"))
151        );
152        assert_eq!(
153            map.get(&CheetahString::from_static_str("policyType")),
154            Some(&CheetahString::from_static_str("Custom"))
155        );
156        assert_eq!(
157            map.get(&CheetahString::from_static_str("resource")),
158            Some(&CheetahString::from_static_str("Topic:test"))
159        );
160    }
161
162    #[test]
163    fn delete_acl_request_header_deserializes_correctly() {
164        let mut map: HashMap<CheetahString, CheetahString> = HashMap::new();
165        map.insert(
166            CheetahString::from_static_str("subject"),
167            CheetahString::from_static_str("user:alice"),
168        );
169        map.insert(
170            CheetahString::from_static_str("resource"),
171            CheetahString::from_static_str("Topic:test"),
172        );
173        map.insert(
174            CheetahString::from_static_str("policyType"),
175            CheetahString::from_static_str("Default"),
176        );
177
178        let header = <DeleteAclRequestHeader as FromMap>::from(&map).unwrap();
179        assert_eq!(header.subject, CheetahString::from_static_str("user:alice"));
180        assert_eq!(header.policy_type, Some(CheetahString::from_static_str("Default")));
181        assert_eq!(header.resource, Some(CheetahString::from_static_str("Topic:test")));
182    }
183}