Skip to content

Commit

Permalink
Increment 5
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbosch committed Sep 18, 2024
1 parent 5ba4962 commit 87bbd2b
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 105 deletions.
6 changes: 4 additions & 2 deletions databroker/src/grpc/kuksa_val_v2/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ impl From<broker::Datapoint> for Option<proto::Datapoint> {
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 {
Expand Down
274 changes: 171 additions & 103 deletions databroker/src/grpc/kuksa_val_v2/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ fn convert_to_proto_stream(
broker::DataValue::NotAvailable => None,
_ => datapoint.into(),
}
},
}
None => None,
};
if let Some(dp) = update_datapoint {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -948,29 +948,16 @@ 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,
proto::GetValueResponse {
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(),
)),
})
},
}
Expand All @@ -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 {
Expand All @@ -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();
Expand Down

0 comments on commit 87bbd2b

Please sign in to comment.