Skip to content

Commit

Permalink
Minor fix in care plan generation (#2223)
Browse files Browse the repository at this point in the history
* Minor fix in care plan generation

* Add tests

* Update tests

* Update tests

* Update the dummy expression string

* Apply suggestions from code review

* Update android/engine/src/test/java/org/smartregister/fhircore/engine/task/FhirCarePlanGeneratorTest.kt

---------

Co-authored-by: Qazi Abubakar <[email protected]>
Co-authored-by: Peter Lubell-Doughtie <[email protected]>
  • Loading branch information
3 people authored Apr 8, 2023
1 parent ca6e629 commit 6f6163b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,20 @@ constructor(
if (definition.hasDynamicValue()) {
definition.dynamicValue.forEach { dynamicValue ->
if (definition.kind == ActivityDefinition.ActivityDefinitionKind.CAREPLAN)
dynamicValue.expression.expression
dynamicValue
.expression
.expression
.let { fhirPathEngine.evaluate(null, input, planDefinition, subject, it) }
?.takeIf { it.isNotEmpty() }
?.let { evaluatedValue ->
// TODO handle cases where we explicitly need to set previous value as null, when
// passing null to Terser, it gives error NPE
Timber.d("${dynamicValue.path}, evaluatedValue: $evaluatedValue")
TerserUtil.setFieldByFhirPath(
FhirContext.forR4Cached(),
dynamicValue.path.removePrefix("${definition.kind.display}."),
output,
evaluatedValue.firstOrNull()
evaluatedValue.first()
)
}
else throw UnsupportedOperationException("${definition.kind} not supported")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import org.hl7.fhir.r4.model.CodeableConcept
import org.hl7.fhir.r4.model.DateTimeType
import org.hl7.fhir.r4.model.DateType
import org.hl7.fhir.r4.model.Encounter
import org.hl7.fhir.r4.model.Expression
import org.hl7.fhir.r4.model.Group
import org.hl7.fhir.r4.model.Immunization
import org.hl7.fhir.r4.model.Patient
Expand Down Expand Up @@ -1137,6 +1138,29 @@ class FhirCarePlanGeneratorTest : RobolectricTest() {
}
}

@Test
@ExperimentalCoroutinesApi
fun `test generateOrUpdateCarePlan returns success even when evaluatedValue is null`() =
runBlocking {
val planDefinitionResources =
loadPlanDefinitionResources("child-immunization-schedule", listOf("register-temp"))
val planDefinition = planDefinitionResources.planDefinition
val patient = planDefinitionResources.patient
val data = Bundle().addEntry(Bundle.BundleEntryComponent().apply { resource = patient })

val dynamicValue = planDefinition.action.first().dynamicValue
val expressionValue = dynamicValue.find { it.expression.expression == "%rootResource.title" }

// Update the value of the expression
expressionValue?.let { it.expression = Expression().apply { expression = "dummyExpression" } }

// call the method under test and get the result
val result = fhirCarePlanGenerator.generateOrUpdateCarePlan(planDefinition, patient, data)

// assert that the result is not null
assertNotNull(result)
}

@Test
@ExperimentalCoroutinesApi
fun `Generate CarePlan should generate child immunization schedule with correct groupIdentifier value`() =
Expand Down

0 comments on commit 6f6163b

Please sign in to comment.