|
18 | 18 | use std::collections::HashSet; |
19 | 19 | use std::sync::Mutex; |
20 | 20 |
|
| 21 | +use cheetah_string::CheetahString; |
21 | 22 | use lazy_static::lazy_static; |
22 | 23 |
|
23 | 24 | pub const TOPIC_MAX_LENGTH: usize = 127; |
@@ -97,35 +98,35 @@ impl TopicValidator { |
97 | 98 |
|
98 | 99 | pub fn validate_topic(topic: &str) -> ValidateTopicResult { |
99 | 100 | if topic.trim().is_empty() { |
| 101 | + const REMARK: &str = "The specified topic is blank."; |
100 | 102 | return ValidateTopicResult { |
101 | 103 | valid: false, |
102 | | - remark: String::from("The specified topic is blank."), |
| 104 | + remark: CheetahString::from_static_str(REMARK), |
103 | 105 | }; |
104 | 106 | } |
105 | 107 |
|
106 | 108 | if Self::is_topic_or_group_illegal(topic) { |
| 109 | + const REMARK: &str = |
| 110 | + "The specified topic contains illegal characters, allowing only ^[%|a-zA-Z0-9_-]+$"; |
107 | 111 | return ValidateTopicResult { |
108 | 112 | valid: false, |
109 | | - remark: String::from( |
110 | | - "The specified topic contains illegal characters, allowing only \ |
111 | | - ^[%|a-zA-Z0-9_-]+$", |
112 | | - ), |
| 113 | + remark: CheetahString::from_static_str(REMARK), |
113 | 114 | }; |
114 | 115 | } |
115 | 116 |
|
116 | 117 | if topic.len() > TOPIC_MAX_LENGTH { |
117 | 118 | return ValidateTopicResult { |
118 | 119 | valid: false, |
119 | | - remark: format!( |
| 120 | + remark: CheetahString::from(format!( |
120 | 121 | "The specified topic is longer than topic max length {}.", |
121 | 122 | TOPIC_MAX_LENGTH |
122 | | - ), |
| 123 | + )), |
123 | 124 | }; |
124 | 125 | } |
125 | 126 |
|
126 | 127 | ValidateTopicResult { |
127 | 128 | valid: true, |
128 | | - remark: String::new(), |
| 129 | + remark: CheetahString::empty(), |
129 | 130 | } |
130 | 131 | } |
131 | 132 |
|
@@ -155,21 +156,105 @@ impl TopicValidator { |
155 | 156 |
|
156 | 157 | pub struct ValidateTopicResult { |
157 | 158 | valid: bool, |
158 | | - remark: String, |
| 159 | + remark: CheetahString, |
159 | 160 | } |
160 | 161 |
|
161 | 162 | impl ValidateTopicResult { |
162 | 163 | pub fn valid(&self) -> bool { |
163 | 164 | self.valid |
164 | 165 | } |
165 | | - pub fn remark(&self) -> &str { |
| 166 | + pub fn remark(&self) -> &CheetahString { |
166 | 167 | &self.remark |
167 | 168 | } |
168 | 169 |
|
169 | 170 | pub fn set_valid(&mut self, valid: bool) { |
170 | 171 | self.valid = valid; |
171 | 172 | } |
172 | | - pub fn set_remark(&mut self, remark: String) { |
| 173 | + pub fn set_remark(&mut self, remark: CheetahString) { |
173 | 174 | self.remark = remark; |
174 | 175 | } |
175 | 176 | } |
| 177 | + |
| 178 | +#[cfg(test)] |
| 179 | +mod tests { |
| 180 | + use super::*; |
| 181 | + #[test] |
| 182 | + fn validate_topic_with_valid_topic() { |
| 183 | + let result = TopicValidator::validate_topic("valid_topic"); |
| 184 | + assert!(result.valid()); |
| 185 | + assert_eq!(result.remark(), ""); |
| 186 | + } |
| 187 | + |
| 188 | + #[test] |
| 189 | + fn validate_topic_with_empty_topic() { |
| 190 | + let result = TopicValidator::validate_topic(""); |
| 191 | + assert!(!result.valid()); |
| 192 | + assert_eq!(result.remark(), "The specified topic is blank."); |
| 193 | + } |
| 194 | + |
| 195 | + #[test] |
| 196 | + fn validate_topic_with_illegal_characters() { |
| 197 | + let result = TopicValidator::validate_topic("invalid@topic"); |
| 198 | + assert!(!result.valid()); |
| 199 | + assert_eq!( |
| 200 | + result.remark(), |
| 201 | + "The specified topic contains illegal characters, allowing only ^[%|a-zA-Z0-9_-]+$" |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | + #[test] |
| 206 | + fn validate_topic_with_exceeding_length() { |
| 207 | + let long_topic = "a".repeat(TOPIC_MAX_LENGTH + 1); |
| 208 | + let result = TopicValidator::validate_topic(&long_topic); |
| 209 | + assert!(!result.valid()); |
| 210 | + assert_eq!( |
| 211 | + result.remark().as_str(), |
| 212 | + format!( |
| 213 | + "The specified topic is longer than topic max length {}.", |
| 214 | + TOPIC_MAX_LENGTH |
| 215 | + ) |
| 216 | + ); |
| 217 | + } |
| 218 | + |
| 219 | + #[test] |
| 220 | + fn is_system_topic_with_system_topic() { |
| 221 | + assert!(TopicValidator::is_system_topic( |
| 222 | + TopicValidator::RMQ_SYS_SCHEDULE_TOPIC |
| 223 | + )); |
| 224 | + } |
| 225 | + |
| 226 | + #[test] |
| 227 | + fn is_system_topic_with_non_system_topic() { |
| 228 | + assert!(!TopicValidator::is_system_topic("non_system_topic")); |
| 229 | + } |
| 230 | + |
| 231 | + #[test] |
| 232 | + fn is_not_allowed_send_topic_with_not_allowed_topic() { |
| 233 | + assert!(TopicValidator::is_not_allowed_send_topic( |
| 234 | + TopicValidator::RMQ_SYS_SCHEDULE_TOPIC |
| 235 | + )); |
| 236 | + } |
| 237 | + |
| 238 | + #[test] |
| 239 | + fn is_not_allowed_send_topic_with_allowed_topic() { |
| 240 | + assert!(!TopicValidator::is_not_allowed_send_topic("allowed_topic")); |
| 241 | + } |
| 242 | + |
| 243 | + #[test] |
| 244 | + fn add_system_topic_adds_new_topic() { |
| 245 | + TopicValidator::add_system_topic("new_system_topic"); |
| 246 | + assert!(TopicValidator::is_system_topic("new_system_topic")); |
| 247 | + } |
| 248 | + |
| 249 | + #[test] |
| 250 | + fn get_system_topic_set_returns_all_system_topics() { |
| 251 | + let system_topics = TopicValidator::get_system_topic_set(); |
| 252 | + assert!(system_topics.contains(TopicValidator::RMQ_SYS_SCHEDULE_TOPIC)); |
| 253 | + } |
| 254 | + |
| 255 | + #[test] |
| 256 | + fn get_not_allowed_send_topic_set_returns_all_not_allowed_topics() { |
| 257 | + let not_allowed_topics = TopicValidator::get_not_allowed_send_topic_set(); |
| 258 | + assert!(not_allowed_topics.contains(TopicValidator::RMQ_SYS_SCHEDULE_TOPIC)); |
| 259 | + } |
| 260 | +} |
0 commit comments