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] Add Resource Tagging #2499

Merged
merged 45 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
3e90564
rework auth items
sevenreup Apr 26, 2023
1d2b0e0
Merge branch 'mwcore-dev' into mwcore-migrate-auth-code
sevenreup May 26, 2023
3ed6fb2
init tagging
sevenreup Jun 5, 2023
ffff742
update register
sevenreup Jun 6, 2023
a4224b4
update
sevenreup Jun 6, 2023
6aaa2fd
update sync
sevenreup Jun 9, 2023
0b05614
Merge branch 'mwcore-dev' into mwcore-migrate-auth-code
sevenreup Jun 9, 2023
39564ac
update sync key
sevenreup Jun 9, 2023
dc34c49
fix location syncs
sevenreup Jun 18, 2023
639c7af
fix activity listener
sevenreup Jun 21, 2023
92536ca
test fixes
sevenreup Jun 26, 2023
c638406
fixes
sevenreup Jun 26, 2023
cea8e95
replace app with default
sevenreup Jun 27, 2023
7e5da06
tests fixed
sevenreup Jun 27, 2023
a72ff10
Merge branch 'mwcore-dev' into mwcore-migrate-auth-code
sevenreup Jun 27, 2023
2a34a3e
Merge branch 'mwcore-dev' into mwcore-migrate-auth-code
sevenreup Jun 27, 2023
2a28a52
stuff
sevenreup Jun 27, 2023
9991c0b
test fixes
sevenreup Jun 27, 2023
a525453
Merge branch 'mwcore-migrate-auth-code' of github.com:opensrp/fhircor…
sevenreup Jun 27, 2023
067b2ca
Update CqlContentTest.kt
sevenreup Jun 27, 2023
79f8267
add tests
sevenreup Jun 28, 2023
3718c03
add tests
sevenreup Jun 28, 2023
d0c116a
update appsettings
sevenreup Jun 29, 2023
cbef459
update
sevenreup Jul 4, 2023
9c2b9e9
update
sevenreup Jul 5, 2023
5b8c799
update QuestViewmodel
sevenreup Jul 11, 2023
b99b092
Update QuestionnaireActivity.kt
sevenreup Jul 12, 2023
eee0185
Update LoginViewModel.kt
sevenreup Jul 12, 2023
3807eb9
Update TokenAuthenticator.kt
sevenreup Jul 13, 2023
cf69b88
update auth
sevenreup Jul 17, 2023
39695d3
Update AppSettingActivityTest.kt
sevenreup Jul 17, 2023
c1de593
updates
sevenreup Jul 18, 2023
2d0962b
update resource tags
sevenreup Jul 20, 2023
cb62edf
block to finish saving data
sevenreup Jul 26, 2023
1c099a5
update
sevenreup Jul 26, 2023
518b38a
spotless run
sevenreup Jul 26, 2023
59d95d4
Update network_security_config.xml
sevenreup Jul 28, 2023
49fc301
Merge branch 'mwcore-dev' into mwcore-migrate-auth-code
LZRS Jul 28, 2023
183cc6a
Fix failed AppNotIdleException for some Compose tests
LZRS Jul 30, 2023
3c91f1b
Disable catching of non-test related exceptions
LZRS Jul 30, 2023
875eb8f
add tests
sevenreup Jul 31, 2023
0631d83
Update SharedPreferencesHelperTest.kt
sevenreup Jul 31, 2023
1002bc7
Merge branch 'mwcore-dev' into mwcore-migrate-auth-code
sevenreup Jul 31, 2023
7885fa2
Update AndroidExtensions.kt
sevenreup Jul 31, 2023
3449870
update tests
sevenreup Aug 1, 2023
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
Prev Previous commit
Next Next commit
stuff
  • Loading branch information
