diff --git a/opendut-carl/src/actions/peers/get_peer_state.rs b/opendut-carl/src/actions/peers/get_peer_state.rs index 043be6095..0083de587 100644 --- a/opendut-carl/src/actions/peers/get_peer_state.rs +++ b/opendut-carl/src/actions/peers/get_peer_state.rs @@ -1,5 +1,5 @@ -use crate::resources::manager::{ResourcesManagerRef}; -use opendut_carl_api::carl::peer::{GetPeerStateError}; +use crate::resources::manager::ResourcesManagerRef; +use opendut_carl_api::carl::peer::GetPeerStateError; use opendut_types::peer::state::PeerState; use opendut_types::peer::{PeerDescriptor, PeerId}; use tracing::{debug, error, info}; @@ -21,12 +21,13 @@ pub async fn get_peer_state(params: GetPeerStateParams) -> Result(peer_id) - .map_err(|cause| GetPeerStateError::Internal { peer_id ,cause: cause.to_string() })?; + .map_err(|cause| GetPeerStateError::Internal { peer_id, cause: cause.to_string() })?; match peer_state { Some(peer_state) => { Ok(peer_state) } None => { - match resources.get::(peer_id) - .map_err(|cause| GetPeerStateError::Internal { peer_id ,cause: cause.to_string() })? { + let peer_descriptor = resources.get::(peer_id) + .map_err(|cause| GetPeerStateError::Internal { peer_id, cause: cause.to_string() })?; + match peer_descriptor { Some(_) => { Ok(PeerState::Down) } None => { Err(GetPeerStateError::PeerNotFound { peer_id }) } } @@ -45,45 +46,68 @@ pub async fn get_peer_state(params: GetPeerStateParams) -> Result anyhow::Result<()> { - + async fn should_get_peer_state_down_in_memory(fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> { let resources_manager = ResourcesManager::new_in_memory(); + should_get_peer_state(resources_manager, fixture, store_peer_descriptor_options).await + } + + #[test_with::no_env(SKIP_DATABASE_CONTAINER_TESTS)] + #[rstest] + #[tokio::test] + async fn should_get_peer_state_down_in_database(fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> { + let db = crate::persistence::database::testing::spawn_and_connect_resources_manager().await?; + should_get_peer_state(db.resources_manager, fixture, store_peer_descriptor_options).await + } + async fn should_get_peer_state(resources_manager: ResourcesManagerRef, fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> { actions::store_peer_descriptor(StorePeerDescriptorParams { resources_manager: Arc::clone(&resources_manager), vpn: fixture.vpn, peer_descriptor: fixture.peer_a_descriptor, options: store_peer_descriptor_options, }).await?; - - let peer_state = actions::get_peer_state(GetPeerStateParams { + + assert_that!(resources_manager.get::(fixture.peer_a_id).await?.as_ref(), none()); + + let peer_state = get_peer_state(GetPeerStateParams { peer: fixture.peer_a_id, resources_manager: Clone::clone(&resources_manager), }).await?; - + assert_that!(peer_state, eq(PeerState::Down)); + assert_that!(resources_manager.get::(fixture.peer_a_id).await?.as_ref(), none()); Ok(()) } #[rstest] #[tokio::test] - async fn should_not_find_peer_for_id(fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> { - + async fn should_throw_error_if_peer_not_found_in_memory(fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> { let resources_manager = ResourcesManager::new_in_memory(); - + should_throw_error_if_peer_not_found(resources_manager, fixture, store_peer_descriptor_options).await + } + + #[test_with::no_env(SKIP_DATABASE_CONTAINER_TESTS)] + #[rstest] + #[tokio::test] + async fn should_throw_error_if_peer_not_found_in_database(fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> { + let db = crate::persistence::database::testing::spawn_and_connect_resources_manager().await?; + should_throw_error_if_peer_not_found(db.resources_manager, fixture, store_peer_descriptor_options).await + } + + async fn should_throw_error_if_peer_not_found(resources_manager: ResourcesManagerRef, fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> { actions::store_peer_descriptor(StorePeerDescriptorParams { resources_manager: Arc::clone(&resources_manager), vpn: fixture.vpn, @@ -91,13 +115,15 @@ mod tests { options: store_peer_descriptor_options, }).await?; - let peer_id = PeerId::random(); - let peer_state_result = actions::get_peer_state(GetPeerStateParams { - peer: peer_id, + let not_existing_peer_id = PeerId::random(); + assert_that!(resources_manager.get::(not_existing_peer_id).await?.as_ref(), none()); + + let peer_state_result = get_peer_state(GetPeerStateParams { + peer: not_existing_peer_id, resources_manager: Clone::clone(&resources_manager), }).await; - assert_that!(peer_state_result, err(eq(GetPeerStateError::PeerNotFound { peer_id }))); + assert_that!(peer_state_result, err(eq(GetPeerStateError::PeerNotFound { peer_id: not_existing_peer_id }))); Ok(()) } }