VeraPdfConformanceTestRunner.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.Diagnostics;
  2. using System.Text;
  3. using System.Text.Json;
  4. using QuestPDF.Fluent;
  5. using QuestPDF.Infrastructure;
  6. namespace QuestPDF.ConformanceTests.TestEngine;
  7. public static class VeraPdfConformanceTestRunner
  8. {
  9. public class ValidationResult
  10. {
  11. public bool IsDocumentValid => !FailedRules.Any();
  12. public ICollection<FailedRule> FailedRules { get; set; } = [];
  13. public class FailedRule
  14. {
  15. public string Profile { get; set; }
  16. public string Specification { get; set; }
  17. public string Clause { get; set; }
  18. public string Description { get; set; }
  19. }
  20. public string GetErrorMessage()
  21. {
  22. if (!FailedRules.Any())
  23. return string.Empty;
  24. var errorMessage = new StringBuilder();
  25. foreach (var failedRule in FailedRules)
  26. {
  27. errorMessage.AppendLine($"🟥\t{failedRule.Profile}");
  28. errorMessage.AppendLine($"\t{failedRule.Specification}");
  29. errorMessage.AppendLine($"\t{failedRule.Clause}");
  30. errorMessage.AppendLine($"\t{failedRule.Description}");
  31. errorMessage.AppendLine();
  32. }
  33. return errorMessage.ToString();
  34. }
  35. }
  36. public static void TestConformanceWithVeraPdf(this IDocument document)
  37. {
  38. var filePath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.pdf");
  39. document.GeneratePdf(filePath);
  40. var result = RunVeraPDF(filePath);
  41. if (!result.IsDocumentValid)
  42. {
  43. Console.WriteLine(result.GetErrorMessage());
  44. Assert.Fail();
  45. }
  46. File.Delete(filePath);
  47. }
  48. private static ValidationResult RunVeraPDF(string pdfFilePath)
  49. {
  50. if (!File.Exists(pdfFilePath))
  51. throw new FileNotFoundException($"PDF file not found: {pdfFilePath}");
  52. var arguments = $"--format json \"{pdfFilePath}\"";
  53. var process = new Process
  54. {
  55. StartInfo = new ProcessStartInfo
  56. {
  57. FileName = "verapdf",
  58. Arguments = arguments,
  59. RedirectStandardOutput = true,
  60. RedirectStandardError = true,
  61. UseShellExecute = false,
  62. CreateNoWindow = true
  63. }
  64. };
  65. process.Start();
  66. var output = process.StandardOutput.ReadToEnd();
  67. process.WaitForExit();
  68. var result = new ValidationResult();
  69. var profileResults = JsonDocument
  70. .Parse(output)
  71. .RootElement
  72. .GetProperty("report")
  73. .GetProperty("jobs")[0]
  74. .GetProperty("validationResult");
  75. foreach (var profileValidationResult in profileResults.EnumerateArray())
  76. {
  77. var failedRules = profileValidationResult
  78. .GetProperty("details")
  79. .GetProperty("ruleSummaries");
  80. foreach (var failedRule in failedRules.EnumerateArray())
  81. {
  82. result.FailedRules.Add(new ValidationResult.FailedRule
  83. {
  84. Profile = profileValidationResult.GetProperty("profileName").GetString().Split(" ").First(),
  85. Specification = failedRule.GetProperty("specification").GetString(),
  86. Clause = failedRule.GetProperty("clause").GetString(),
  87. Description = failedRule.GetProperty("description").GetString()
  88. });
  89. }
  90. }
  91. return result;
  92. }
  93. }