sevenreup committed Jun 27, 2023
commit 2a28a52c9e862132c9974281f44a8abd67375aa7
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ import org.smartregister.fhircore.engine.data.remote.shared.TokenAuthenticator
import org.smartregister.fhircore.engine.data.remote.shared.TokenAuthenticator.Companion.AUTH_TOKEN_TYPE
import org.smartregister.fhircore.engine.ui.login.LoginActivity
import org.smartregister.fhircore.engine.util.SecureSharedPreference
import org.smartregister.fhircore.engine.util.extension.getActivity
import org.smartregister.fhircore.engine.util.extension.launchActivityWithNoBackStackHistory
import retrofit2.HttpException
import timber.log.Timber

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import dagger.hilt.android.testing.UninstallModules
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.spyk
import io.mockk.verify
import java.io.InterruptedIOException
Expand Down Expand Up @@ -118,7 +116,7 @@ class BaseRegisterActivityTest : ActivityRobolectricTest() {
every { secureSharedPreference.retrieveSessionUsername() } returns "demo"
every { secureSharedPreference.retrieveCredentials() } returns FakeModel.authCredentials
every { secureSharedPreference.deleteCredentials() } returns Unit
every { accountAuthenticator.logout(any()) } returns Unit
every { accountAuthenticator.logout(any()) } returns Unit

ApplicationProvider.getApplicationContext<Context>().apply { setTheme(R.style.AppTheme) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ constructor(

fun onEvent(event: AppMainEvent) {
when (event) {
is AppMainEvent.Logout -> accountAuthenticator.logout {
event.context.getActivity()?.launchActivityWithNoBackStackHistory<LoginActivity>()
}
is AppMainEvent.Logout ->
accountAuthenticator.logout {
event.context.getActivity()?.launchActivityWithNoBackStackHistory<LoginActivity>()
}
is AppMainEvent.SwitchLanguage -> {
sharedPreferencesHelper.write(SharedPreferenceKey.LANG.name, event.language.tag)
event.context.run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,93 +20,106 @@ import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.logicalId
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.slot
import io.mockk.spyk
import kotlinx.coroutines.runBlocking
import org.cqframework.cql.cql2elm.CqlTranslator
import org.cqframework.cql.cql2elm.FhirLibrarySourceProvider
import org.cqframework.cql.cql2elm.LibraryManager
import org.hl7.fhir.r4.model.Bundle
import org.hl7.fhir.r4.model.DataRequirement
import org.hl7.fhir.r4.model.Library
import org.hl7.fhir.r4.model.Observation
import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.smartregister.fhircore.engine.cql.LibraryEvaluator
import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.util.DefaultDispatcherProvider
import org.smartregister.fhircore.quest.robolectric.RobolectricTest

@HiltAndroidTest
class CqlContentTest : RobolectricTest() {
@get:Rule
var hiltRule = HiltAndroidRule(this)
val fhirContext: FhirContext = FhirContext.forCached(FhirVersionEnum.R4)
val parser = fhirContext.newJsonParser()!!
val evaluator = LibraryEvaluator().apply { initialize() }

@Before
fun setUp() {
hiltRule.inject()
}

@Test
fun runCqlLibraryTestForPqMedication() {
val resourceDir = "cql/pq-medication"
val cql = "$resourceDir/cql.txt".readFile()

val cqlElm = toJsonElm(cql).readStringToBase64Encoded()
val cqlLibrary =
parser.parseResource(
"$resourceDir/library.json".readFile().replace("#library-elm.json", cqlElm)
) as
Library
parser.parseResource(
"$resourceDir/library.json".readFile().replace("#library-elm.json", cqlElm)
) as
Library

println(cqlLibrary.convertToString(false) as String)

val fhirHelpersLibrary = "cql-common/helper.json".parseSampleResourceFromFile() as Library

val patient =
"patient-registration-questionnaire/sample/patient.json".parseSampleResourceFromFile() as
Patient
"patient-registration-questionnaire/sample/patient.json".parseSampleResourceFromFile() as
Patient
val dataBundle =
Bundle().apply {
// output of test results extraction is input of this cql
"test-results-questionnaire/sample"
.readDir()
.map { it.parseSampleResource() as Resource }
.forEach { addEntry().apply { resource = it } }

// output of test results cql is also added to input of this cql
"cql/test-results/sample".readDir().map { it.parseSampleResource() as Resource }.forEach {
addEntry().apply { resource = it }
}
}
Bundle().apply {
// output of test results extraction is input of this cql
"test-results-questionnaire/sample"
.readDir()
.map { it.parseSampleResource() as Resource }
.forEach { addEntry().apply { resource = it } }

// output of test results cql is also added to input of this cql
"cql/test-results/sample".readDir().map { it.parseSampleResource() as Resource }.forEach {
addEntry().apply { resource = it }
}
}

val fhirEngine = mockk<FhirEngine>()
val defaultRepository = spyk(DefaultRepository(fhirEngine, DefaultDispatcherProvider()))
val defaultRepository = mockk<DefaultRepository>()

coEvery { fhirEngine.get(ResourceType.Library, cqlLibrary.logicalId) } returns cqlLibrary
coEvery { fhirEngine.get(ResourceType.Library, fhirHelpersLibrary.logicalId) } returns
fhirHelpersLibrary
fhirHelpersLibrary
every { defaultRepository.fhirEngine } returns fhirEngine
coEvery { defaultRepository.save(any()) } just runs
coEvery { defaultRepository.search(any()) } returns listOf()

val result = runBlocking {
evaluator.runCqlLibrary(
cqlLibrary.logicalId,
patient,
dataBundle.apply {
this.entry.removeIf { it.resource.resourceType == ResourceType.Patient }
},
defaultRepository,
true
cqlLibrary.logicalId,
patient,
dataBundle.apply {
this.entry.removeIf { it.resource.resourceType == ResourceType.Patient }
},
defaultRepository,
true
)
}

assertOutput(
"$resourceDir/output_medication_request.json",
result,
ResourceType.MedicationRequest
"$resourceDir/output_medication_request.json",
result,
ResourceType.MedicationRequest
)

coVerify { defaultRepository.save(any()) }
Expand All @@ -119,58 +132,58 @@ class CqlContentTest : RobolectricTest() {

val cqlElm = toJsonElm(cql).readStringToBase64Encoded()
val cqlLibrary =
parser.parseResource(
"$resourceDir/library.json".readFile().replace("#library-elm.json", cqlElm)
) as
Library
parser.parseResource(
"$resourceDir/library.json".readFile().replace("#library-elm.json", cqlElm)
) as
Library

println(cqlLibrary.convertToString(false) as String)

val fhirHelpersLibrary = "cql-common/helper.json".parseSampleResourceFromFile() as Library

val patient =
"patient-registration-questionnaire/sample/patient.json".parseSampleResourceFromFile() as
Patient
"patient-registration-questionnaire/sample/patient.json".parseSampleResourceFromFile() as
Patient
val dataBundle =
Bundle().apply {
// output of test results extraction is input of this cql
"test-results-questionnaire/sample"
.readDir()
.map { it.parseSampleResource() as Resource }
.forEach { addEntry().apply { resource = it } }
}
Bundle().apply {
// output of test results extraction is input of this cql
"test-results-questionnaire/sample"
.readDir()
.map { it.parseSampleResource() as Resource }
.forEach { addEntry().apply { resource = it } }
}

val fhirEngine = mockk<FhirEngine>()
val defaultRepository = spyk(DefaultRepository(fhirEngine, DefaultDispatcherProvider()))
val defaultRepository = mockk<DefaultRepository>()

coEvery { fhirEngine.get(ResourceType.Library, cqlLibrary.logicalId) } returns cqlLibrary
coEvery { fhirEngine.get(ResourceType.Library, fhirHelpersLibrary.logicalId) } returns
fhirHelpersLibrary
fhirHelpersLibrary
coEvery { defaultRepository.save(any()) } just runs
coEvery { defaultRepository.search(any()) } returns listOf()

val result = runBlocking {
evaluator.runCqlLibrary(
cqlLibrary.logicalId,
patient,
dataBundle.apply {
this.entry.removeIf { it.resource.resourceType == ResourceType.Patient }
},
defaultRepository,
true
cqlLibrary.logicalId,
patient,
dataBundle.apply {
this.entry.removeIf { it.resource.resourceType == ResourceType.Patient }
},
defaultRepository,
true
)
}

assertOutput("$resourceDir/sample/output_condition.json", result, ResourceType.Condition)
assertOutput(
"$resourceDir/sample/output_service_request.json",
result,
ResourceType.ServiceRequest
"$resourceDir/sample/output_service_request.json",
result,
ResourceType.ServiceRequest
)
assertOutput(
"$resourceDir/sample/output_diagnostic_report.json",
result,
ResourceType.DiagnosticReport
"$resourceDir/sample/output_diagnostic_report.json",
result,
ResourceType.DiagnosticReport
)

coVerify(exactly = 3) { defaultRepository.save(any()) }
Expand All @@ -183,32 +196,34 @@ class CqlContentTest : RobolectricTest() {

val cqlElm = toJsonElm(cql).readStringToBase64Encoded()
val cqlLibrary =
parser.parseResource(
"$resourceDir/library.json".readFile().replace("#library-elm.json", cqlElm)
) as
Library
parser.parseResource(
"$resourceDir/library.json".readFile().replace("#library-elm.json", cqlElm)
) as
Library

println(cqlLibrary.convertToString(false) as String)

val fhirHelpersLibrary = "cql-common/helper.json".parseSampleResourceFromFile() as Library

val dataBundle =
Bundle().apply {
addEntry().apply {
// questionnaire-response of test results is input of this cql
resource =
"test-results-questionnaire/questionnaire-response.json".parseSampleResourceFromFile() as
Resource
}
}
Bundle().apply {
addEntry().apply {
// questionnaire-response of test results is input of this cql
resource =
"test-results-questionnaire/questionnaire-response.json".parseSampleResourceFromFile() as
Resource
}
}

val fhirEngine = mockk<FhirEngine>()
val defaultRepository = spyk(DefaultRepository(fhirEngine, DefaultDispatcherProvider()))
val defaultRepository = mockk<DefaultRepository>()

coEvery { fhirEngine.get(ResourceType.Library, cqlLibrary.logicalId) } returns cqlLibrary
coEvery { fhirEngine.get(ResourceType.Library, fhirHelpersLibrary.logicalId) } returns
fhirHelpersLibrary
fhirHelpersLibrary
every { defaultRepository.fhirEngine } returns fhirEngine
coEvery { defaultRepository.save(any()) } just runs
coEvery { defaultRepository.search(any<DataRequirement>()) } returns listOf()

val result = runBlocking {
evaluator.runCqlLibrary(cqlLibrary.logicalId, null, dataBundle, defaultRepository)
Expand All @@ -218,23 +233,23 @@ class CqlContentTest : RobolectricTest() {

Assert.assertTrue(result.contains("OUTPUT -> Correct Result"))
Assert.assertTrue(
result.contains(
"OUTPUT -> \nDetails:\n" +
"Value (3.0) is in Normal G6PD Range 0-3\n" +
"Value (11.0) is in Normal Haemoglobin Range 8-12"
)
result.contains(
"OUTPUT -> \nDetails:\n" +
"Value (3.0) is in Normal G6PD Range 0-3\n" +
"Value (11.0) is in Normal Haemoglobin Range 8-12"
)
)

val observationSlot = slot<Observation>()
coVerify { defaultRepository.save(capture(observationSlot)) }

Assert.assertEquals(
"QuestionnaireResponse/TEST_QUESTIONNAIRE_RESPONSE",
observationSlot.captured.focusFirstRep.reference
"QuestionnaireResponse/TEST_QUESTIONNAIRE_RESPONSE",
observationSlot.captured.focusFirstRep.reference
)
Assert.assertEquals(
"Correct Result",
observationSlot.captured.valueCodeableConcept.codingFirstRep.display
"Correct Result",
observationSlot.captured.valueCodeableConcept.codingFirstRep.display
)
Assert.assertEquals("Device Operation", observationSlot.captured.code.codingFirstRep.display)
}
Expand All @@ -244,7 +259,7 @@ class CqlContentTest : RobolectricTest() {
libraryManager.librarySourceLoader.registerProvider(FhirLibrarySourceProvider())

val translator: CqlTranslator =
CqlTranslator.fromText(cql, evaluator.modelManager, libraryManager)
CqlTranslator.fromText(cql, evaluator.modelManager, libraryManager)

return translator.toJxson().also { println(it.replace("\n", "").replace(" ", "")) }
}
Expand All @@ -254,12 +269,12 @@ class CqlContentTest : RobolectricTest() {

val expectedResource = resource.parseSampleResourceFromFile().convertToString(true)
val cqlResultStr =
cqlResult.find { it.startsWith("OUTPUT") && it.contains("\"resourceType\":\"$type\"") }!!
.replaceTimePart()
cqlResult.find { it.startsWith("OUTPUT") && it.contains("\"resourceType\":\"$type\"") }!!
.replaceTimePart()

println(cqlResultStr)
println(expectedResource as String)

Assert.assertTrue(cqlResultStr.contains("OUTPUT -> $expectedResource"))
}
}
}
Loading