Implemented mapping XML validation result items
This commit is contained in:
parent
2e70c83358
commit
058fb62849
|
@ -2,19 +2,54 @@ package net.codinux.invoicing.validation
|
|||
|
||||
import org.mustangproject.validator.ZUGFeRDValidator
|
||||
import java.io.File
|
||||
import java.lang.reflect.Field
|
||||
|
||||
class EInvoiceValidator {
|
||||
|
||||
companion object {
|
||||
private val SectionField = getPrivateField("section")
|
||||
private val CriterionField = getPrivateField("criterion")
|
||||
private val StacktraceField = getPrivateField("stacktrace")
|
||||
|
||||
private fun getPrivateField(fieldName: String): Field? = try {
|
||||
org.mustangproject.validator.ValidationResultItem::class.java.getDeclaredField(fieldName).apply {
|
||||
trySetAccessible()
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun validate(fileToValidate: File, disableNotices: Boolean = false): InvoiceValidationResult {
|
||||
val validator = ZUGFeRDValidator()
|
||||
val validator = object : ZUGFeRDValidator() {
|
||||
fun getContext() = this.context
|
||||
}
|
||||
|
||||
if (disableNotices) {
|
||||
validator.disableNotices()
|
||||
}
|
||||
|
||||
// TODO: this is far from ideal to have to report only as string and not the single failures as objects
|
||||
val report = validator.validate(fileToValidate.absolutePath)
|
||||
|
||||
return InvoiceValidationResult(validator.wasCompletelyValid(), report)
|
||||
val context = validator.getContext()
|
||||
val isXmlValid = context.isValid
|
||||
val xmlValidationResults = context.results.map { mapValidationResultItem(it) }
|
||||
|
||||
// TODO: currently it's not possible to get PDF validation result as for PDF validation the same context object
|
||||
// is used and then in a private method before XML validation context.clear() gets called removing all PDF validation results
|
||||
|
||||
return InvoiceValidationResult(validator.wasCompletelyValid(), isXmlValid, xmlValidationResults, report)
|
||||
}
|
||||
|
||||
private fun mapValidationResultItem(item: org.mustangproject.validator.ValidationResultItem) =
|
||||
ValidationResultItem(mapSeverity(item), item.message, item.location, SectionField?.get(item) as? Int, CriterionField?.get(item) as? String, StacktraceField?.get(item) as? String)
|
||||
|
||||
private fun mapSeverity(item: org.mustangproject.validator.ValidationResultItem): ValidationResultSeverity {
|
||||
var name = item.severity.name
|
||||
name = name.first().uppercase() + name.substring(1)
|
||||
|
||||
return ValidationResultSeverity.valueOf(name)
|
||||
}
|
||||
|
||||
}
|
|
@ -1,11 +1,31 @@
|
|||
package net.codinux.invoicing.validation
|
||||
|
||||
class InvoiceValidationResult(
|
||||
/**
|
||||
* If XML and, if supplied, PDF is valid.
|
||||
*/
|
||||
val isValid: Boolean,
|
||||
val report: String
|
||||
/**
|
||||
* If eInvoice XML is valid. If PDF is invalid, then [isValid] is false, but [isXmlValid] still can be true.
|
||||
*/
|
||||
val isXmlValid: Boolean,
|
||||
/**
|
||||
*
|
||||
*/
|
||||
val xmlValidationResults: List<ValidationResultItem>,
|
||||
/**
|
||||
* The validation report as a custom XML.
|
||||
*/
|
||||
val reportAsXml: String
|
||||
) {
|
||||
val countXmlNotices: Int by lazy { xmlValidationResults.count { it.severity == ValidationResultSeverity.Notice } }
|
||||
|
||||
val countXmlErrors: Int by lazy { xmlValidationResults.count { it.severity == ValidationResultSeverity.Error } }
|
||||
|
||||
val countXmlFatalOrExcepton: Int by lazy { xmlValidationResults.count { it.severity == ValidationResultSeverity.Fatal || it.severity == ValidationResultSeverity.Exception } }
|
||||
|
||||
override fun toString() = when (isValid) {
|
||||
true -> "Valid: $report"
|
||||
false -> "Invalid: $report"
|
||||
true -> "Valid: $reportAsXml"
|
||||
false -> "Invalid: $reportAsXml"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package net.codinux.invoicing.validation
|
||||
|
||||
class ValidationResultItem(
|
||||
val severity: ValidationResultSeverity,
|
||||
val message: String,
|
||||
val location: String?,
|
||||
val section: Int?,
|
||||
val criterion: String?,
|
||||
val stacktrace: String? = null
|
||||
) {
|
||||
override fun toString() = "$severity: $message"
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package net.codinux.invoicing.validation
|
||||
|
||||
enum class ValidationResultSeverity {
|
||||
Notice,
|
||||
Warning,
|
||||
Error,
|
||||
Fatal,
|
||||
Exception
|
||||
}
|
|
@ -42,7 +42,7 @@ class Demonstration {
|
|||
val result = validator.validate(invoiceFile)
|
||||
|
||||
println("Is valid? ${result.isValid}")
|
||||
println(result.report)
|
||||
println(result.reportAsXml)
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package net.codinux.invoicing.validation
|
||||
|
||||
import assertk.assertThat
|
||||
import assertk.assertions.isFalse
|
||||
import assertk.assertions.isNotEmpty
|
||||
import assertk.assertions.isTrue
|
||||
import assertk.assertions.*
|
||||
import java.io.File
|
||||
import kotlin.test.Test
|
||||
|
||||
|
@ -19,7 +17,11 @@ class EInvoiceValidatorTest {
|
|||
val result = underTest.validate(testFile)
|
||||
|
||||
assertThat(result.isValid).isFalse() // TODO: add required properties to XRechnung.xml
|
||||
assertThat(result.report).isNotEmpty()
|
||||
assertThat(result.reportAsXml).isNotEmpty()
|
||||
assertThat(result.xmlValidationResults).hasSize(3)
|
||||
assertThat(result.countXmlNotices).isEqualTo(0)
|
||||
assertThat(result.countXmlErrors).isEqualTo(3)
|
||||
assertThat(result.countXmlFatalOrExcepton).isEqualTo(0)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -28,8 +30,13 @@ class EInvoiceValidatorTest {
|
|||
|
||||
val result = underTest.validate(testFile)
|
||||
|
||||
|
||||
assertThat(result.isValid).isTrue()
|
||||
assertThat(result.report).isNotEmpty()
|
||||
assertThat(result.reportAsXml).isNotEmpty()
|
||||
assertThat(result.xmlValidationResults).hasSize(5)
|
||||
assertThat(result.countXmlNotices).isEqualTo(5)
|
||||
assertThat(result.countXmlErrors).isEqualTo(0)
|
||||
assertThat(result.countXmlFatalOrExcepton).isEqualTo(0)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -39,7 +46,11 @@ class EInvoiceValidatorTest {
|
|||
val result = underTest.validate(testFile)
|
||||
|
||||
assertThat(result.isValid).isTrue()
|
||||
assertThat(result.report).isNotEmpty()
|
||||
assertThat(result.reportAsXml).isNotEmpty()
|
||||
assertThat(result.xmlValidationResults).hasSize(5)
|
||||
assertThat(result.countXmlNotices).isEqualTo(5)
|
||||
assertThat(result.countXmlErrors).isEqualTo(0)
|
||||
assertThat(result.countXmlFatalOrExcepton).isEqualTo(0)
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue