Skip to main content

rocketmq_protocol/protocol/header/
update_consumer_offset_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
20use crate::protocol::header::message_operation_header::TopicRequestHeaderTrait;
21use crate::protocol::header::namesrv::topic_operation_header::TopicRequestHeader;
22
23#[derive(Debug, Serialize, Deserialize, Default, RequestHeaderCodec)]
24#[header(
25    type_id = "rocketmq_protocol::protocol::header::update_consumer_offset_header::UpdateConsumerOffsetResponseHeader",
26    java_class = "org.apache.rocketmq.remoting.protocol.header.UpdateConsumerOffsetResponseHeader"
27)]
28pub struct UpdateConsumerOffsetResponseHeader {}
29
30#[derive(Debug, Clone, Serialize, Deserialize, Default, RequestHeaderCodec)]
31#[header(
32    type_id = "rocketmq_protocol::protocol::header::update_consumer_offset_header::UpdateConsumerOffsetRequestHeader",
33    java_class = "org.apache.rocketmq.remoting.protocol.header.UpdateConsumerOffsetRequestHeader"
34)]
35#[serde(rename_all = "camelCase")]
36pub struct UpdateConsumerOffsetRequestHeader {
37    #[header(required)]
38    pub consumer_group: CheetahString,
39    #[header(required)]
40    pub topic: CheetahString,
41    #[header(required)]
42    pub queue_id: i32,
43    #[header(required)]
44    pub commit_offset: i64,
45    #[serde(flatten)]
46    #[header(flatten, presence = "always")]
47    pub topic_request_header: Option<TopicRequestHeader>,
48}
49
50impl TopicRequestHeaderTrait for UpdateConsumerOffsetRequestHeader {
51    fn set_lo(&mut self, lo: Option<bool>) {
52        if let Some(header) = self.topic_request_header.as_mut() {
53            header.lo = lo;
54        }
55    }
56
57    fn lo(&self) -> Option<bool> {
58        self.topic_request_header.as_ref().and_then(|h| h.lo)
59    }
60
61    fn set_topic(&mut self, topic: CheetahString) {
62        self.topic = topic;
63    }
64
65    fn topic(&self) -> &CheetahString {
66        &self.topic
67    }
68
69    fn broker_name(&self) -> Option<&CheetahString> {
70        self.topic_request_header
71            .as_ref()
72            .and_then(|h| h.rpc.as_ref())
73            .and_then(|h| h.broker_name.as_ref())
74    }
75
76    fn set_broker_name(&mut self, broker_name: CheetahString) {
77        if let Some(header) = self.topic_request_header.as_mut() {
78            if let Some(rpc_header) = header.rpc.as_mut() {
79                rpc_header.broker_name = Some(broker_name);
80            }
81        }
82    }
83
84    fn namespace(&self) -> Option<&str> {
85        self.topic_request_header
86            .as_ref()
87            .and_then(|h| h.rpc.as_ref())
88            .and_then(|r| r.namespace.as_deref())
89    }
90
91    fn set_namespace(&mut self, namespace: CheetahString) {
92        if let Some(header) = self.topic_request_header.as_mut() {
93            if let Some(rpc_header) = header.rpc.as_mut() {
94                rpc_header.namespace = Some(namespace);
95            }
96        }
97    }
98
99    fn namespaced(&self) -> Option<bool> {
100        self.topic_request_header
101            .as_ref()
102            .and_then(|h| h.rpc.as_ref())
103            .and_then(|r| r.namespaced)
104    }
105
106    fn set_namespaced(&mut self, namespaced: bool) {
107        if let Some(header) = self.topic_request_header.as_mut() {
108            if let Some(rpc_header) = header.rpc.as_mut() {
109                rpc_header.namespaced = Some(namespaced);
110            }
111        }
112    }
113
114    fn oneway(&self) -> Option<bool> {
115        self.topic_request_header
116            .as_ref()
117            .and_then(|h| h.rpc.as_ref())
118            .and_then(|r| r.oneway)
119    }
120
121    fn set_oneway(&mut self, oneway: bool) {
122        if let Some(header) = self.topic_request_header.as_mut() {
123            if let Some(rpc_header) = header.rpc.as_mut() {
124                rpc_header.oneway = Some(oneway);
125            }
126        }
127    }
128
129    fn queue_id(&self) -> i32 {
130        self.queue_id
131    }
132
133    fn set_queue_id(&mut self, queue_id: i32) {
134        self.queue_id = queue_id;
135    }
136}
137
138#[cfg(test)]
139mod tests {
140    use std::collections::HashMap;
141
142    use super::*;
143    use crate::protocol::command_custom_header::{CommandCustomHeader, FromMap};
144    use crate::rpc::rpc_request_header::RpcRequestHeader;
145
146    fn request_with_rpc_envelope() -> UpdateConsumerOffsetRequestHeader {
147        UpdateConsumerOffsetRequestHeader {
148            consumer_group: CheetahString::from("group-a"),
149            topic: CheetahString::from("topic-a"),
150            queue_id: 1,
151            commit_offset: 100,
152            topic_request_header: Some(TopicRequestHeader {
153                lo: None,
154                rpc: Some(RpcRequestHeader::default()),
155            }),
156        }
157    }
158
159    #[test]
160    fn request_header_codec_round_trips_required_fields_and_always_creates_the_rpc_envelope() {
161        let header = UpdateConsumerOffsetRequestHeader {
162            topic_request_header: None,
163            ..request_with_rpc_envelope()
164        };
165        let map = header.to_map().unwrap();
166
167        assert_eq!(map.get("consumerGroup").map(|value| value.as_str()), Some("group-a"));
168        assert_eq!(map.get("topic").map(|value| value.as_str()), Some("topic-a"));
169        assert_eq!(map.get("queueId").map(|value| value.as_str()), Some("1"));
170        assert_eq!(map.get("commitOffset").map(|value| value.as_str()), Some("100"));
171
172        let decoded = <UpdateConsumerOffsetRequestHeader as FromMap>::from(&map).unwrap();
173        assert_eq!(decoded.consumer_group, "group-a");
174        assert_eq!(decoded.topic, "topic-a");
175        assert_eq!(decoded.queue_id, 1);
176        assert_eq!(decoded.commit_offset, 100);
177        assert!(decoded.topic_request_header.is_some());
178    }
179
180    #[test]
181    fn request_header_codec_rejects_missing_or_invalid_required_values() {
182        assert!(<UpdateConsumerOffsetRequestHeader as FromMap>::from(&HashMap::new()).is_err());
183
184        let mut map = request_with_rpc_envelope().to_map().unwrap();
185        map.insert("queueId".into(), "invalid".into());
186        assert!(<UpdateConsumerOffsetRequestHeader as FromMap>::from(&map).is_err());
187    }
188
189    #[test]
190    fn nested_setters_are_noops_without_an_rpc_envelope() {
191        let mut header = UpdateConsumerOffsetRequestHeader {
192            topic_request_header: None,
193            ..request_with_rpc_envelope()
194        };
195
196        header.set_lo(Some(true));
197        header.set_broker_name(CheetahString::from("broker-a"));
198        header.set_namespace(CheetahString::from("namespace-a"));
199        header.set_namespaced(true);
200        header.set_oneway(true);
201
202        assert_eq!(header.lo(), None);
203        assert_eq!(header.broker_name(), None);
204        assert_eq!(header.namespace(), None);
205        assert_eq!(header.namespaced(), None);
206        assert_eq!(header.oneway(), None);
207    }
208
209    #[test]
210    fn empty_response_header_has_empty_serde_and_codec_representations() {
211        let header = UpdateConsumerOffsetResponseHeader::default();
212
213        assert_eq!(serde_json::to_string(&header).unwrap(), "{}");
214        assert!(serde_json::from_str::<UpdateConsumerOffsetResponseHeader>("{}").is_ok());
215        assert!(header.to_map().unwrap().is_empty());
216    }
217}