Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #1516]♻️Refactor GetKVListByNamespaceRequestHeader with derive marco RequestHeaderCodec #2165

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 115 additions & 31 deletions rocketmq-remoting/src/protocol/header/namesrv/kv_config_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@
* limitations under the License.
*/

use std::collections::HashMap;

use cheetah_string::CheetahString;
use rocketmq_macros::RequestHeaderCodec;
use serde::Deserialize;
use serde::Serialize;

use crate::protocol::command_custom_header::CommandCustomHeader;
use crate::protocol::command_custom_header::FromMap;

#[derive(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodec)]
pub struct PutKVConfigRequestHeader {
#[required]
Expand Down Expand Up @@ -106,45 +101,134 @@
}
}

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
#[derive(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodec)]

Check warning on line 104 in rocketmq-remoting/src/protocol/header/namesrv/kv_config_header.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-remoting/src/protocol/header/namesrv/kv_config_header.rs#L104

Added line #L104 was not covered by tests
pub struct GetKVListByNamespaceRequestHeader {
#[required]
pub namespace: CheetahString,
}

impl GetKVListByNamespaceRequestHeader {
const NAMESPACE: &'static str = "namespace";

pub fn new(namespace: impl Into<CheetahString>) -> Self {
Self {
namespace: namespace.into(),
}
}
}

impl CommandCustomHeader for GetKVListByNamespaceRequestHeader {
fn to_map(&self) -> Option<HashMap<CheetahString, CheetahString>> {
Some(HashMap::from([(
CheetahString::from_static_str(GetKVListByNamespaceRequestHeader::NAMESPACE),
self.namespace.clone(),
)]))
#[cfg(test)]
mod tests {
use cheetah_string::CheetahString;

use super::*;

#[test]
fn put_kv_config_request_header_new() {
let header = PutKVConfigRequestHeader::new("namespace1", "key1", "value1");
assert_eq!(header.namespace, CheetahString::from("namespace1"));
assert_eq!(header.key, CheetahString::from("key1"));
assert_eq!(header.value, CheetahString::from("value1"));
}

#[test]
fn put_kv_config_request_header_serialization() {
let header = PutKVConfigRequestHeader::new("namespace1", "key1", "value1");
let serialized = serde_json::to_string(&header).unwrap();
assert_eq!(
serialized,
r#"{"namespace":"namespace1","key":"key1","value":"value1"}"#
);
}

#[test]
fn put_kv_config_request_header_deserialization() {
let json = r#"{"namespace":"namespace1","key":"key1","value":"value1"}"#;
let deserialized: PutKVConfigRequestHeader = serde_json::from_str(json).unwrap();
assert_eq!(deserialized.namespace, CheetahString::from("namespace1"));
assert_eq!(deserialized.key, CheetahString::from("key1"));
assert_eq!(deserialized.value, CheetahString::from("value1"));
}

#[test]
fn get_kv_config_request_header_new() {
let header = GetKVConfigRequestHeader::new("namespace1", "key1");
assert_eq!(header.namespace, CheetahString::from("namespace1"));
assert_eq!(header.key, CheetahString::from("key1"));
}

#[test]
fn get_kv_config_request_header_serialization() {
let header = GetKVConfigRequestHeader::new("namespace1", "key1");
let serialized = serde_json::to_string(&header).unwrap();
assert_eq!(serialized, r#"{"namespace":"namespace1","key":"key1"}"#);
}

#[test]
fn get_kv_config_request_header_deserialization() {
let json = r#"{"namespace":"namespace1","key":"key1"}"#;
let deserialized: GetKVConfigRequestHeader = serde_json::from_str(json).unwrap();
assert_eq!(deserialized.namespace, CheetahString::from("namespace1"));
assert_eq!(deserialized.key, CheetahString::from("key1"));
}

#[test]
fn get_kv_config_response_header_new() {
let header = GetKVConfigResponseHeader::new(Some(CheetahString::from("value1")));
assert_eq!(header.value, Some(CheetahString::from("value1")));
}

#[test]
fn get_kv_config_response_header_serialization() {
let header = GetKVConfigResponseHeader::new(Some(CheetahString::from("value1")));
let serialized = serde_json::to_string(&header).unwrap();
assert_eq!(serialized, r#"{"value":"value1"}"#);
}

#[test]
fn get_kv_config_response_header_deserialization() {
let json = r#"{"value":"value1"}"#;
let deserialized: GetKVConfigResponseHeader = serde_json::from_str(json).unwrap();
assert_eq!(deserialized.value, Some(CheetahString::from("value1")));
}

#[test]
fn delete_kv_config_request_header_new() {
let header = DeleteKVConfigRequestHeader::new("namespace1", "key1");
assert_eq!(header.namespace, CheetahString::from("namespace1"));
assert_eq!(header.key, CheetahString::from("key1"));
}

#[test]
fn delete_kv_config_request_header_serialization() {
let header = DeleteKVConfigRequestHeader::new("namespace1", "key1");
let serialized = serde_json::to_string(&header).unwrap();
assert_eq!(serialized, r#"{"namespace":"namespace1","key":"key1"}"#);
}

#[test]
fn delete_kv_config_request_header_deserialization() {
let json = r#"{"namespace":"namespace1","key":"key1"}"#;
let deserialized: DeleteKVConfigRequestHeader = serde_json::from_str(json).unwrap();
assert_eq!(deserialized.namespace, CheetahString::from("namespace1"));
assert_eq!(deserialized.key, CheetahString::from("key1"));
}

#[test]
fn get_kv_list_by_namespace_request_header_new() {
let header = GetKVListByNamespaceRequestHeader::new("namespace1");
assert_eq!(header.namespace, CheetahString::from("namespace1"));
}

#[test]
fn get_kv_list_by_namespace_request_header_serialization() {
let header = GetKVListByNamespaceRequestHeader::new("namespace1");
let serialized = serde_json::to_string(&header).unwrap();
assert_eq!(serialized, r#"{"namespace":"namespace1"}"#);
}
}

impl FromMap for GetKVListByNamespaceRequestHeader {
type Error = crate::remoting_error::RemotingError;

type Target = GetKVListByNamespaceRequestHeader;

fn from(map: &HashMap<CheetahString, CheetahString>) -> Result<Self::Target, Self::Error> {
Ok(GetKVListByNamespaceRequestHeader {
namespace: map
.get(&CheetahString::from_static_str(
GetKVListByNamespaceRequestHeader::NAMESPACE,
))
.cloned()
.ok_or(Self::Error::RemotingCommandError(
"Miss namespace field".to_string(),
))?,
})
#[test]
fn get_kv_list_by_namespace_request_header_deserialization() {
let json = r#"{"namespace":"namespace1"}"#;
let deserialized: GetKVListByNamespaceRequestHeader = serde_json::from_str(json).unwrap();
assert_eq!(deserialized.namespace, CheetahString::from("namespace1"));
}
}
Loading