use std::sync::Arc;
use axum::{Json, extract::State};
use pib_service_api_types::get::ResponseBody;
use pib_service_facade::Service;
use crate::Result;
pub async fn handle(State(service): State<Arc<dyn Service>>) -> Result<Json<ResponseBody>> {
Ok(Json(service.handle_get().await?))
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use crate::Error;
use super::{ResponseBody, handle};
use axum::extract::State;
use pib_service_facade::MockService;
use pretty_assertions::{assert_eq, assert_matches};
#[tokio::test]
async fn valid() {
let mut service = MockService::new();
service
.expect_handle_get()
.times(1)
.return_once(|| Ok(ResponseBody {}));
assert_eq!(
handle(State(Arc::new(service))).await.unwrap().0,
ResponseBody {}
);
}
#[tokio::test]
async fn invalid() {
let mut service = MockService::new();
service
.expect_handle_get()
.times(1)
.return_once(|| Err(pib_service_facade::Error::NotFound));
assert_matches!(
handle(State(Arc::new(service))).await,
Err(Error::Service {
source: pib_service_facade::Error::NotFound
})
);
}
}