#![cfg(all(feature = "cli", feature = "cgmes3"))]
mod common;
use std::path::Path;
use std::process::Command;
const NS: &str = "http://iec.ch/TC57/CIM100#";
const LOCATION: &str = "22222222-2222-4222-8222-222222222222";
fn cim() -> Command {
Command::new(env!("CARGO_BIN_EXE_cim"))
}
fn scratch(name: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!("cim-cli-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
fn location_file(dir: &Path, postal_code: &str) {
location_file_as(dir, postal_code, "11111111-1111-4111-8111-111111111111");
}
fn location_file_as(dir: &Path, postal_code: &str, model: &str) {
let text = format!(
r##"<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cim="{NS}"
xmlns:md="http://iec.ch/TC57/61970-552/ModelDescription/1#">
<md:FullModel rdf:about="urn:uuid:{model}">
<md:Model.created>2024-01-01T00:00:00Z</md:Model.created>
<md:Model.scenarioTime>2024-01-01T00:00:00Z</md:Model.scenarioTime>
<md:Model.profile>http://iec.ch/TC57/ns/CIM/GeographicalLocation-EU/3.0</md:Model.profile>
</md:FullModel>
<cim:Location rdf:ID="_{LOCATION}">
<cim:IdentifiedObject.mRID>{LOCATION}</cim:IdentifiedObject.mRID>
<cim:Location.mainAddress rdf:parseType="Resource">
<cim:StreetAddress.postalCode>{postal_code}</cim:StreetAddress.postalCode>
</cim:Location.mainAddress>
</cim:Location>
</rdf:RDF>
"##
);
std::fs::create_dir_all(dir).unwrap();
std::fs::write(dir.join("GL.xml"), text).unwrap();
}
#[test]
fn a_change_set_written_to_stdout_is_a_document_and_nothing_else() {
let root = scratch("diff-stdout");
location_file(&root.join("base"), "1000");
location_file(&root.join("target"), "2000");
let out = cim()
.arg("diff")
.arg(root.join("base"))
.arg(root.join("target"))
.output()
.unwrap();
assert!(out.status.success(), "cim diff failed: {out:?}");
let document = String::from_utf8(out.stdout).unwrap();
let stderr = String::from_utf8(out.stderr).unwrap();
assert!(
stderr.contains("CIM0015") && stderr.contains("compound"),
"the compound change was not reported at all:\nstderr: {stderr}"
);
assert!(
!document.contains("CIM0015"),
"the diagnostics report was written into the document:\n{document}"
);
common::assert_well_formed("cim diff > change.xml", &document);
assert!(document.trim_end().ends_with("</rdf:RDF>"), "{document}");
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_change_set_written_to_a_file_leaves_stdout_for_the_report() {
let root = scratch("diff-file");
location_file(&root.join("base"), "1000");
location_file(&root.join("target"), "2000");
let path = root.join("change_DIFF.xml");
let out = cim()
.arg("diff")
.arg(root.join("base"))
.arg(root.join("target"))
.arg("--out")
.arg(&path)
.output()
.unwrap();
assert!(out.status.success(), "cim diff --out failed: {out:?}");
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(stdout.contains("CIM0015"), "{stdout}");
common::assert_well_formed("change_DIFF.xml", &std::fs::read_to_string(&path).unwrap());
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn rdf_writes_a_graph_only_for_a_profile_the_model_describes() {
let root = scratch("rdf-empty");
location_file(&root.join("model"), "1000");
let graphs = root.join("graphs");
let out = cim()
.arg("rdf")
.arg(root.join("model"))
.arg("--out")
.arg(&graphs)
.arg("--ntriples")
.output()
.unwrap();
assert!(out.status.success(), "cim rdf failed: {out:?}");
assert!(graphs.join("GL.nt").is_file(), "the GL graph is missing");
for absent in ["EQ.nt", "SSH.nt", "SV.nt", "TP.nt", "DY.nt", "FH.nt"] {
assert!(
!graphs.join(absent).is_file(),
"{absent} was written for a profile this model says nothing about"
);
}
let stderr = String::from_utf8(out.stderr).unwrap();
assert!(stderr.contains("no data for"), "{stderr}");
let gl = std::fs::read_to_string(graphs.join("GL.nt")).unwrap();
assert!(gl.contains("GeographicalLocation-EU"), "{gl}");
assert!(common::check_ntriples(&gl).unwrap() > 3, "{gl}");
let _ = std::fs::remove_dir_all(&root);
}
#[test]
#[cfg(feature = "cgmes2")]
fn the_vintage_is_detected_from_the_input() {
let root = scratch("vintage");
let model = root.join("model");
std::fs::create_dir_all(&model).unwrap();
std::fs::write(
model.join("EQ.xml"),
concat!(
r#"<?xml version="1.0" encoding="UTF-8"?>"#,
"\n",
r#"<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#""#,
r#" xmlns:cim="http://iec.ch/TC57/2013/CIM-schema-cim16#""#,
r#" xmlns:md="http://iec.ch/TC57/61970-552/ModelDescription/1#">"#,
"\n",
r#" <md:FullModel rdf:about="urn:uuid:11111111-1111-4111-8111-111111111111">"#,
r#"<md:Model.profile>http://entsoe.eu/CIM/EquipmentCore/3/1</md:Model.profile>"#,
r#"</md:FullModel>"#,
"\n",
r#" <cim:Substation rdf:ID="_22222222-2222-4222-8222-222222222222">"#,
r#"<cim:IdentifiedObject.name>S1</cim:IdentifiedObject.name></cim:Substation>"#,
"\n</rdf:RDF>\n"
),
)
.unwrap();
let out = cim().arg("info").arg(&model).output().unwrap();
let stdout = String::from_utf8_lossy(&out.stdout).into_owned();
assert!(stdout.contains("objects 1"), "{stdout}");
assert!(!stdout.contains("CIM0021"), "{stdout}");
let out = cim()
.arg("info")
.arg(&model)
.args(["--vintage", "cgmes3"])
.output()
.unwrap();
let stdout = String::from_utf8_lossy(&out.stdout).into_owned();
assert!(
stdout.contains("CIM0021"),
"no mismatch reported:\n{stdout}"
);
assert!(stdout.contains("objects 0"), "{stdout}");
assert_eq!(
out.status.code(),
Some(1),
"a model that read as nothing exited 0"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn the_binary_says_which_release_it_is() {
for flag in ["--version", "-V"] {
let out = cim().arg(flag).output().unwrap();
assert_eq!(out.status.code(), Some(0), "{flag}: {out:?}");
assert_eq!(
String::from_utf8_lossy(&out.stdout).trim(),
format!("cim {}", env!("CARGO_PKG_VERSION")),
"{flag}"
);
}
}
#[test]
fn a_mistyped_flag_is_an_error_rather_than_a_silently_ignored_one() {
let out = cim().arg("info").arg("-Q").arg(".").output().unwrap();
assert_eq!(out.status.code(), Some(2), "{out:?}");
assert!(
String::from_utf8_lossy(&out.stderr).contains("unknown option"),
"{out:?}"
);
}
#[test]
fn exporting_a_model_whose_files_declare_no_profile_writes_it_anyway() {
let dir = scratch("headerless");
let input = dir.join("in");
std::fs::create_dir_all(&input).unwrap();
std::fs::write(
input.join("model.xml"),
format!(
r##"<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cim="{NS}">
<cim:ACLineSegment rdf:ID="_{LOCATION}">
<cim:IdentifiedObject.name>Line</cim:IdentifiedObject.name>
<cim:ACLineSegment.r>2.2</cim:ACLineSegment.r>
</cim:ACLineSegment>
</rdf:RDF>
"##
),
)
.unwrap();
let out = dir.join("out");
let result = cim()
.args(["export"])
.arg(&input)
.arg("--out")
.arg(&out)
.output()
.unwrap();
assert!(
result.status.success(),
"stderr: {}",
String::from_utf8_lossy(&result.stderr)
);
let written: Vec<_> = std::fs::read_dir(&out).unwrap().flatten().collect();
assert_eq!(written.len(), 1, "expected one file, got {written:?}");
let text = std::fs::read_to_string(written[0].path()).unwrap();
assert!(text.contains("ACLineSegment"), "{text}");
assert!(
String::from_utf8_lossy(&result.stderr).contains("no input file declares a profile"),
"the fallback was silent: {:?}",
String::from_utf8_lossy(&result.stderr)
);
common::assert_well_formed("fallback export", &text);
let named = dir.join("named");
let result = cim()
.args(["export"])
.arg(&input)
.args(["--assume-profile", "EQ"])
.arg("--out")
.arg(&named)
.output()
.unwrap();
assert!(result.status.success());
let text = std::fs::read_to_string(named.join("model.xml")).expect("model.xml");
assert!(text.contains("<md:FullModel"), "{text}");
assert!(
text.contains("CoreEquipment-EU"),
"the header does not declare the assumed profile:\n{text}"
);
}
#[test]
fn a_change_set_computed_by_the_tool_can_be_applied_by_the_tool() {
fn named(dir: &Path, name: &str) {
let text = format!(
r##"<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cim="{NS}"
xmlns:md="http://iec.ch/TC57/61970-552/ModelDescription/1#">
<md:FullModel rdf:about="urn:uuid:11111111-1111-4111-8111-111111111111">
<md:Model.profile>http://iec.ch/TC57/ns/CIM/CoreEquipment-EU/3.0</md:Model.profile>
</md:FullModel>
<cim:Substation rdf:ID="_{LOCATION}">
<cim:IdentifiedObject.mRID>{LOCATION}</cim:IdentifiedObject.mRID>
<cim:IdentifiedObject.name>{name}</cim:IdentifiedObject.name>
</cim:Substation>
</rdf:RDF>
"##
);
std::fs::create_dir_all(dir).unwrap();
std::fs::write(dir.join("EQ.xml"), text).unwrap();
}
let dir = scratch("apply");
let base = dir.join("base");
let target = dir.join("target");
named(&base, "before");
named(&target, "after");
let change = dir.join("change_DIFF.xml");
let made = cim()
.args(["diff"])
.arg(&base)
.arg(&target)
.arg("--out")
.arg(&change)
.output()
.unwrap();
assert!(
made.status.success(),
"{}",
String::from_utf8_lossy(&made.stderr)
);
let out = dir.join("out");
let applied = cim()
.args(["apply"])
.arg(&base)
.args(["--change"])
.arg(&change)
.arg("--out")
.arg(&out)
.output()
.unwrap();
assert!(
applied.status.success(),
"{}",
String::from_utf8_lossy(&applied.stderr)
);
let written = std::fs::read_to_string(out.join("EQ.xml")).expect("EQ.xml");
common::assert_well_formed("applied", &written);
assert!(
written.contains("<cim:IdentifiedObject.name>after<"),
"{written}"
);
assert!(!written.contains("before"), "{written}");
let missing = cim()
.args(["apply"])
.arg(&base)
.arg("--out")
.arg(&out)
.output()
.unwrap();
assert_eq!(missing.status.code(), Some(2));
assert!(
String::from_utf8_lossy(&missing.stderr).contains("--change"),
"{}",
String::from_utf8_lossy(&missing.stderr)
);
}
#[test]
fn an_input_over_the_budget_names_the_flag_that_raises_it() {
let root = scratch("max-input");
location_file(&root.join("model"), "1000");
let out = cim()
.args(["info"])
.arg(root.join("model"))
.args(["--max-input", "256B"])
.output()
.unwrap();
assert_eq!(out.status.code(), Some(2), "{out:?}");
let stderr = String::from_utf8(out.stderr).unwrap();
assert!(stderr.contains("input budget"), "{stderr}");
assert!(
stderr.contains("--max-input 1KiB"),
"suggests a value: {stderr}"
);
assert!(stderr.contains("GL.xml"), "names the file: {stderr}");
assert!(
!stderr.contains("ReadOptions"),
"the library's remedy is not the operator's: {stderr}"
);
let out = cim()
.args(["info"])
.arg(root.join("model"))
.args(["--max-input", "1KiB"])
.output()
.unwrap();
assert_ne!(
out.status.code(),
Some(2),
"the suggested value works: {out:?}"
);
}
#[test]
fn an_export_over_the_budget_writes_nothing() {
let root = scratch("max-input-export");
location_file(&root.join("model"), "1000");
let dest = root.join("out");
let out = cim()
.args(["export"])
.arg(root.join("model"))
.arg("--out")
.arg(&dest)
.args(["--max-input", "100B"])
.output()
.unwrap();
assert_eq!(out.status.code(), Some(2), "{out:?}");
let written = std::fs::read_dir(&dest).map_or(0, Iterator::count);
assert_eq!(written, 0, "no file is written after the budget is hit");
}
#[test]
fn max_input_takes_sizes_the_way_people_write_them() {
let root = scratch("max-input-forms");
location_file(&root.join("model"), "1000");
for size in ["8GiB", "512MB", "4096", "4kib", "unlimited", "0"] {
let out = cim()
.args(["info"])
.arg(root.join("model"))
.args(["--max-input", size])
.output()
.unwrap();
assert_ne!(out.status.code(), Some(2), "{size}: {out:?}");
}
for size in ["banana", "1.5GiB", "12XB", "-1", ""] {
let out = cim()
.args(["info"])
.arg(root.join("model"))
.args(["--max-input", size])
.output()
.unwrap();
assert_eq!(out.status.code(), Some(2), "{size} is refused");
let stderr = String::from_utf8(out.stderr).unwrap();
assert!(
stderr.contains("GiB") && stderr.contains("unlimited"),
"{stderr}"
);
}
}
#[test]
fn a_change_set_applied_to_the_wrong_model_is_refused() {
let root = scratch("apply-wrong-base");
location_file_as(
&root.join("base"),
"1000",
"aaaaaaaa-1111-4111-8111-111111111111",
);
location_file_as(
&root.join("target"),
"2000",
"bbbbbbbb-2222-4222-8222-222222222222",
);
let change = root.join("change.xml");
let out = cim()
.arg("diff")
.arg(root.join("base"))
.arg(root.join("target"))
.arg("--out")
.arg(&change)
.output()
.unwrap();
assert!(out.status.success(), "{out:?}");
let dest = root.join("result");
let out = cim()
.arg("apply")
.arg(root.join("target"))
.arg("--change")
.arg(&change)
.arg("--out")
.arg(&dest)
.output()
.unwrap();
assert_eq!(out.status.code(), Some(1), "{out:?}");
let stderr = String::from_utf8(out.stderr).unwrap();
assert!(
stderr.contains("CIM0014") && stderr.contains("aaaaaaaa"),
"{stderr}"
);
assert_eq!(
std::fs::read_dir(&dest).map_or(0, Iterator::count),
0,
"nothing written"
);
let out = cim()
.arg("apply")
.arg(root.join("base"))
.arg("--change")
.arg(&change)
.arg("--out")
.arg(&dest)
.output()
.unwrap();
assert_ne!(
out.status.code(),
Some(2),
"onto its own base it applies: {out:?}"
);
assert!(std::fs::read_dir(&dest).map_or(0, Iterator::count) > 0);
}
#[test]
fn a_truncated_file_is_named_with_where_it_stops() {
let dir = scratch("truncated");
location_file(&dir, "1000");
let whole = std::fs::read_to_string(dir.join("GL.xml")).unwrap();
let cut = whole.find("</cim:Location>").unwrap();
std::fs::write(dir.join("GL_cut.xml"), &whole[..cut]).unwrap();
std::fs::remove_file(dir.join("GL.xml")).unwrap();
location_file_as(&dir, "1000", "33333333-3333-4333-8333-333333333333");
let out = cim().arg("validate").arg(&dir).output().unwrap();
let stdout = String::from_utf8_lossy(&out.stdout);
assert_eq!(out.status.code(), Some(1), "{stdout}");
assert!(
stdout.contains("error[CIM0022] GL_cut.xml@") && stdout.contains("truncated"),
"{stdout}"
);
let out = cim()
.args(["validate", "--strict"])
.arg(&dir)
.output()
.unwrap();
let stderr = String::from_utf8_lossy(&out.stderr);
assert_eq!(out.status.code(), Some(2), "{stderr}");
assert!(
stderr.contains("truncated document GL_cut.xml") && stderr.contains("<cim:Location>"),
"{stderr}"
);
}
#[test]
fn single_description_reports_an_object_described_twice() {
let dir = scratch("single-description");
let file = dir.join("EQ.xml");
std::fs::write(
&file,
format!(
r##"<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cim="{NS}">
<cim:Terminal rdf:ID="_{LOCATION}"/>
<cim:Terminal rdf:about="#_{LOCATION}"/>
</rdf:RDF>"##
),
)
.unwrap();
let run = |flag: Option<&str>| {
let out = cim()
.args(["validate", "--rule", "CIM0005"])
.args(flag)
.arg(&file)
.output()
.unwrap();
String::from_utf8_lossy(&out.stdout).into_owned()
};
assert!(!run(None).contains("error[CIM0005]"));
let flagged = run(Some("--single-description"));
assert!(
flagged.contains("error[CIM0005] EQ.xml@") && flagged.contains("described again"),
"{flagged}"
);
}