use crate::core::macros::{tmf_struct, tmf_value};
use crate::core::{ConnectionPoint, Ref, TmfType};
use super::specification::{ConnectionAssociationType, ResourceGraphRelationshipType};
tmf_value! {
pub struct ResourceGraph {
id: String,
name: String,
description: String,
graph_relationship: Vec<ResourceGraphRelationship>,
connection: Vec<Connection>,
}
}
impl TmfType for ResourceGraph {
const TYPE_NAME: &'static str = "ResourceGraph";
const REF_TYPE_NAME: &'static str = "ResourceGraphRef";
}
tmf_struct! {
@name = "ResourceGraphRelationship";
pub struct ResourceGraphRelationship {
id: String,
href: String,
relationship_type: ResourceGraphRelationshipType,
resource_graph: Ref<ResourceGraph>,
}
}
tmf_struct! {
@name = "Connection";
pub struct Connection {
id: String,
href: String,
name: String,
association_type: ConnectionAssociationType,
endpoint: Vec<Endpoint>,
}
}
tmf_struct! {
@name = "EndpointRef";
pub struct Endpoint {
id: String,
href: String,
name: String,
is_root: bool,
connection_point: Ref<ConnectionPoint>,
@renamed {
"@referredType" referred_type: String,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_graph_round_trips_with_its_edges() {
let json = r#"{"id":"g1","name":"fabric","connection":[{"id":"c1","associationType":"pointtoPoint","endpoint":[{"id":"e1","isRoot":true,"@type":"EndpointRef"},{"id":"e2","isRoot":false,"@type":"EndpointRef"}],"@type":"Connection"}]}"#;
let graph: ResourceGraph = serde_json::from_str(json).unwrap();
let edges = graph.connection.as_ref().unwrap();
assert_eq!(
edges[0].association_type,
Some(ConnectionAssociationType::PointToPoint)
);
assert_eq!(edges[0].endpoint.as_ref().unwrap().len(), 2);
assert_eq!(
serde_json::to_value(&graph).unwrap(),
serde_json::from_str::<serde_json::Value>(json).unwrap()
);
}
#[test]
fn an_absent_is_root_is_not_false() {
let endpoint: Endpoint = serde_json::from_str(r#"{"id":"e1"}"#).unwrap();
assert_eq!(endpoint.is_root, None);
assert!(endpoint.is_root.unwrap_or(true), "the spec default is true");
}
}