diff --git a/components/fms-server/src/lib.rs b/components/fms-server/src/lib.rs index 043a6b2..c379e60 100644 --- a/components/fms-server/src/lib.rs +++ b/components/fms-server/src/lib.rs @@ -21,7 +21,7 @@ use axum::http::StatusCode; use axum::{routing::get, Json, Router}; use axum::extract::{Query, State}; -use clap::{Command, ArgMatches}; +use clap::{ArgMatches, Command}; use log::{error, info}; use serde_json::json; @@ -37,9 +37,13 @@ mod influx_reader; mod models; mod query_parser; +use models::position::{ + VehiclePositionResponseObject, VehiclePositionResponseObjectVehiclePositionResponse, +}; +use models::status::{ + VehicleStatusResponseObject, VehicleStatusResponseObjectVehicleStatusResponse, +}; use query_parser::parse_query_parameters; -use models::position::{VehiclePositionResponseObject, VehiclePositionResponseObjectVehiclePositionResponse}; -use models::status::{VehicleStatusResponseObjectVehicleStatusResponse, VehicleStatusResponseObject}; pub fn app(args: ArgMatches) -> Router { let influx_reader = InfluxReader::new(&args).map_or_else( @@ -77,10 +81,9 @@ async fn get_vehicleposition( .await .map(|positions| { let result = json!(VehiclePositionResponseObject { - vehicle_position_response: - VehiclePositionResponseObjectVehiclePositionResponse { - vehicle_positions: Some(positions) - }, + vehicle_position_response: VehiclePositionResponseObjectVehiclePositionResponse { + vehicle_positions: Some(positions) + }, more_data_available: false, more_data_available_link: None, request_server_date_time: chrono::Utc::now() @@ -146,4 +149,4 @@ async fn get_vehiclesstatuses( error!("error retrieving vehicle statuses: {e}"); StatusCode::INTERNAL_SERVER_ERROR }) - } +} diff --git a/components/fms-server/src/main.rs b/components/fms-server/src/main.rs index 7c0a3c8..ac31b96 100644 --- a/components/fms-server/src/main.rs +++ b/components/fms-server/src/main.rs @@ -29,5 +29,7 @@ async fn main() { let args = parser.get_matches(); let router = fms_server::app(args); let listener = tokio::net::TcpListener::bind("0.0.0.0:8081").await.unwrap(); - axum::serve::serve(listener, router.into_make_service()).await.unwrap(); + axum::serve::serve(listener, router.into_make_service()) + .await + .unwrap(); } diff --git a/components/fms-server/src/query_parser.rs b/components/fms-server/src/query_parser.rs index c9c1742..7ead4ea 100644 --- a/components/fms-server/src/query_parser.rs +++ b/components/fms-server/src/query_parser.rs @@ -29,8 +29,9 @@ const LATEST_ONLY_QUERY: &str = "latestOnly"; const START_TIME_QUERY: &str = "starttime"; const STOP_TIME_QUERY: &str = "stoptime"; const VIN_QUERY: &str = "vin"; -const TRIGGER_FILTER_QUERY: &str = "triggerFiler"; +const TRIGGER_FILTER_QUERY: &str = "triggerFilter"; +#[derive(Debug)] pub struct QueryParameters { pub start_time: i64, pub stop_time: i64, @@ -98,3 +99,133 @@ fn parse_time(params: &HashMap, key: &str) -> Result } Ok(None) } + +#[cfg(test)] +mod tests { + use crate::query_parser::parse_query_parameters; + use axum::http::StatusCode; + use std::collections::HashMap; + + #[test] + fn no_parameters_returns_400() { + let parameters: HashMap = vec![].into_iter().collect(); + let result = parse_query_parameters(¶meters); + assert!(result.is_err(), "Expected Err but got Ok"); + assert_eq!(result.unwrap_err(), StatusCode::BAD_REQUEST); + } + + #[test] + fn no_latestonly_or_time_returns_400() { + let parameters: HashMap = vec![(String::from("vin"), String::from("1234"))] + .into_iter() + .collect(); + let result = parse_query_parameters(¶meters); + assert!(result.is_err(), "Expected Err but got Ok"); + assert_eq!(result.unwrap_err(), StatusCode::BAD_REQUEST); + } + + #[test] + fn latestonly_and_starttime_returns_400() { + let parameters: HashMap = vec![ + (String::from("latestOnly"), String::from("true")), + ( + String::from("starttime"), + String::from("2022-12-15T15:12:00Z"), + ), + ] + .into_iter() + .collect(); + let result = parse_query_parameters(¶meters); + assert!(result.is_err(), "Expected Err but got Ok"); + assert_eq!(result.unwrap_err(), StatusCode::BAD_REQUEST); + } + + #[test] + fn latestonly_and_stoptime_returns_400() { + let parameters: HashMap = vec![ + (String::from("latestOnly"), String::from("false")), + ( + String::from("stoptime"), + String::from("2022-12-15T15:12:00Z"), + ), + ] + .into_iter() + .collect(); + let result = parse_query_parameters(¶meters); + assert!(result.is_err()); + assert_eq!(result.unwrap_err(), StatusCode::BAD_REQUEST); + } + + #[test] + fn latestonly_but_not_bool_returns_400() { + let parameters: HashMap = + vec![(String::from("latestOnly"), String::from("123"))] + .into_iter() + .collect(); + let result = parse_query_parameters(¶meters); + assert!(result.is_err()); + assert_eq!(result.unwrap_err(), StatusCode::BAD_REQUEST); + } + + #[test] + fn startime_but_not_timestamp_returns_400() { + let parameters: HashMap = + vec![(String::from("starttime"), String::from("Random"))] + .into_iter() + .collect(); + let result = parse_query_parameters(¶meters); + assert!(result.is_err(), "Expected Err but got Ok"); + assert_eq!(result.unwrap_err(), StatusCode::BAD_REQUEST); + } + + #[test] + fn with_latestonly_works() { + let parameters: HashMap = vec![ + (String::from("latestOnly"), String::from("false")), + (String::from("vin"), String::from("WDD169005-1J-236684")), + (String::from("triggerFilter"), String::from("TIMER")), + ] + .into_iter() + .collect(); + let result = parse_query_parameters(¶meters); + assert!(result.is_ok(), "Expected Ok but got Err"); + let query_result = result.unwrap(); + assert_eq!( + query_result + .latest_only + .unwrap_or_else(|| panic!("No latestOnly present")), + false + ); + assert_eq!( + query_result.vin.unwrap_or_else(|| panic!("No vin present")), + "WDD169005-1J-236684" + ); + assert_eq!( + query_result + .trigger_filter + .unwrap_or_else(|| panic!("No trigger filter present")), + "TIMER" + ); + } + + #[test] + fn with_starttime_works() { + let parameters: HashMap = vec![ + ( + String::from("starttime"), + String::from("2022-12-15T15:12:00Z"), + ), + ( + String::from("stoptime"), + String::from("2022-12-15T15:13:00Z"), + ), + ] + .into_iter() + .collect(); + let result = parse_query_parameters(¶meters); + assert!(result.is_ok(), "Expected Ok but got Err"); + let query_result = result.unwrap(); + assert_eq!(query_result.start_time, 1671117120); + assert_eq!(query_result.stop_time, 1671117180); + } +}