|
| 1 | +/* |
| 2 | + * Copyright 2021-2024 Ona Systems, Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.smartregister.fhircore.engine.util.extension |
| 18 | + |
| 19 | +import ca.uhn.fhir.validation.FhirValidator |
| 20 | +import dagger.hilt.android.testing.HiltAndroidRule |
| 21 | +import dagger.hilt.android.testing.HiltAndroidTest |
| 22 | +import io.mockk.spyk |
| 23 | +import io.mockk.verify |
| 24 | +import javax.inject.Inject |
| 25 | +import kotlinx.coroutines.test.runTest |
| 26 | +import org.hl7.fhir.instance.model.api.IBaseResource |
| 27 | +import org.hl7.fhir.r4.model.CarePlan |
| 28 | +import org.hl7.fhir.r4.model.Patient |
| 29 | +import org.hl7.fhir.r4.model.Reference |
| 30 | +import org.junit.Assert |
| 31 | +import org.junit.Before |
| 32 | +import org.junit.Rule |
| 33 | +import org.junit.Test |
| 34 | +import org.smartregister.fhircore.engine.robolectric.RobolectricTest |
| 35 | + |
| 36 | +@HiltAndroidTest |
| 37 | +class FhirValidatorExtensionTest : RobolectricTest() { |
| 38 | + |
| 39 | + @get:Rule var hiltRule = HiltAndroidRule(this) |
| 40 | + |
| 41 | + @Inject lateinit var validator: FhirValidator |
| 42 | + |
| 43 | + @Before |
| 44 | + fun setUp() { |
| 45 | + hiltRule.inject() |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun testCheckResourceValidRunsNoValidationWhenBuildTypeIsNotDebug() = runTest { |
| 50 | + val basicResource = CarePlan() |
| 51 | + val fhirValidatorSpy = spyk(validator) |
| 52 | + val results = fhirValidatorSpy.checkResourceValid(basicResource, isDebug = false) |
| 53 | + Assert.assertTrue(results.isEmpty()) |
| 54 | + verify(exactly = 0) { fhirValidatorSpy.validateWithResult(any<IBaseResource>()) } |
| 55 | + verify(exactly = 0) { fhirValidatorSpy.validateWithResult(any<String>()) } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun testCheckResourceValidValidatesResourceStructureWhenCarePlanResourceInvalid() = runTest { |
| 60 | + val basicCarePlan = CarePlan() |
| 61 | + val results = validator.checkResourceValid(basicCarePlan) |
| 62 | + Assert.assertFalse(results.isEmpty()) |
| 63 | + Assert.assertTrue( |
| 64 | + results.any { |
| 65 | + it.errorMessages.contains( |
| 66 | + "CarePlan.status: minimum required = 1, but only found 0", |
| 67 | + ignoreCase = true, |
| 68 | + ) |
| 69 | + }, |
| 70 | + ) |
| 71 | + Assert.assertTrue( |
| 72 | + results.any { |
| 73 | + it.errorMessages.contains( |
| 74 | + "CarePlan.intent: minimum required = 1, but only found 0", |
| 75 | + ignoreCase = true, |
| 76 | + ) |
| 77 | + }, |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun testCheckResourceValidValidatesReferenceType() = runTest { |
| 83 | + val carePlan = |
| 84 | + CarePlan().apply { |
| 85 | + status = CarePlan.CarePlanStatus.ACTIVE |
| 86 | + intent = CarePlan.CarePlanIntent.PLAN |
| 87 | + subject = Reference("Task/unknown") |
| 88 | + } |
| 89 | + val results = validator.checkResourceValid(carePlan) |
| 90 | + Assert.assertFalse(results.isEmpty()) |
| 91 | + Assert.assertEquals(1, results.size) |
| 92 | + Assert.assertTrue( |
| 93 | + results |
| 94 | + .first() |
| 95 | + .errorMessages |
| 96 | + .contains( |
| 97 | + "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", |
| 98 | + ignoreCase = true, |
| 99 | + ), |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun testCheckResourceValidValidatesReferenceWithNoType() = runTest { |
| 105 | + val carePlan = |
| 106 | + CarePlan().apply { |
| 107 | + status = CarePlan.CarePlanStatus.ACTIVE |
| 108 | + intent = CarePlan.CarePlanIntent.PLAN |
| 109 | + subject = Reference("unknown") |
| 110 | + } |
| 111 | + val results = validator.checkResourceValid(carePlan) |
| 112 | + Assert.assertFalse(results.isEmpty()) |
| 113 | + Assert.assertEquals(1, results.size) |
| 114 | + Assert.assertTrue( |
| 115 | + results |
| 116 | + .first() |
| 117 | + .errorMessages |
| 118 | + .contains( |
| 119 | + "The syntax of the reference 'unknown' looks incorrect, and it should be checked - CarePlan.subject", |
| 120 | + ignoreCase = true, |
| 121 | + ), |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + fun testCheckResourceValidValidatesResourceCorrectly() = runTest { |
| 127 | + val patient = Patient() |
| 128 | + val carePlan = |
| 129 | + CarePlan().apply { |
| 130 | + status = CarePlan.CarePlanStatus.ACTIVE |
| 131 | + intent = CarePlan.CarePlanIntent.PLAN |
| 132 | + subject = Reference(patient) |
| 133 | + } |
| 134 | + val results = validator.checkResourceValid(carePlan) |
| 135 | + Assert.assertFalse(results.isEmpty()) |
| 136 | + Assert.assertEquals(1, results.size) |
| 137 | + Assert.assertTrue(results.first().errorMessages.isBlank()) |
| 138 | + } |
| 139 | +} |
0 commit comments