Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MWCore] Enhance StructureMap transform service with Search and Reverse reference #3028

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ fun Reference.extractId(): String =
if (this.reference.isNullOrEmpty()) ""
else this.reference.substringAfterLast(delimiter = '/', missingDelimiterValue = "")

fun String.toReference(): Reference? {
return if (this.isEmpty()) null
else {
val parts = this.split("/")
if (parts.size != 2) return null
val resourceString = parts[0]
val resourceId = parts[1]
val resourceType = ResourceType.fromCode(resourceString)

return resourceId.asReference(resourceType)
}
}

fun String.asReference(resourceType: ResourceType): Reference {
val resourceId = this
return Reference().apply { reference = "${resourceType.name}/$resourceId" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

package org.smartregister.fhircore.engine.util.helper

import com.google.android.fhir.FhirEngine
import com.google.android.fhir.search.search
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.runBlocking
import org.hl7.fhir.exceptions.FHIRException
import org.hl7.fhir.r4.context.SimpleWorkerContext
import org.hl7.fhir.r4.model.Appointment
Expand All @@ -27,14 +30,17 @@ import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Encounter
import org.hl7.fhir.r4.model.EpisodeOfCare
import org.hl7.fhir.r4.model.Group
import org.hl7.fhir.r4.model.IdType
import org.hl7.fhir.r4.model.Immunization
import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.PlanDefinition
import org.hl7.fhir.r4.model.ResourceFactory
import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.RiskAssessment.RiskAssessmentPredictionComponent
import org.hl7.fhir.r4.model.Timing
import org.hl7.fhir.r4.terminologies.ConceptMapEngine
import org.hl7.fhir.r4.utils.StructureMapUtilities.ITransformerServices
import org.smartregister.fhircore.engine.util.extension.toReference
import timber.log.Timber

/**
Expand All @@ -47,7 +53,9 @@ import timber.log.Timber
* Immunization.Reaction
*/
@Singleton
class TransformSupportServices @Inject constructor(val simpleWorkerContext: SimpleWorkerContext) :
class TransformSupportServices
@Inject
constructor(val simpleWorkerContext: SimpleWorkerContext, val fhirEngine: FhirEngine) :
ITransformerServices {

val outputs: MutableList<Base> = mutableListOf()
Expand Down Expand Up @@ -89,11 +97,16 @@ class TransformSupportServices @Inject constructor(val simpleWorkerContext: Simp

@Throws(FHIRException::class)
override fun resolveReference(appContext: Any, url: String): Base {
throw FHIRException("resolveReference is not supported yet")
return runBlocking {
val reference = url.toReference()
IdType(reference?.reference).let {
fhirEngine.get(ResourceType.fromCode(it.resourceType), it.idPart)
}
}
}

@Throws(FHIRException::class)
override fun performSearch(appContext: Any, url: String): List<Base> {
throw FHIRException("performSearch is not supported yet")
return runBlocking { fhirEngine.search(url).map { it.resource } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ class ReferenceExtensionTest {
Assert.assertEquals("123456", result)
}

@Test
fun testExtractReferenceFromString() {
val ref = "Patient/123456"

val result = ref.toReference()

Assert.assertNotNull(result)
Assert.assertEquals("Patient/123456", result?.reference)
}

@Test
fun testStringAsReferenceShouldReturnReference() {
val ref = "123456"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@

package org.smartregister.fhircore.engine.util.helper

import com.google.android.fhir.FhirEngine
import com.google.android.fhir.SearchResult
import com.google.android.fhir.search.Search
import com.google.android.fhir.search.search
import io.mockk.coEvery
import io.mockk.mockk
import org.hl7.fhir.exceptions.FHIRException
import kotlin.test.assertEquals
import org.hl7.fhir.r4.model.Appointment
import org.hl7.fhir.r4.model.CarePlan
import org.hl7.fhir.r4.model.Encounter
import org.hl7.fhir.r4.model.EpisodeOfCare
import org.hl7.fhir.r4.model.Group
import org.hl7.fhir.r4.model.Immunization
import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.RiskAssessment
import org.hl7.fhir.r4.model.TimeType
import org.junit.Assert
Expand All @@ -34,12 +40,12 @@ import org.smartregister.fhircore.engine.robolectric.RobolectricTest

/** Created by Ephraim Kigamba - nek.eam@gmail.com on 24-09-2021. */
class TransformSupportServicesTest : RobolectricTest() {

lateinit var transformSupportServices: TransformSupportServices
private val fhirEngine = mockk<FhirEngine>()
private lateinit var transformSupportServices: TransformSupportServices

@Before
fun setUp() {
transformSupportServices = TransformSupportServices(mockk())
transformSupportServices = TransformSupportServices(mockk(), fhirEngine)
}

@Test
Expand Down Expand Up @@ -149,15 +155,28 @@ class TransformSupportServicesTest : RobolectricTest() {

@Test
fun `resolveReference() should throw FHIRException this is not supported yet when given url`() {
Assert.assertThrows("resolveReference is not supported yet", FHIRException::class.java) {
transformSupportServices.resolveReference("", "https://url.com")
}
coEvery { fhirEngine.get(ResourceType.Patient, "1234") } returns Patient().apply { id = "1234" }

val result = transformSupportServices.resolveReference("", "Patient/1234")

assertEquals(ResourceType.Patient.name, result.fhirType())
assertEquals("1234", result.idBase)
}

@Test
fun `performSearch() should throw FHIRException this is not supported yet when given url`() {
Assert.assertThrows("performSearch is not supported yet", FHIRException::class.java) {
transformSupportServices.performSearch("", "https://url.com")
}
coEvery { fhirEngine.search<Patient>(any<Search>()) } returns
listOf(
SearchResult(
resource = Patient().apply { id = "1234" },
included = null,
revIncluded = null
)
)

val result = transformSupportServices.performSearch("", "Patient")

assertEquals(result.size, 1)
assertEquals("1234", result.first().idBase)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import ca.uhn.fhir.context.FhirVersionEnum
import ca.uhn.fhir.parser.IParser
import dagger.hilt.android.testing.HiltTestApplication
import io.mockk.clearAllMocks
import io.mockk.mockk
import java.io.File
import java.io.FileReader
import java.util.Base64
Expand Down Expand Up @@ -122,7 +123,7 @@ abstract class RobolectricTest {
contextR4.setExpansionProfile(Parameters())
contextR4.isCanRunWithoutTerminology = true

val transformSupportServices = TransformSupportServices(contextR4)
val transformSupportServices = TransformSupportServices(contextR4, mockk())

return StructureMapUtilities(contextR4, transformSupportServices)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class SimpleDetailsActivityTest : RobolectricTest() {
contextR4.setExpansionProfile(Parameters())
contextR4.isCanRunWithoutTerminology = true

val transformSupportServices = TransformSupportServices(contextR4)
val transformSupportServices = TransformSupportServices(contextR4, mockk())

val scu = org.hl7.fhir.r4.utils.StructureMapUtilities(contextR4, transformSupportServices)
val map = scu.parse(g6pdStructureMap, "PatientRegistration")
Expand Down Expand Up @@ -127,7 +127,7 @@ class SimpleDetailsActivityTest : RobolectricTest() {
contextR4.setExpansionProfile(Parameters())
contextR4.isCanRunWithoutTerminology = true

val transformSupportServices = TransformSupportServices(contextR4)
val transformSupportServices = TransformSupportServices(contextR4, mockk())

val scu = org.hl7.fhir.r4.utils.StructureMapUtilities(contextR4, transformSupportServices)
val map = scu.parse(g6pdStructureMap, "TestResults")
Expand Down
Loading