Skip to main content

rocketmq_protocol/protocol/body/
group_list.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 std::collections::HashSet;
16
17use cheetah_string::CheetahString;
18use serde::Deserialize;
19use serde::Serialize;
20
21#[derive(Deserialize, Serialize, Debug, Clone, Default)]
22#[serde(rename_all = "camelCase")]
23pub struct GroupList {
24    #[serde(alias = "group_list")]
25    pub group_list: HashSet<CheetahString>,
26}
27
28impl GroupList {
29    pub fn new(group_list: HashSet<CheetahString>) -> Self {
30        Self { group_list }
31    }
32
33    pub fn get_group_list(&self) -> &HashSet<CheetahString> {
34        &self.group_list
35    }
36
37    pub fn set_group_list(&mut self, group_list: HashSet<CheetahString>) {
38        self.group_list = group_list;
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn group_list_serializes_with_java_field_name() {
48        let group_list = GroupList::new(HashSet::from([CheetahString::from_static_str("consumer_group_a")]));
49
50        let json = serde_json::to_string(&group_list).expect("serialize GroupList");
51
52        assert!(json.contains("\"groupList\""));
53        assert!(!json.contains("\"group_list\""));
54    }
55
56    #[test]
57    fn group_list_deserializes_java_and_legacy_field_names() {
58        let java_json = r#"{"groupList":["consumer_group_a"]}"#;
59        let legacy_json = r#"{"group_list":["consumer_group_b"]}"#;
60
61        let java_group_list: GroupList = serde_json::from_str(java_json).expect("deserialize Java GroupList");
62        let legacy_group_list: GroupList = serde_json::from_str(legacy_json).expect("deserialize legacy GroupList");
63
64        assert!(java_group_list
65            .get_group_list()
66            .contains(&CheetahString::from_static_str("consumer_group_a")));
67        assert!(legacy_group_list
68            .get_group_list()
69            .contains(&CheetahString::from_static_str("consumer_group_b")));
70    }
71}