automapper_validation/validator/
report.rs1use serde::{Deserialize, Serialize};
4
5use super::issue::{Severity, ValidationCategory, ValidationIssue};
6use super::level::ValidationLevel;
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct ValidationReport {
14 pub message_type: String,
16
17 pub pruefidentifikator: Option<String>,
19
20 pub format_version: Option<String>,
22
23 pub level: ValidationLevel,
25
26 pub issues: Vec<ValidationIssue>,
28}
29
30impl ValidationReport {
31 pub fn new(message_type: impl Into<String>, level: ValidationLevel) -> Self {
33 Self {
34 message_type: message_type.into(),
35 pruefidentifikator: None,
36 format_version: None,
37 level,
38 issues: Vec::new(),
39 }
40 }
41
42 pub fn with_pruefidentifikator(mut self, pid: impl Into<String>) -> Self {
44 self.pruefidentifikator = Some(pid.into());
45 self
46 }
47
48 pub fn with_format_version(mut self, fv: impl Into<String>) -> Self {
50 self.format_version = Some(fv.into());
51 self
52 }
53
54 pub fn add_issue(&mut self, issue: ValidationIssue) {
56 self.issues.push(issue);
57 }
58
59 pub fn add_issues(&mut self, issues: impl IntoIterator<Item = ValidationIssue>) {
61 self.issues.extend(issues);
62 }
63
64 pub fn is_valid(&self) -> bool {
66 !self.issues.iter().any(|i| i.severity == Severity::Error)
67 }
68
69 pub fn error_count(&self) -> usize {
71 self.issues
72 .iter()
73 .filter(|i| i.severity == Severity::Error)
74 .count()
75 }
76
77 pub fn warning_count(&self) -> usize {
79 self.issues
80 .iter()
81 .filter(|i| i.severity == Severity::Warning)
82 .count()
83 }
84
85 pub fn errors(&self) -> impl Iterator<Item = &ValidationIssue> {
87 self.issues.iter().filter(|i| i.severity == Severity::Error)
88 }
89
90 pub fn warnings(&self) -> impl Iterator<Item = &ValidationIssue> {
92 self.issues
93 .iter()
94 .filter(|i| i.severity == Severity::Warning)
95 }
96
97 pub fn infos(&self) -> impl Iterator<Item = &ValidationIssue> {
99 self.issues.iter().filter(|i| i.severity == Severity::Info)
100 }
101
102 pub fn by_category(
104 &self,
105 category: ValidationCategory,
106 ) -> impl Iterator<Item = &ValidationIssue> {
107 self.issues.iter().filter(move |i| i.category == category)
108 }
109
110 pub fn total_issues(&self) -> usize {
112 self.issues.len()
113 }
114
115 pub fn enrich_bo4e_paths(&mut self, resolver: impl Fn(&str) -> Option<String>) {
121 for issue in &mut self.issues {
122 if let Some(ref edifact_path) = issue.field_path {
123 issue.bo4e_path = resolver(edifact_path);
124 }
125 }
126 }
127}
128
129#[cfg(test)]
130mod tests {
131 use super::*;
132 use crate::validator::issue::ValidationCategory;
133
134 fn make_error(code: &str) -> ValidationIssue {
135 ValidationIssue::new(Severity::Error, ValidationCategory::Ahb, code, "test error")
136 }
137
138 fn make_warning(code: &str) -> ValidationIssue {
139 ValidationIssue::new(
140 Severity::Warning,
141 ValidationCategory::Structure,
142 code,
143 "test warning",
144 )
145 }
146
147 fn make_info(code: &str) -> ValidationIssue {
148 ValidationIssue::new(Severity::Info, ValidationCategory::Code, code, "test info")
149 }
150
151 #[test]
152 fn test_empty_report_is_valid() {
153 let report = ValidationReport::new("UTILMD", ValidationLevel::Full);
154 assert!(report.is_valid());
155 assert_eq!(report.error_count(), 0);
156 assert_eq!(report.warning_count(), 0);
157 assert_eq!(report.total_issues(), 0);
158 }
159
160 #[test]
161 fn test_report_with_errors_is_invalid() {
162 let mut report = ValidationReport::new("UTILMD", ValidationLevel::Full);
163 report.add_issue(make_error("AHB001"));
164
165 assert!(!report.is_valid());
166 assert_eq!(report.error_count(), 1);
167 }
168
169 #[test]
170 fn test_report_with_only_warnings_is_valid() {
171 let mut report = ValidationReport::new("UTILMD", ValidationLevel::Full);
172 report.add_issue(make_warning("STR001"));
173
174 assert!(report.is_valid());
175 assert_eq!(report.warning_count(), 1);
176 assert_eq!(report.error_count(), 0);
177 }
178
179 #[test]
180 fn test_report_mixed_issues() {
181 let mut report = ValidationReport::new("UTILMD", ValidationLevel::Full)
182 .with_pruefidentifikator("11001")
183 .with_format_version("FV2510");
184
185 report.add_issue(make_error("AHB001"));
186 report.add_issue(make_error("AHB003"));
187 report.add_issue(make_warning("STR002"));
188 report.add_issue(make_info("COD001"));
189
190 assert!(!report.is_valid());
191 assert_eq!(report.error_count(), 2);
192 assert_eq!(report.warning_count(), 1);
193 assert_eq!(report.total_issues(), 4);
194 assert_eq!(report.errors().count(), 2);
195 assert_eq!(report.warnings().count(), 1);
196 assert_eq!(report.infos().count(), 1);
197 }
198
199 #[test]
200 fn test_report_by_category() {
201 let mut report = ValidationReport::new("UTILMD", ValidationLevel::Full);
202 report.add_issue(make_error("AHB001"));
203 report.add_issue(make_warning("STR002"));
204
205 assert_eq!(report.by_category(ValidationCategory::Ahb).count(), 1);
206 assert_eq!(report.by_category(ValidationCategory::Structure).count(), 1);
207 assert_eq!(report.by_category(ValidationCategory::Format).count(), 0);
208 }
209
210 #[test]
211 fn test_report_add_issues() {
212 let mut report = ValidationReport::new("UTILMD", ValidationLevel::Full);
213 let issues = vec![make_error("AHB001"), make_warning("STR001")];
214 report.add_issues(issues);
215
216 assert_eq!(report.total_issues(), 2);
217 }
218
219 #[test]
220 fn test_enrich_bo4e_paths() {
221 let mut report = ValidationReport::new("UTILMD", ValidationLevel::Full);
222 report.add_issue(make_error("AHB001").with_field_path("SG4/SG5/LOC/C517/3225"));
223 report.add_issue(make_warning("STR001").with_field_path("SG2/NAD/3035"));
224 report.add_issue(make_error("AHB002"));
226
227 report.enrich_bo4e_paths(|path| match path {
228 "SG4/SG5/LOC/C517/3225" => Some("stammdaten.Marktlokation.marktlokationsId".into()),
229 "SG2/NAD/3035" => Some("stammdaten.Marktteilnehmer".into()),
230 _ => None,
231 });
232
233 assert_eq!(
234 report.issues[0].bo4e_path.as_deref(),
235 Some("stammdaten.Marktlokation.marktlokationsId")
236 );
237 assert_eq!(
238 report.issues[1].bo4e_path.as_deref(),
239 Some("stammdaten.Marktteilnehmer")
240 );
241 assert!(report.issues[2].bo4e_path.is_none());
243 }
244
245 #[test]
246 fn test_report_serialization() {
247 let mut report = ValidationReport::new("UTILMD", ValidationLevel::Conditions)
248 .with_pruefidentifikator("11001")
249 .with_format_version("FV2510");
250 report.add_issue(make_error("AHB001"));
251
252 let json = serde_json::to_string_pretty(&report).unwrap();
253 assert!(json.contains("UTILMD"));
254 assert!(json.contains("11001"));
255 assert!(json.contains("AHB001"));
256
257 let deserialized: ValidationReport = serde_json::from_str(&json).unwrap();
258 assert_eq!(deserialized.message_type, "UTILMD");
259 assert_eq!(deserialized.pruefidentifikator.as_deref(), Some("11001"));
260 assert_eq!(deserialized.total_issues(), 1);
261 }
262}