diff --git a/databroker/src/grpc/kuksa_val_v2/conversions.rs b/databroker/src/grpc/kuksa_val_v2/conversions.rs index 8fc0f971..874f7181 100644 --- a/databroker/src/grpc/kuksa_val_v2/conversions.rs +++ b/databroker/src/grpc/kuksa_val_v2/conversions.rs @@ -47,8 +47,10 @@ impl From for Option { fn from(from: broker::Datapoint) -> Self { match from.value { broker::DataValue::NotAvailable => Some(proto::Datapoint { - value_state: Some(proto::datapoint::ValueState::Failure(proto::ValueFailure::NotProvided.into())), - timestamp: None + value_state: Some(proto::datapoint::ValueState::Failure( + proto::ValueFailure::NotProvided.into(), + )), + timestamp: None, }), broker::DataValue::Bool(value) => Some(proto::Datapoint { value_state: Some(proto::datapoint::ValueState::Value(proto::Value { diff --git a/databroker/src/grpc/kuksa_val_v2/val.rs b/databroker/src/grpc/kuksa_val_v2/val.rs index 1d0271d1..64652efd 100644 --- a/databroker/src/grpc/kuksa_val_v2/val.rs +++ b/databroker/src/grpc/kuksa_val_v2/val.rs @@ -679,7 +679,7 @@ fn convert_to_proto_stream( broker::DataValue::NotAvailable => None, _ => datapoint.into(), } - }, + } None => None, }; if let Some(dp) = update_datapoint { @@ -744,65 +744,14 @@ mod tests { } } - #[tokio::test] - async fn test_publish_value() { - let broker = DataBroker::default(); - let authorized_access = broker.authorized_access(&permissions::ALLOW_ALL); - let f = false; - - let entry_id = authorized_access - .add_entry( - "test.datapoint1".to_owned(), - broker::DataType::Bool, - broker::ChangeType::OnChange, - broker::EntryType::Sensor, - "Test datapoint 1".to_owned(), - None, - None, - ) - .await - .unwrap(); - - let request = proto::PublishValueRequest { - signal_id: Some(proto::SignalId { - signal: Some(proto::signal_id::Signal::Id(entry_id)), - }), - data_point: { - let timestamp = Some(std::time::SystemTime::now().into()); - - let value = proto::Value { - typed_value: Some(proto::value::TypedValue::Bool(true)), - }; - - Some(proto::Datapoint { - timestamp, - value_state: Some(proto::datapoint::ValueState::Value(value)), - }) - }, - }; - - // Manually insert permissions - let mut publish_value_request = tonic::Request::new(request); - publish_value_request - .extensions_mut() - .insert(permissions::ALLOW_ALL.clone()); - - match broker.publish_value(publish_value_request).await { - Ok(response) => { - // Handle the successful response - let publish_response = response.into_inner(); - assert_eq!(publish_response, proto::PublishValueResponse {}) - } - Err(status) => { - // Handle the error from the publish_value function - assert!(f, "Publish failed with status: {:?}", status); - } - } - } // Helper for adding an int32 signal and adding value - async fn helper_add_int32(broker : &DataBroker, name: &str, value: i32, timestamp: std::time::SystemTime) -> i32 { - + async fn helper_add_int32( + broker: &DataBroker, + name: &str, + value: i32, + timestamp: std::time::SystemTime, + ) -> i32 { let authorized_access = broker.authorized_access(&permissions::ALLOW_ALL); let entry_id = authorized_access .add_entry( @@ -842,14 +791,11 @@ mod tests { } #[tokio::test] - async fn test_get_value_id() { + async fn test_get_value_id_ok() { let broker = DataBroker::default(); - let timestamp = std::time::SystemTime::now(); - - let entry_id = helper_add_int32(&broker, "test.datapoint1", -64, timestamp).await; let request = proto::GetValueRequest { @@ -868,27 +814,56 @@ mod tests { Ok(response) => { // Handle the successful response let get_response = response.into_inner(); - // TODO : Which is preferred - just checking value - match get_response.data_point.clone().unwrap().value_state { - Some(proto::datapoint::ValueState::Value(value)) => { - assert_eq!( - value.typed_value.unwrap(), - proto::value::TypedValue::Int32(-64) - ); - } - Some(proto::datapoint::ValueState::Failure(_failure)) => { - // TODO: When do we expect a failure - assert!(false, "Did not expect failure"); - } - None => { - // Handle the error from the publish_value function - assert!(false, "Expected a value"); + + let value = proto::Value { + typed_value: Some(proto::value::TypedValue::Int32(-64)), + }; + assert_eq!( + get_response, + proto::GetValueResponse { + data_point: { + Some(proto::Datapoint { + timestamp: Some(timestamp.into()), + value_state: Some(proto::datapoint::ValueState::Value(value)), + }) + }, } - } - // TODO : Which is preferred - compare response as such + ); + } + Err(status) => { + // Handle the error from the publish_value function + assert!(false, "Get failed with status: {:?}", status); + } + } + } + + #[tokio::test] + async fn test_get_value_name_ok() { + let broker = DataBroker::default(); + + let timestamp = std::time::SystemTime::now(); + + let _entry_id = helper_add_int32(&broker, "test.datapoint1", -64, timestamp).await; + + let request = proto::GetValueRequest { + signal_id: Some(proto::SignalId { + signal: Some(proto::signal_id::Signal::Path("test.datapoint1".to_string())), + }), + }; + + // Manually insert permissions + let mut get_value_request = tonic::Request::new(request); + get_value_request + .extensions_mut() + .insert(permissions::ALLOW_ALL.clone()); + + match broker.get_value(get_value_request).await { + Ok(response) => { + // Handle the successful response + let get_response = response.into_inner(); let value = proto::Value { - typed_value: Some(proto::value::TypedValue::Int32(-64)), + typed_value: Some(proto::value::TypedValue::Int32(-64)), }; assert_eq!( get_response, @@ -910,11 +885,36 @@ mod tests { } #[tokio::test] - async fn test_get_value_id_no_value() { + async fn test_get_value_id_not_authorized() { + let broker = DataBroker::default(); + + let timestamp = std::time::SystemTime::now(); + let entry_id = helper_add_int32(&broker, "test.datapoint1", -64, timestamp).await; + + let request = proto::GetValueRequest { + signal_id: Some(proto::SignalId { + signal: Some(proto::signal_id::Signal::Id(entry_id)), + }), + }; + + // Do not insert permissions + let get_value_request = tonic::Request::new(request); + + match broker.get_value(get_value_request).await { + Ok(_response) => { + assert!(false, "Did not expect success"); + } + Err(status) => { + assert_eq!(status.code(), tonic::Code::Unauthenticated) + } + } + } + + #[tokio::test] + async fn test_get_value_id_no_value() { // Define signal but do not assign any value - let broker = DataBroker::default(); let authorized_access = broker.authorized_access(&permissions::ALLOW_ALL); let entry_id = authorized_access @@ -930,7 +930,7 @@ mod tests { .await .unwrap(); - // Now try to get it + // Now try to get it let request = proto::GetValueRequest { signal_id: Some(proto::SignalId { @@ -948,21 +948,6 @@ mod tests { Ok(response) => { // Handle the successful response let get_response = response.into_inner(); - // TODO : Which is preferred - just checking value - match get_response.data_point.clone().unwrap().value_state { - Some(proto::datapoint::ValueState::Value(_value)) => { - assert!(false, "Did not expect success"); - } - Some(proto::datapoint::ValueState::Failure(failure)) => { - let res:i32 = proto::ValueFailure::NotProvided.into(); - assert_eq!(failure, res); - } - None => { - // Handle the error from the publish_value function - assert!(false, "Did not expect this error"); - } - } - // TODO : Which is preferred - compare response as such assert_eq!( get_response, @@ -970,7 +955,9 @@ mod tests { data_point: { Some(proto::Datapoint { timestamp: None, - value_state: Some(proto::datapoint::ValueState::Failure(proto::ValueFailure::NotProvided.into())), + value_state: Some(proto::datapoint::ValueState::Failure( + proto::ValueFailure::NotProvided.into(), + )), }) }, } @@ -983,16 +970,13 @@ mod tests { } } - #[tokio::test] async fn test_get_value_id_not_defined() { - - let broker = DataBroker::default(); // Just use some arbitrary number let entry_id: i32 = 12345; - // Now try to get it + // Now try to get it let request = proto::GetValueRequest { signal_id: Some(proto::SignalId { @@ -1016,6 +1000,90 @@ mod tests { } } + #[tokio::test] + async fn test_get_value_name_not_defined() { + let broker = DataBroker::default(); + + // Now try to get it + + let request = proto::GetValueRequest { + signal_id: Some(proto::SignalId { + signal: Some(proto::signal_id::Signal::Path("test.datapoint1".to_string())), + }), + }; + + // Manually insert permissions + let mut get_value_request = tonic::Request::new(request); + get_value_request + .extensions_mut() + .insert(permissions::ALLOW_ALL.clone()); + + match broker.get_value(get_value_request).await { + Ok(_response) => { + assert!(false, "Did not expect success"); + } + Err(status) => { + assert_eq!(status.code(), tonic::Code::NotFound) + } + } + } + + #[tokio::test] + async fn test_publish_value() { + let broker = DataBroker::default(); + let authorized_access = broker.authorized_access(&permissions::ALLOW_ALL); + let f = false; + + let entry_id = authorized_access + .add_entry( + "test.datapoint1".to_owned(), + broker::DataType::Bool, + broker::ChangeType::OnChange, + broker::EntryType::Sensor, + "Test datapoint 1".to_owned(), + None, + None, + ) + .await + .unwrap(); + + let request = proto::PublishValueRequest { + signal_id: Some(proto::SignalId { + signal: Some(proto::signal_id::Signal::Id(entry_id)), + }), + data_point: { + let timestamp = Some(std::time::SystemTime::now().into()); + + let value = proto::Value { + typed_value: Some(proto::value::TypedValue::Bool(true)), + }; + + Some(proto::Datapoint { + timestamp, + value_state: Some(proto::datapoint::ValueState::Value(value)), + }) + }, + }; + + // Manually insert permissions + let mut publish_value_request = tonic::Request::new(request); + publish_value_request + .extensions_mut() + .insert(permissions::ALLOW_ALL.clone()); + + match broker.publish_value(publish_value_request).await { + Ok(response) => { + // Handle the successful response + let publish_response = response.into_inner(); + assert_eq!(publish_response, proto::PublishValueResponse {}) + } + Err(status) => { + // Handle the error from the publish_value function + assert!(f, "Publish failed with status: {:?}", status); + } + } + } + #[tokio::test] async fn test_publish_value_signal_id_not_found() { let broker = DataBroker::default();