Skip to main content

rocketmq_remoting/protocol/header/
create_user_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 CreateUserRequestHeader {
23    pub username: CheetahString,
24}
25
26impl CreateUserRequestHeader {
27    pub fn new(username: CheetahString) -> Self {
28        Self { username }
29    }
30
31    pub fn set_username(&mut self, username: CheetahString) {
32        self.username = username;
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use std::collections::HashMap;
39
40    use super::*;
41    use crate::protocol::command_custom_header::CommandCustomHeader;
42    use crate::protocol::command_custom_header::FromMap;
43
44    #[test]
45    fn create_user_request_header_new() {
46        let username = CheetahString::from_static_str("test_user");
47        let header = CreateUserRequestHeader::new(username.clone());
48
49        assert_eq!(header.username, username);
50    }
51
52    #[test]
53    fn create_user_request_header_default() {
54        let header = CreateUserRequestHeader::default();
55
56        assert!(header.username.is_empty());
57    }
58
59    #[test]
60    fn create_user_request_header_set_username() {
61        let mut header = CreateUserRequestHeader::default();
62        let username = CheetahString::from_static_str("admin");
63
64        header.set_username(username.clone());
65
66        assert_eq!(header.username, username);
67    }
68
69    #[test]
70    fn create_user_request_header_serializes_correctly() {
71        let header = CreateUserRequestHeader {
72            username: CheetahString::from_static_str("test_admin"),
73        };
74
75        let map = header.to_map().unwrap();
76
77        assert_eq!(
78            map.get(&CheetahString::from_static_str("username")).unwrap(),
79            "test_admin"
80        );
81    }
82
83    #[test]
84    fn create_user_request_header_deserializes_correctly() {
85        let mut map = HashMap::new();
86        map.insert(
87            CheetahString::from_static_str("username"),
88            CheetahString::from_static_str("deserialized_user"),
89        );
90
91        let header = <CreateUserRequestHeader as FromMap>::from(&map).unwrap();
92
93        assert_eq!(header.username.as_str(), "deserialized_user");
94    }
95
96    #[test]
97    fn create_user_request_header_serde_json_roundtrip() {
98        let header = CreateUserRequestHeader {
99            username: CheetahString::from_static_str("json_test_user"),
100        };
101
102        let json = serde_json::to_string(&header).unwrap();
103        let deserialized: CreateUserRequestHeader = serde_json::from_str(&json).unwrap();
104
105        assert_eq!(deserialized.username, header.username);
106    }
107}