From 6f6163b2b3be766e46fdc258b09523f7bb8812ee Mon Sep 17 00:00:00 2001 From: qaziabubakar-vd <72507786+qaziabubakar-vd@users.noreply.github.com> Date: Sat, 8 Apr 2023 07:59:26 +0500 Subject: [PATCH] Minor fix in care plan generation (#2223) * 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 Co-authored-by: Peter Lubell-Doughtie --- .../engine/task/FhirCarePlanGenerator.kt | 10 ++++++-- .../engine/task/FhirCarePlanGeneratorTest.kt | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/task/FhirCarePlanGenerator.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/task/FhirCarePlanGenerator.kt index d91ac443b4..44a4a4354a 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/task/FhirCarePlanGenerator.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/task/FhirCarePlanGenerator.kt @@ -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") diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/task/FhirCarePlanGeneratorTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/task/FhirCarePlanGeneratorTest.kt index 569b49a0ed..65419756c9 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/task/FhirCarePlanGeneratorTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/task/FhirCarePlanGeneratorTest.kt @@ -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 @@ -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`() =