Skip to content

Commit

Permalink
Test closure of resources without a base resource.
Browse files Browse the repository at this point in the history
Signed-off-by: Lentumunai-Mark <lentumunai.mark@students.jkuat.ac.ke>
  • Loading branch information
Lentumunai-Mark committed Sep 10, 2024
1 parent bff5e19 commit 74a41b8
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Condition
import org.hl7.fhir.r4.model.ContactPoint
import org.hl7.fhir.r4.model.DateTimeType
import org.hl7.fhir.r4.model.Encounter
import org.hl7.fhir.r4.model.Enumerations
import org.hl7.fhir.r4.model.Group
import org.hl7.fhir.r4.model.HumanName
import org.hl7.fhir.r4.model.Location
import org.hl7.fhir.r4.model.Organization
import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.Period
import org.hl7.fhir.r4.model.Procedure
import org.hl7.fhir.r4.model.Reference
import org.hl7.fhir.r4.model.RelatedPerson
Expand All @@ -80,6 +82,7 @@ import org.smartregister.fhircore.engine.app.fakes.Faker
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.configuration.UniqueIdAssignmentConfig
import org.smartregister.fhircore.engine.configuration.app.ConfigService
import org.smartregister.fhircore.engine.configuration.event.EventTriggerCondition
import org.smartregister.fhircore.engine.configuration.event.EventWorkflow
import org.smartregister.fhircore.engine.configuration.event.UpdateWorkflowValueConfig
import org.smartregister.fhircore.engine.configuration.profile.ManagingEntityConfig
Expand Down Expand Up @@ -887,6 +890,90 @@ class DefaultRepositoryTest : RobolectricTest() {
coVerify(exactly = 0) { fhirEngine.update(any()) }
}

@Test
fun testNonCareplanRelatedResourcesUpdatedCorrectlyAndWithNoSpecifiedBaseResource() = runTest {
val encounter =
Encounter().apply {
id = "test-Encounter"
period = Period().apply { start = Date().plusDays(-2) }
status = Encounter.EncounterStatus.INPROGRESS
type =
listOf(
CodeableConcept().apply {
coding =
listOf(
Coding().apply {
system = "http://smartregister.org/"
code = "SVISIT"
display = "Service Point Visit"
},
)
text = "Service Point Visit"
},
)
}
val eventWorkflow =
EventWorkflow(
triggerConditions =
listOf(
EventTriggerCondition(
eventResourceId = "encounterToBeClosed",
matchAll = false,
conditionalFhirPathExpressions =
listOf(
"true",
),
),
),
eventResources =
listOf(
ResourceConfig(
id = "encounterToBeClosed",
resource = ResourceType.Encounter,
configRules = listOf(),
dataQueries =
listOf(
DataQuery(
paramName = "reason-code",
filterCriteria =
listOf(
FilterCriterionConfig.TokenFilterCriterionConfig(
dataType = Enumerations.DataType.CODEABLECONCEPT,
value = Code(system = "http://smartregister.org/", code = "SVISIT"),
),
),
),
),
),
),
updateValues =
listOf(
UpdateWorkflowValueConfig(
jsonPathExpression = "Encounter.status",
value = JsonPrimitive("finished"),
resourceType = ResourceType.Encounter,
),
),
resourceFilterExpressions =
listOf(
ResourceFilterExpression(
conditionalFhirPathExpressions = listOf("Encounter.period.end < now()"),
matchAll = true,
),
),
)
fhirEngine.create(encounter)
defaultRepository.updateResourcesRecursively(
resourceConfig = eventWorkflow.eventResources[0],
eventWorkflow = eventWorkflow
)
val resourceSlot = slot<Encounter>()
val captured = resourceSlot.captured
coVerify { fhirEngine.update(capture(resourceSlot)) }
Assert.assertEquals("test-Encounter", captured.id)
Assert.assertEquals(Encounter.EncounterStatus.FINISHED, captured.status)
}

@Test
fun testUpdateResourcesRecursivelyClosesResource() = runTest {
val patient =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import javax.inject.Inject
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.hl7.fhir.r4.model.CarePlan
import org.hl7.fhir.r4.model.Encounter
import org.hl7.fhir.r4.model.Period
import org.hl7.fhir.r4.model.Reference
import org.hl7.fhir.r4.model.Task
Expand Down Expand Up @@ -282,22 +281,4 @@ class FhirResourceUtilTest : RobolectricTest() {

assertEquals(TaskStatus.REQUESTED, task.status)
}

@Test
fun testCloseCustomResources() {
val encounter =
Encounter().apply {
id = "test-Encounter"
period = Period().apply { start = Date().plusDays(-2) }
status = Encounter.EncounterStatus.INPROGRESS
}

runBlocking { fhirEngine.create(encounter) }

coEvery { defaultRepository.update(any()) } just runs

assertEquals(Encounter.EncounterStatus.INPROGRESS, encounter.status)

runBlocking { fhirResourceUtil.closeCustomResources() }
}
}
48 changes: 48 additions & 0 deletions android/quest/src/test/assets/configs/app/application_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,54 @@
"QuestionnaireResponse"
]
},
"eventWorkflows": [
{
"eventType": "RESOURCE_CLOSURE",
"triggerConditions": [
{
"eventResourceId": "encounterToBeClosed",
"matchAll": false,
"conditionalFhirPathExpressions": [
"true"
]
}
],
"eventResources": [
{
"id": "encounterToBeClosed",
"resource": "Encounter",
"configRules": [],
"dataQueries": [
{
"paramName": "reason-code",
"filterCriteria": [
{
"dataType": "CODEABLECONCEPT",
"value": {
"system": "http://smartregister.org/",
"code": "SVISIT"
}
}
]
}
]
}
],
"updateValues": [
{
"jsonPathExpression": "Encounter.status",
"value": "finished",
"resourceType": "Encounter"
}
],
"resourceFilterExpression": {
"conditionalFhirPathExpressions": [
"Encounter.period.end < now()"
],
"matchAll": true
}
}
],
"logGpsLocation": [
"QUESTIONNAIRE"
]
Expand Down

0 comments on commit 74a41b8

Please sign in to comment.