use std::fs::read_to_string; use std::path::PathBuf; use serde_lwjson::{Document, HelperMethods, Metadata}; fn read_file_contents(name: &str) -> String { let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); path.push("resources/test"); path.push(name); let contents = read_to_string(path).expect("failed to read file"); contents } fn read_from_file(name: &str) -> Document { let contents = read_file_contents(name); let document: Document = serde_json::from_str(&*contents).expect("failed to deserialize"); document } #[test] fn test_deserialize_full() { let document = read_from_file("full.json"); assert_eq!(6, document.artifacts.as_ref().expect("missing artifacts").len()); assert_eq!(1, document.repositories.as_ref().expect("missing repositories").len()); assert_eq!(3, document.vulnerabilities.as_ref().expect("missing vulns").len()); assert_eq!(1, document.fix_suggestions.as_ref().expect("missing fix suggestions").len()); assert_eq!("maven", document.build_artifacts_map().get("0fe373ef-1866-4a4b-81a2-b748d65361ac") .expect("missing artifact").type_.as_ref().expect("missing type")); assert_eq!("git", document.build_repo_map().get("1aef42df-adf4-49d7-9639-d43c7cec98a1") .expect("missing repo").info.kind); } #[test] fn test_deserialize_simple() { let document = read_from_file("simple.json"); assert_eq!(1, document.schema_version); assert_eq!("some-id", document.metadata.id); } #[test] fn test_serialize_simple() { let document = Document { schema_version: 1, metadata: Metadata { ci_info: None, created: None, id: "some-id".to_string(), input_kind: None, input_location: None, input_origin: None, platform: None, custom_properties: None, }, artifacts: None, fix_suggestions: None, identities: None, repositories: None, root_artifact_id: None, vulnerabilities: None, weaknesses: None, license_violations: None, secrets: None, }; let actual = serde_json::to_string(&document).expect("failed to serialize"); let expected = read_file_contents("serialized.json"); assert_eq!(expected, actual); }