Skip to content

Commit

Permalink
Code Refactor/Clean up + Unit Tests ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
ndegwamartin committed Jul 12, 2023
1 parent 42115a0 commit ed9f4e4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ constructor(
// For appId that ends with suffix /debug e.g. app/debug, we load configurations from assets
// extract appId by removing the suffix e.g. app from above example
val loadFromAssets = appId.endsWith(DEBUG_SUFFIX, ignoreCase = true)
val parsedAppId = appId.substringBefore("/").trim()
val parsedAppId = appId.substringBefore(TYPE_REFERENCE_DELIMITER).trim()
if (loadFromAssets) {
try {
context.assets
Expand All @@ -236,7 +236,7 @@ constructor(
it.focus.hasIdentifier() && isIconConfig(it.focus.identifier.value)
}
if (iconConfigs.isNotEmpty()) {
val ids = iconConfigs.joinToString(",") { it.focus.extractId() }
val ids = iconConfigs.joinToString(DEFAULT_STRING_SEPARATOR) { it.focus.extractId() }
fhirResourceDataSource
.getResource("${ResourceType.Binary.name}?$ID=$ids")
.entry
Expand Down Expand Up @@ -292,7 +292,7 @@ constructor(
composition.retrieveCompositionSections().forEach {
if (it.hasFocus() && it.focus.hasReferenceElement() && it.focus.hasIdentifier()) {
val configIdentifier = it.focus.identifier.value
val referenceResourceType = it.focus.reference.substringBefore("/")
val referenceResourceType = it.focus.reference.substringBefore(TYPE_REFERENCE_DELIMITER)
if (isAppConfig(referenceResourceType) && !isIconConfig(configIdentifier)) {
val configBinary = fhirEngine.get<Binary>(it.focus.extractId())
configsJsonMap[configIdentifier] = configBinary.content.decodeToString()
Expand Down Expand Up @@ -368,22 +368,14 @@ constructor(
}
.forEach { resourceGroup ->
if (resourceGroup.key == ResourceType.List.name) {
if (!isNonProxy()) {
resourceGroup.value.forEach {
processCompositionManifestResources(
FHIR_GATEWAY_MODE_HEADER_VALUE,
"${resourceGroup.key}/${it.focus.extractId()}",
)
}
} else {
// TODO Duplication for Backward Compatibility for NON-PROXY version
// Refactor to strip out this after all projects migrate to the proxy implementation

if (isNonProxy()) { // Backward compatibility for NON-PROXY version
val chunkedResourceIdList =
resourceGroup.value.chunked(MANIFEST_PROCESSOR_BATCH_SIZE)
chunkedResourceIdList.forEach {
val resourceIds =
it.joinToString(",") { sectionComponent -> sectionComponent.focus.extractId() }
it.joinToString(DEFAULT_STRING_SEPARATOR) { sectionComponent ->
sectionComponent.focus.extractId()
}
fhirResourceDataSource
.getResource("${resourceGroup.key}?$ID=$resourceIds")
.entry
Expand All @@ -393,7 +385,10 @@ constructor(
addOrUpdate(bundleEntryComponent.resource)
val list = bundleEntryComponent.resource as ListResource
list.entry.forEach { listEntryComponent ->
val resourceKey = listEntryComponent.item.reference.substringBefore("/")
val resourceKey =
listEntryComponent.item.reference.substringBefore(
TYPE_REFERENCE_DELIMITER
)
val resourceId =
listEntryComponent.item.reference.extractLogicalIdUuid()

Expand All @@ -408,6 +403,13 @@ constructor(
}
}
}
} else {
resourceGroup.value.forEach {
processCompositionManifestResources(
FHIR_GATEWAY_MODE_HEADER_VALUE,
"${resourceGroup.key}/${it.focus.extractId()}",
)
}
}
} else {
val chunkedResourceIdList = resourceGroup.value.chunked(MANIFEST_PROCESSOR_BATCH_SIZE)
Expand Down Expand Up @@ -514,11 +516,11 @@ constructor(
const val COUNT = "count"
const val DEBUG_SUFFIX = "/debug"
const val DEFAULT_STRING_SEPARATOR = ","
const val FHIR_GATEWAY_MODE_HEADER_VALUE = "list-entries"
const val ICON_PREFIX = "ic_"
const val ID = "_id"
const val MANIFEST_PROCESSOR_BATCH_SIZE = 30
const val ORGANIZATION = "organization"
const val TYPE_REFERENCE_DELIMITER = "/"
const val FHIR_GATEWAY_MODE_HEADER_VALUE = "list-entries"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ class TokenAuthenticatorTest : RobolectricTest() {
@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun testFetchTokenShouldSetPasswordAndAuthTokenForExistingAccount() = runTest {
val account = Account(sampleUsername, PROVIDER)
val testAPKApplicationId = "org.smartregister.fhircore.engine.test"
val account = Account(sampleUsername, testAPKApplicationId)
val password = charArrayOf('P', '4', '5', '5', 'W', '4', '0')
val token = "goodToken"
val refreshToken = "refreshToken"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@ class ConfigurationRegistryTest : RobolectricTest() {
coVerify { fhirEngine.get(patient.resourceType, patient.logicalId) }
}

// TODO TO DO Remove after migration to PROXY for all projects: Duplication for Backward
// Compatibility for NON-PROXY version
// Backward compatibility for NON-PROXY version
@Test
@kotlinx.coroutines.ExperimentalCoroutinesApi
fun testFetchNonWorkflowConfigResourcesBundleListResourceProxyBackwardCompatible() {
Expand Down Expand Up @@ -319,51 +318,6 @@ class ConfigurationRegistryTest : RobolectricTest() {
coEvery { fhirResourceDataSource.getResource("$focusReference?_id=$focusReference") }
}

@Test
@kotlinx.coroutines.ExperimentalCoroutinesApi
fun testFetchNonWorkflowConfigResourcesBundleListResource() {
val appId = "theAppId"
val focusReference = ResourceType.Questionnaire.name
val resourceKey = "resourceKey"
val resourceId = "resourceId"
val listResource =
ListResource().apply {
entry =
listOf(
ListResource.ListEntryComponent().apply {
item = Reference().apply { reference = "$resourceKey/$resourceId" }
},
)
}
val bundle =
Bundle().apply {
entry = listOf(Bundle.BundleEntryComponent().apply { resource = listResource })
}

val composition =
Composition().apply {
identifier = Identifier().apply { value = appId }
section = listOf(SectionComponent().apply { focus.reference = focusReference })
}
configRegistry.sharedPreferencesHelper.write(SharedPreferenceKey.APP_ID.name, appId)
coEvery { fhirEngine.create(composition) } returns listOf(composition.id)
coEvery { fhirEngine.search<Composition>(Search(composition.resourceType)) } returns
listOf(composition)
coEvery { fhirResourceDataSource.getResource("$focusReference?_id=$focusReference") } returns
bundle
coEvery { fhirEngine.update(any()) } returns Unit
coEvery { fhirEngine.get(ResourceType.List, "") } returns listResource
coEvery { fhirResourceDataSource.getResource("$resourceKey?_id=$resourceId") } returns bundle

runTest {
configRegistry.fhirEngine.create(composition)
configRegistry.fetchNonWorkflowConfigResources()
}

coVerify { fhirEngine.get(ResourceType.List, "") }
coVerify { fhirResourceDataSource.getResource("$resourceKey?_id=$resourceId") }
}

@Test
@kotlinx.coroutines.ExperimentalCoroutinesApi
fun testAddOrUpdate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class ConfigurationRegistryTest : RobolectricTest() {
}

@Test
fun testFetchListResource() = runBlocking {
fun testFetchListResourceNonProxy() = runBlocking {
val composition =
Composition().apply {
addSection().apply {
Expand All @@ -137,6 +137,7 @@ class ConfigurationRegistryTest : RobolectricTest() {
}
}

configurationRegistry.setNonProxy(true)
every { secureSharedPreference.retrieveSessionUsername() } returns "demo"
coEvery { fhirEngine.search<Composition>(any<Search>()) } returns listOf(composition)
coEvery { fhirEngine.get(any(), any()) } throws ResourceNotFoundException("Exce", "Exce")
Expand All @@ -148,4 +149,42 @@ class ConfigurationRegistryTest : RobolectricTest() {
coVerify { configurationRegistry.fhirResourceDataSource.getResource(any()) }
coVerify { configurationRegistry.create(any()) }
}

@Test
fun testFetchListResource() = runBlocking {
val composition =
Composition().apply {
addSection().apply {
this.focus =
Reference().apply {
reference = "List/123456"
identifier = Identifier().apply { value = "012345" }
}
}
}

val bundle =
org.hl7.fhir.r4.model.Bundle().apply {
addEntry().apply {
this.resource = ListResource().apply { ListResource@ this.id = "123456" }
}
}

every { secureSharedPreference.retrieveSessionUsername() } returns "demo"
coEvery { fhirEngine.search<Composition>(any<Search>()) } returns listOf(composition)
coEvery { fhirEngine.get(any(), any()) } throws ResourceNotFoundException("Exce", "Exce")
coEvery { configurationRegistry.fhirResourceDataSource.getResource(any()) } returns bundle
coEvery {
fhirResourceService.getResourceWithGatewayModeHeader(
ConfigurationRegistry.FHIR_GATEWAY_MODE_HEADER_VALUE,
"List/123456"
)
} returns bundle
every { sharedPreferencesHelper.read(SharedPreferenceKey.APP_ID.name, null) } returns "demo"

configurationRegistry.fetchNonWorkflowConfigResources()

coVerify { fhirResourceService.getResourceWithGatewayModeHeader(any(), any()) }
coVerify { configurationRegistry.create(any()) }
}
}

0 comments on commit ed9f4e4

Please sign in to comment.