-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate extracted fhir resources while in debug
- Loading branch information
Showing
10 changed files
with
436 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirValidatorModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2021-2024 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.smartregister.fhircore.engine.di | ||
|
||
import ca.uhn.fhir.context.FhirContext | ||
import ca.uhn.fhir.context.support.DefaultProfileValidationSupport | ||
import ca.uhn.fhir.context.support.IValidationSupport | ||
import ca.uhn.fhir.validation.FhirValidator | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
import org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService | ||
import org.hl7.fhir.common.hapi.validation.support.InMemoryTerminologyServerValidationSupport | ||
import org.hl7.fhir.common.hapi.validation.support.UnknownCodeSystemWarningValidationSupport | ||
import org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain | ||
import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
class FhirValidatorModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideFhirValidator(): FhirValidator { | ||
val fhirContext = FhirContext.forR4() | ||
|
||
val validationSupportChain = | ||
ValidationSupportChain( | ||
DefaultProfileValidationSupport(fhirContext), | ||
InMemoryTerminologyServerValidationSupport(fhirContext), | ||
CommonCodeSystemsTerminologyService(fhirContext), | ||
UnknownCodeSystemWarningValidationSupport(fhirContext).apply { | ||
setNonExistentCodeSystemSeverity(IValidationSupport.IssueSeverity.WARNING) | ||
}, | ||
) | ||
val instanceValidator = FhirInstanceValidator(validationSupportChain) | ||
instanceValidator.isAssumeValidRestReferences = true | ||
instanceValidator.invalidateCaches() | ||
return fhirContext.newValidator().apply { registerValidatorModule(instanceValidator) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
.../src/main/java/org/smartregister/fhircore/engine/util/extension/FhirValidatorExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2021-2024 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.smartregister.fhircore.engine.util.extension | ||
|
||
import ca.uhn.fhir.validation.FhirValidator | ||
import ca.uhn.fhir.validation.ResultSeverityEnum | ||
import ca.uhn.fhir.validation.ValidationResult | ||
import kotlin.coroutines.coroutineContext | ||
import kotlinx.coroutines.withContext | ||
import org.hl7.fhir.r4.model.Resource | ||
import org.smartregister.fhircore.engine.BuildConfig | ||
|
||
suspend fun FhirValidator.checkResourceValid( | ||
vararg resource: Resource, | ||
isDebug: Boolean = BuildConfig.BUILD_TYPE.contains("debug", ignoreCase = true), | ||
): List<ValidationResult> { | ||
if (!isDebug) return emptyList() | ||
|
||
return withContext(coroutineContext) { | ||
resource.map { this@checkResourceValid.validateWithResult(it) } | ||
} | ||
} | ||
|
||
val ValidationResult.errorMessages | ||
get() = buildString { | ||
for (validationMsg in | ||
messages.filter { it.severity.ordinal >= ResultSeverityEnum.WARNING.ordinal }) { | ||
appendLine("${validationMsg.message} - ${validationMsg.locationString}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
.../test/java/org/smartregister/fhircore/engine/util/extension/FhirValidatorExtensionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright 2021-2024 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.smartregister.fhircore.engine.util.extension | ||
|
||
import ca.uhn.fhir.validation.FhirValidator | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import io.mockk.spyk | ||
import io.mockk.verify | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.test.runTest | ||
import org.hl7.fhir.instance.model.api.IBaseResource | ||
import org.hl7.fhir.r4.model.CarePlan | ||
import org.hl7.fhir.r4.model.Patient | ||
import org.hl7.fhir.r4.model.Reference | ||
import org.junit.Assert | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.smartregister.fhircore.engine.robolectric.RobolectricTest | ||
|
||
@HiltAndroidTest | ||
class FhirValidatorExtensionTest : RobolectricTest() { | ||
|
||
@get:Rule var hiltRule = HiltAndroidRule(this) | ||
|
||
@Inject lateinit var validator: FhirValidator | ||
|
||
@Before | ||
fun setUp() { | ||
hiltRule.inject() | ||
} | ||
|
||
@Test | ||
fun testCheckResourceValidRunsNoValidationWhenBuildTypeIsNotDebug() = runTest { | ||
val basicResource = CarePlan() | ||
val fhirValidatorSpy = spyk(validator) | ||
val results = fhirValidatorSpy.checkResourceValid(basicResource, isDebug = false) | ||
Assert.assertTrue(results.isEmpty()) | ||
verify(exactly = 0) { fhirValidatorSpy.validateWithResult(any<IBaseResource>()) } | ||
verify(exactly = 0) { fhirValidatorSpy.validateWithResult(any<String>()) } | ||
} | ||
|
||
@Test | ||
fun testCheckResourceValidValidatesResourceStructureWhenCarePlanResourceInvalid() = runTest { | ||
val basicCarePlan = CarePlan() | ||
val results = validator.checkResourceValid(basicCarePlan) | ||
Assert.assertFalse(results.isEmpty()) | ||
Assert.assertTrue( | ||
results.any { | ||
it.errorMessages.contains( | ||
"CarePlan.status: minimum required = 1, but only found 0", | ||
ignoreCase = true, | ||
) | ||
}, | ||
) | ||
Assert.assertTrue( | ||
results.any { | ||
it.errorMessages.contains( | ||
"CarePlan.intent: minimum required = 1, but only found 0", | ||
ignoreCase = true, | ||
) | ||
}, | ||
) | ||
} | ||
|
||
@Test | ||
fun testCheckResourceValidValidatesReferenceType() = runTest { | ||
val carePlan = | ||
CarePlan().apply { | ||
status = CarePlan.CarePlanStatus.ACTIVE | ||
intent = CarePlan.CarePlanIntent.PLAN | ||
subject = Reference("Task/unknown") | ||
} | ||
val results = validator.checkResourceValid(carePlan) | ||
Assert.assertFalse(results.isEmpty()) | ||
Assert.assertEquals(1, results.size) | ||
Assert.assertTrue( | ||
results | ||
.first() | ||
.errorMessages | ||
.contains( | ||
"The type 'Task' implied by the reference URL Task/unknown is not a valid Target for this element (must be one of [Group, Patient]) - CarePlan.subject", | ||
ignoreCase = true, | ||
), | ||
) | ||
} | ||
|
||
@Test | ||
fun testCheckResourceValidValidatesReferenceWithNoType() = runTest { | ||
val carePlan = | ||
CarePlan().apply { | ||
status = CarePlan.CarePlanStatus.ACTIVE | ||
intent = CarePlan.CarePlanIntent.PLAN | ||
subject = Reference("unknown") | ||
} | ||
val results = validator.checkResourceValid(carePlan) | ||
Assert.assertFalse(results.isEmpty()) | ||
Assert.assertEquals(1, results.size) | ||
Assert.assertTrue( | ||
results | ||
.first() | ||
.errorMessages | ||
.contains( | ||
"The syntax of the reference 'unknown' looks incorrect, and it should be checked - CarePlan.subject", | ||
ignoreCase = true, | ||
), | ||
) | ||
} | ||
|
||
@Test | ||
fun testCheckResourceValidValidatesResourceCorrectly() = runTest { | ||
val patient = Patient() | ||
val carePlan = | ||
CarePlan().apply { | ||
status = CarePlan.CarePlanStatus.ACTIVE | ||
intent = CarePlan.CarePlanIntent.PLAN | ||
subject = Reference(patient) | ||
} | ||
val results = validator.checkResourceValid(carePlan) | ||
Assert.assertFalse(results.isEmpty()) | ||
Assert.assertEquals(1, results.size) | ||
Assert.assertTrue(results.first().errorMessages.isBlank()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.