Skip to content

Commit

Permalink
Adding test on manager.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ndefokou committed Nov 19, 2024
1 parent 50beae0 commit 9ba0921
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 18 deletions.
1 change: 1 addition & 0 deletions crates/plugin-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use axum::Router;
#[derive(Debug, PartialEq)]
pub enum PluginError {
InitError,
DidcommMessagContainerError,
}

pub trait Plugin: Sync + Send {
Expand Down
17 changes: 8 additions & 9 deletions crates/web-plugins/didcomm-messaging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ keystore.workspace = true
shared.workspace = true
plugin-api.workspace = true
filesystem.workspace = true
forward.workspace = true
pickup.workspace = true
trust-ping.workspace = true
mediator-coordination.workspace = true
forward = { workspace = true, optional = true }
pickup = { workspace = true, optional = true }
trust-ping = { workspace = true, optional = true }
mediator-coordination = { workspace = true, optional = true }
message-api.workspace = true


Expand All @@ -31,11 +31,10 @@ serde = { version = "1.0", features = ["derive"] }


[features]
default = ["routing", "pickup", "trust-ping", "mediator-coordination"]
routing = []
pickup = []
trust-ping = []
mediator-coordination = []
routing = ["dep:forward"]
pickup = ["dep:pickup"]
trust-ping = ["dep:trust-ping"]
mediator-coordination = ["dep:mediator-coordination"]


[dev-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions crates/web-plugins/didcomm-messaging/message-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::{collections::HashMap, sync::Arc};
use std::{collections::HashMap, fmt::Debug, sync::Arc};

use async_trait::async_trait;
use axum::response::Response;
use didcomm::Message;
use shared::state::AppState;

#[async_trait]
pub trait MessageHandler: Send + Sync {
pub trait MessageHandler: Send + Sync + Debug{
async fn handle(&self, state: Arc<AppState>, msg: Message)
-> Result<Option<Message>, Response>;
}

#[derive(Default, Clone)]
#[derive(Default, Clone, Debug)]
pub struct MessageRouter {
handlers: HashMap<String, Arc<dyn MessageHandler>>,
}
Expand Down
125 changes: 125 additions & 0 deletions crates/web-plugins/didcomm-messaging/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,128 @@ impl<'a> MessagePluginContainer<'a> {
}))
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;

// Real plugin implementations
struct FirstPlugin;
impl MessagePlugin for FirstPlugin {
fn name(&self) -> &'static str {
"first"
}

fn didcomm_routes(&self) -> MessageRouter {
MessageRouter::new()
}
}

struct SecondPlugin;
impl MessagePlugin for SecondPlugin {
fn name(&self) -> &'static str {
"second"
}

fn didcomm_routes(&self) -> MessageRouter {
MessageRouter::new()
}
}

#[test]
fn test_loading_plugins() {
let plugins: Vec<Arc<dyn MessagePlugin>> =
vec![Arc::new(FirstPlugin {}), Arc::new(SecondPlugin {})];

let mut container = MessagePluginContainer {
loaded: false,
collected_routes: vec![],
message_plugins: &plugins,
};

assert!(container.load().is_ok());
assert!(container.loaded);

assert_eq!(container.collected_routes.len(), 2);

assert!(container.find_handler("first").is_some());
assert!(container.find_handler("second").is_some());
assert!(container.find_handler("non-existent").is_none());
}

#[test]
fn test_loading_duplicate_plugins() {
let plugins: Vec<Arc<dyn MessagePlugin>> = vec![
Arc::new(FirstPlugin {}),
Arc::new(SecondPlugin {}),
Arc::new(SecondPlugin {}),
];

let mut container = MessagePluginContainer {
loaded: false,
collected_routes: vec![],
message_plugins: &plugins,
};

assert_eq!(
container.load().unwrap_err(),
MessageContainerError::DuplicateEntry
);

assert_eq!(container.collected_routes.len(), 0);
}

#[test]
fn test_double_loading() {
let plugins: Vec<Arc<dyn MessagePlugin>> =
vec![Arc::new(FirstPlugin {}), Arc::new(SecondPlugin {})];

let mut container = MessagePluginContainer {
loaded: false,
collected_routes: vec![],
message_plugins: &plugins,
};

assert!(container.load().is_ok());
assert!(container.load().is_ok());
}

#[test]
fn test_unloading_plugins() {
let plugins: Vec<Arc<dyn MessagePlugin>> =
vec![Arc::new(FirstPlugin {}), Arc::new(SecondPlugin {})];

let mut container = MessagePluginContainer {
loaded: false,
collected_routes: vec![],
message_plugins: &plugins,
};

assert!(container.load().is_ok());
assert_eq!(container.collected_routes.len(), 2);

assert!(container.unload().is_ok());
assert!(!container.loaded);

assert_eq!(container.collected_routes.len(), 0);
}

#[test]
fn test_routes_without_loading() {
let plugins: Vec<Arc<dyn MessagePlugin>> =
vec![Arc::new(FirstPlugin {}), Arc::new(SecondPlugin {})];

let container = MessagePluginContainer {
loaded: false,
collected_routes: vec![],
message_plugins: &plugins,
};

// Attempt to access routes without loading
assert_eq!(
container.didcomm_routes().unwrap_err(),
MessageContainerError::Unloaded
);
}
}
9 changes: 8 additions & 1 deletion crates/web-plugins/didcomm-messaging/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::web;
use crate::{manager::MessagePluginContainer, web};
use axum::Router;
use filesystem::StdFileSystem;
use mongodb::Database;
use plugin_api::PluginError::DidcommMessagContainerError;
use plugin_api::{Plugin, PluginError};
use shared::{
repository::{MongoConnectionRepository, MongoMessagesRepository},
Expand All @@ -23,6 +24,12 @@ struct DidcommMessagingPluginEnv {

/// Loads environment variables required for this plugin
fn load_plugin_env() -> Result<DidcommMessagingPluginEnv, PluginError> {
let mut container = MessagePluginContainer::new();
if container.load().is_err() {
tracing::error!("failed to load DIDComm protocols container");
return Err(DidcommMessagContainerError);
}

let public_domain = std::env::var("SERVER_PUBLIC_DOMAIN").map_err(|_| {
tracing::error!("SERVER_PUBLIC_DOMAIN env variable required");
PluginError::InitError
Expand Down
6 changes: 1 addition & 5 deletions crates/web-plugins/didcomm-messaging/src/web/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ pub(crate) async fn process_didcomm_message(
State(state): State<Arc<AppState>>,
Extension(message): Extension<Message>,
) -> Response {
let mut container = MessagePluginContainer::new();
if container.load().is_err() {
tracing::error!("failed to load DIDComm protocols container");
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
}
let container = MessagePluginContainer::new();

if let Some(handler) = container
.didcomm_routes()
Expand Down

0 comments on commit 9ba0921

Please sign in to comment.