use crate::csaf::types::csaf_document_category::CsafDocumentCategory;
use crate::csaf::types::language::CsafLanguage;
use crate::csaf_traits::{CsafTrait, DocumentTrait, NoteTrait};
use crate::schema::csaf2_1::schema::NoteCategory;
use crate::validation::ValidationError;
use crate::validations::utils::document_category_test_config::DocumentCategoryTestConfig;
fn create_missing_reasoning_error(document_category: &CsafDocumentCategory) -> ValidationError {
ValidationError {
message: format!(
"The document does not contain a note with title `Reasoning for Withdrawal` and category `description` which is required for documents with category {document_category}"
),
instance_path: "/document/notes".to_string(),
}
}
fn create_duplicated_reasoning_error(document_category: &CsafDocumentCategory, note_index: usize) -> ValidationError {
ValidationError {
message: format!(
"Duplicate note with title `Reasoning for Withdrawal` found while only one is allowed for documents with category {document_category}"
),
instance_path: format!("/document/notes[{note_index}]"),
}
}
fn create_incorrect_category_error(note_index: usize) -> ValidationError {
ValidationError {
message: "The note has the correct title. However it uses the wrong category.".to_string(),
instance_path: format!("/document/notes[{note_index}]"),
}
}
pub fn test_6_1_27_18_document_notes_for_supersession(doc: &impl CsafTrait) -> Result<(), Vec<ValidationError>> {
let doc_category = doc.get_document().get_category();
if !PROFILE_TEST_CONFIG.matches_category_with_csaf_version(doc.get_document().get_csaf_version(), &doc_category) {
return Ok(()); }
match doc.get_document().get_lang() {
Some(CsafLanguage::Invalid(_, _)) => return Ok(()), Some(CsafLanguage::Valid(valid_lang)) if valid_lang.is_default() || !valid_lang.is_english() => return Ok(()), Some(_) => {}, None => {}, }
let mut errors = Vec::new();
let mut supersessions = Vec::new();
if let Some(notes) = doc.get_document().get_notes() {
for (i_n, note) in notes.iter().enumerate() {
if let Some(title) = note.get_title()
&& title == "Reasoning for Supersession"
{
if note.get_category() != NoteCategory::Description {
errors.push(create_incorrect_category_error(i_n));
}
supersessions.push(i_n);
}
}
}
if supersessions.is_empty() {
return Err(vec![create_missing_reasoning_error(&doc_category)]);
} else if supersessions.len() > 1 {
return Err(supersessions
.iter()
.map(|f| create_duplicated_reasoning_error(&doc_category, *f))
.collect::<Vec<_>>());
}
if !errors.is_empty() {
return Err(errors);
}
Ok(())
}
const PROFILE_TEST_CONFIG: DocumentCategoryTestConfig =
DocumentCategoryTestConfig::new().csaf21(&[CsafDocumentCategory::CsafSuperseded]);
crate::test_validation::impl_validator!(
csaf2_1,
ValidatorForTest6_1_27_18,
test_6_1_27_18_document_notes_for_supersession
);
#[cfg(test)]
mod tests {
use super::*;
use crate::csaf2_1::testcases::TESTS_2_1;
#[test]
fn test_test_6_1_27_18() {
let undefined_lang_wrong_category = Err(vec![create_incorrect_category_error(0)]);
let undefined_lang_duplicate_title = Err(vec![
create_duplicated_reasoning_error(&CsafDocumentCategory::CsafSuperseded, 0),
create_duplicated_reasoning_error(&CsafDocumentCategory::CsafSuperseded, 1),
]);
let lang_en_us_wrong_category = Err(vec![create_incorrect_category_error(0)]);
let lang_en_gb_missing_reasoning = Err(vec![create_missing_reasoning_error(
&CsafDocumentCategory::CsafSuperseded,
)]);
TESTS_2_1.test_6_1_27_18.expect(
undefined_lang_wrong_category.clone(),
undefined_lang_duplicate_title.clone(),
undefined_lang_wrong_category,
undefined_lang_duplicate_title,
lang_en_us_wrong_category,
lang_en_gb_missing_reasoning.clone(),
lang_en_gb_missing_reasoning,
Ok(()),
Ok(()),
Ok(()),
);
}
}