Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: [AEA-0000] - refactor test file #203

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .mvn/jvm.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Xms3000m -Xmx3000m -DTEST_SYSTEM_PROP_VALIDATION_RESOURCE_CACHES_MS=9223372036854775807 -DUNIT_TEST_MODE=true
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.io.ClassPathResource
import java.util.*
import kotlin.streams.toList

@Configuration
class PackageConfiguration(val objectMapper: ObjectMapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ValidationConfiguration(private val implementationGuideParser: Implementat
@Bean
fun terminologyValidationSupport(fhirContext: FhirContext): InMemoryTerminologyServerValidationSupport {
return object : InMemoryTerminologyServerValidationSupport(fhirContext) {
@Synchronized
override fun validateCodeInValueSet(
theValidationSupportContext: ValidationSupportContext?,
theOptions: ConceptValidationOptions?,
Expand Down Expand Up @@ -134,7 +135,7 @@ class ValidationConfiguration(private val implementationGuideParser: Implementat
it.type.forEach{
if (it.hasTargetProfile())
it.targetProfile.forEach {
it.value = getBase(it.value, supportChain);
it.value = getBase(it.value, supportChain)
}
}
}
Expand All @@ -144,13 +145,13 @@ class ValidationConfiguration(private val implementationGuideParser: Implementat

private fun getBase(profile : String,supportChain: IValidationSupport): String? {
val structureDefinition : StructureDefinition=
supportChain.fetchStructureDefinition(profile) as StructureDefinition;
supportChain.fetchStructureDefinition(profile) as StructureDefinition
if (structureDefinition.hasBaseDefinition()) {
var baseProfile = structureDefinition.baseDefinition
if (baseProfile.contains(".uk")) baseProfile = getBase(baseProfile, supportChain)
return baseProfile
}
return null;
return null
}
private fun shouldGenerateSnapshot(structureDefinition: StructureDefinition): Boolean {
return !structureDefinition.hasSnapshot() && structureDefinition.derivation == StructureDefinition.TypeDerivationRule.CONSTRAINT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class ValidateController(
return fhirContext.newJsonParser().encodeResourceToString(result)
}

// use Synchronized here to ensure single thread as newJsonParser is not thread safe
@Synchronized
fun parseAndValidateResource(input: String): OperationOutcome {
return try {
val inputResource = fhirContext.newJsonParser().parseResource(input)
Expand All @@ -48,14 +50,28 @@ class ValidateController(
createOperationOutcome(e.message ?: "Invalid JSON", null)
}
}

fun validateResource(resource: IBaseResource): OperationOutcome? {
capabilityStatementApplier.applyCapabilityStatementProfiles(resource)
val messageDefinitionErrors = messageDefinitionApplier.applyMessageDefinition(resource)
if (messageDefinitionErrors != null) {
return messageDefinitionErrors
}
return validator.validateWithResult(resource).toOperationOutcome() as? OperationOutcome
validator.setConcurrentBundleValidation(true)
val result = validator.validateWithResult(resource).toOperationOutcome() as? OperationOutcome
var hasError = false
for (issue in result?.issue!!) {
if (issue.severity.equals(OperationOutcome.IssueSeverity.ERROR)) {
println("Error found checking file. Error: ${issue.diagnostics}")
hasError = true
}
}
if (hasError) {
val string_repr = fhirContext.newJsonParser().encodeResourceToString(resource)
println("There was an error")
println(string_repr)
}
return result
}

fun getResourcesToValidate(inputResource: IBaseResource?): List<IBaseResource> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ class MessageDefinitionApplier(
private fun findMessageHeader(bundle: Bundle): MessageHeader? {
return bundle.entry
?.map { it.resource }
?.filterIsInstance(MessageHeader::class.java)
?.filterIsInstance<MessageHeader>()
?.singleOrNull()
}

private fun findMessageDefinition(messageType: Coding, messageDefinitionProfile: String?): MessageDefinition? {

if (messageDefinitionProfile != null) {
return messageDefinitions
return if (messageDefinitionProfile != null) {
messageDefinitions
.filter { it.eventCoding.system == messageType.system }
.firstOrNull { it.eventCoding.code == messageType.code && it.url == messageDefinitionProfile }
.lastOrNull { it.eventCoding.code == messageType.code && it.url == messageDefinitionProfile }
} else {
return messageDefinitions
messageDefinitions
.filter { it.eventCoding.system == messageType.system }
.firstOrNull { it.eventCoding.code == messageType.code }
.lastOrNull { it.eventCoding.code == messageType.code }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.fhirvalidator

import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.fhirvalidator.controller

import org.hl7.fhir.r4.model.OperationOutcome
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.Named
import org.junit.jupiter.api.DisplayName
Expand All @@ -13,7 +13,6 @@ import org.springframework.beans.factory.annotation.Autowired
import org.opentest4j.AssertionFailedError
import java.io.File
import java.util.stream.Stream
import javax.print.attribute.standard.Severity

@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand All @@ -23,6 +22,16 @@ internal class FhirExamplesTest {
@Autowired
lateinit var testValidateController: ValidateController

@BeforeAll
fun initValidator() {
println("Priming the validator")
val primerPayload = loader.getResourceAsStream("examples/primary-care/acute/nominated-pharmacy/responsible-party-org/separate-telecom/1-Process-Request-Send-200_OK.json")
val lines = primerPayload.bufferedReader().readLines()
val fileContent = lines.joinToString(" ")
testValidateController.parseAndValidateResource(fileContent)
println("Validator primed")
}

fun getExampleFhirFiles(): Stream<Arguments> {
val dir = File( loader.getResource("examples").file)
val exampleFiles = dir.walk()
Expand Down
Loading
Loading