Skip to content

Commit

Permalink
add reference and search to str_map
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenreup committed Feb 1, 2024
1 parent ab836af commit 5791c65
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 13 deletions.
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 - [email protected] 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)
}
}

0 comments on commit 5791c65

Please sign in to comment.