Skip to content

Commit

Permalink
Merge pull request #423 from statisticsnorway/Barnevern_bugfix_xsd
Browse files Browse the repository at this point in the history
Barnevern: Add TiltaksgrunnlagType
  • Loading branch information
jonolehagemo authored Jan 13, 2024
2 parents 5f8f718 + e8bdf63 commit 8affc9f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class DefaultXmlStreamHandler(
validationErrors
}
} catch (thrown: Throwable) {
listOf(avgiverFileError)
listOf(avgiverFileError(thrown.message))
}

private fun processIndividElement(
Expand All @@ -78,7 +78,7 @@ class DefaultXmlStreamHandler(
validationErrors
}
} catch (thrown: Throwable) {
listOf(individFileError)
listOf(individFileError(thrown.message))
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ object FixedValidationErrors {
messageText = "Klarer ikke å lese fil. Får feilmeldingen: $exceptionMessage"
)

val individFileError = ValidationReportEntry(
fun individFileError(exceptionMessage: String?) = ValidationReportEntry(
severity = Severity.ERROR,
ruleName = IndividRuleId.INDIVID_01.title,
messageText = "Definisjon av Individ er feil i forhold til filspesifikasjonen"
messageText = "Definisjon av Individ er feil i forhold til filspesifikasjonen. $exceptionMessage"
)

val individMissingError = ValidationReportEntry(
Expand All @@ -24,10 +24,10 @@ object FixedValidationErrors {
messageText = "Filen mangler individer"
)

val avgiverFileError = ValidationReportEntry(
fun avgiverFileError(exceptionMessage: String?) = ValidationReportEntry(
severity = Severity.ERROR,
ruleName = AvgiverRuleId.AVGIVER_01.title,
messageText = "Klarer ikke å validere Avgiver mot filspesifikasjon"
messageText = "Klarer ikke å validere Avgiver mot filspesifikasjon. $exceptionMessage"
)

fun singleAvgiverError(found: Int) = ValidationReportEntry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ object XmlElementHandlers {
xmlStreamReader,
KostraIndividType::class.java
).let { individType ->
if (KostraValidationUtils.validate(
xmlReader = StringReader(KostraBarnevernConverter.marshallInstance(individType)),
xsdResource = KostraValidationUtils.INDIVID_XSD_RESOURCE
)
) IndividRules.individRules.mapNotNull { it.validate(individType, arguments) }.flatten()
.map { reportEntry ->
reportEntry.copy(
caseworker = individType.saksbehandler,
journalId = individType.journalnummer,
individId = individType.id
)
} to individType
else listOf(individFileError) to null
val (valid, errorMessage) = KostraValidationUtils.validate(
xmlReader = StringReader(KostraBarnevernConverter.marshallInstance(individType)),
xsdResource = KostraValidationUtils.INDIVID_XSD_RESOURCE
)
if (valid)
IndividRules.individRules
.mapNotNull { it.validate(individType, arguments) }
.flatten()
.map { reportEntry ->
reportEntry.copy(
caseworker = individType.saksbehandler,
journalId = individType.journalnummer,
individId = individType.id
)
} to individType
else {
listOf(individFileError(errorMessage)) to null
}
}
}

Expand All @@ -39,12 +44,15 @@ object XmlElementHandlers {
xmlStreamReader,
KostraAvgiverType::class.java
).let { avgiverType ->
if (KostraValidationUtils.validate(
xmlReader = StringReader(KostraBarnevernConverter.marshallInstance(avgiverType)),
xsdResource = KostraValidationUtils.AVGIVER_XSD_RESOURCE
)
) AvgiverRules.avgiverRules.mapNotNull { it.validate(avgiverType, arguments) }.flatten() to avgiverType
else listOf(avgiverFileError) to null
val (valid, errorMessage) = KostraValidationUtils.validate(
xmlReader = StringReader(KostraBarnevernConverter.marshallInstance(avgiverType)),
xsdResource = KostraValidationUtils.AVGIVER_XSD_RESOURCE
)
if (valid)
AvgiverRules.avgiverRules
.mapNotNull { it.validate(avgiverType, arguments) }
.flatten() to avgiverType
else listOf(avgiverFileError(errorMessage)) to null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import no.ssb.kostra.validation.report.Severity
import no.ssb.kostra.validation.report.ValidationReportEntry
import no.ssb.kostra.validation.rule.RuleTestData.argumentsInTest
import no.ssb.kostra.validation.rule.barnevern.xmlhandling.BarnevernXmlStreamHandler
import no.ssb.kostra.validation.rule.barnevern.xmlhandling.DefaultXmlStreamHandler
import no.ssb.kostra.validation.rule.barnevern.xmlhandling.FixedValidationErrors
import no.ssb.kostra.validation.rule.barnevern.xmlhandling.XmlElementHandlers
import java.io.File
import java.time.Year

class BarnevernValidatorTest : BehaviorSpec({
Expand Down Expand Up @@ -59,7 +62,8 @@ class BarnevernValidatorTest : BehaviorSpec({
ValidationReportEntry(
severity = Severity.ERROR,
ruleName = AvgiverRuleId.AVGIVER_01.title,
messageText = "Klarer ikke å validere Avgiver mot filspesifikasjon"
messageText = "Klarer ikke å validere Avgiver mot filspesifikasjon. " +
"cvc-datatype-valid.1.2.1: '42' is not a valid value for 'gYear'."
), 51
),
row(
Expand All @@ -70,7 +74,9 @@ class BarnevernValidatorTest : BehaviorSpec({
ValidationReportEntry(
severity = Severity.ERROR,
ruleName = IndividRuleId.INDIVID_01.title,
messageText = "Definisjon av Individ er feil i forhold til filspesifikasjonen"
messageText = """Definisjon av Individ er feil i forhold til filspesifikasjonen. """ +
"""cvc-pattern-valid: Value 'abc' is not facet-valid with respect to pattern '\d{11}' """ +
"""for type '#AnonType_FodselsnummerIndividType'."""
), 4
),
row(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class DefaultXmlStreamHandlerTest : BehaviorSpec({

Then("result should be as expected") {
result.shouldNotBeNull()
result.shouldContain(avgiverFileError)
result.shouldContain(avgiverFileError("null"))
}
}

Expand All @@ -94,7 +94,7 @@ class DefaultXmlStreamHandlerTest : BehaviorSpec({

Then("result should be as expected") {
result.shouldNotBeNull()
result.shouldContain(individFileError)
result.shouldContain(individFileError("null"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ object KostraValidationUtils {
private const val DISALLOW_DOCTYPE_DECL = "http://apache.org/xml/features/disallow-doctype-decl"

@JvmStatic
fun validate(xml: String): Boolean = validate(StringReader(xml))
fun validate(xml: String): Boolean = validate(StringReader(xml)).first

@JvmStatic
fun validate(xmlReader: Reader, xsdResource: String = KOSTRA_BARNEVERN_XSD_RESOURCE): Boolean = try {
fun validate(xmlReader: Reader, xsdResource: String = KOSTRA_BARNEVERN_XSD_RESOURCE): Pair<Boolean, String> = try {
getSchemaValidator(xsdResource).validate(StreamSource(xmlReader))
true
true to ""
} catch (thrown: SAXParseException) {
false
false to "${thrown.message}"
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.time.LocalDate


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TiltakType", propOrder = ["lovhjemmel", "jmfrLovhjemmel", "kategori", "opphevelse"])
@XmlType(name = "TiltakType", propOrder = ["lovhjemmel", "jmfrLovhjemmel", "kategori", "tiltaksgrunnlag", "opphevelse"])
data class KostraTiltakType(

@field:XmlAttribute(name = "Id", required = true)
Expand Down

0 comments on commit 8affc9f

Please sign in to comment.