#[cfg(test)]
#[macro_use]
extern crate mockall;
#[cfg(test)]
mod tests {
use std::io::{Error, ErrorKind};
use new_home_application::application::application::ApplicationInfo;
use new_home_application::application::application_framework::*;
use new_home_application::communication::communication_manager::*;
use new_home_application::method::method_manager::*;
mock! {
TestFramework {}
trait ApplicationFramework {
fn get_application_port(&self) -> i16;
fn get_application_ip(&self) -> String;
fn get_application_info(&self) -> ApplicationInfo;
fn setup_method_manager(&self, method_manager: &mut Box<dyn MethodManager>);
}
}
mock! {
TestCommunicationManager {}
trait CommunicationManager {
fn run(&mut self);
fn handle_client(&self) -> Result<(), Error>;
}
}
fn get_test_application_framework() -> MockTestFramework {
let mut application_framework = MockTestFramework::new();
application_framework.expect_get_application_port()
.times(1)
.returning(|| 4221);
application_framework.expect_get_application_ip()
.times(1)
.returning(|| String::from("localhost"));
application_framework.expect_get_application_info()
.times(1)
.returning(|| {
ApplicationInfo {
name: String::from("test_app"),
description: String::from("test_description"),
authors: String::from("test_author"),
version: String::from("test_version"),
}
});
application_framework
}
#[test]
fn test_application_call_framework() {
let mut framework = get_test_application_framework();
framework.expect_setup_method_manager()
.times(1)
.returning(|_| ());
let mut app = Application::new(Box::new(framework));
assert!(app.boot().is_ok());
}
#[test]
fn test_application_single_boot_only() {
let mut framework = get_test_application_framework();
framework.expect_setup_method_manager()
.never()
.returning(|_| ());
let mut communication_manager = MockTestCommunicationManager::new();
communication_manager.expect_run()
.times(1)
.returning(|| ());
let mut app = Application::new(Box::new(framework));
app.communication_manager = Some(Box::new(communication_manager));
assert!(app.boot().is_ok());
assert!(app.boot().is_err());
}
#[test]
fn test_application_run() {
let framework = MockTestFramework::new();
let mut communication_manager = MockTestCommunicationManager::new();
communication_manager.expect_handle_client()
.times(1)
.returning(|| Err(Error::new(ErrorKind::Other, "This is just a mock")));
let mut app = Application::new(Box::new(framework));
app.communication_manager = Some(Box::new(communication_manager));
assert!(app.run().is_err());
}
#[test]
fn test_application_multi_run() {
let framework = MockTestFramework::new();
let mut communication_manager = MockTestCommunicationManager::new();
communication_manager.expect_handle_client()
.times(4)
.returning(|| Err(Error::new(ErrorKind::Other, "This is just a mock")));
let mut app = Application::new(Box::new(framework));
app.communication_manager = Some(Box::new(communication_manager));
assert!(app.multi_run(4).is_ok());
}
}