Skip to content

Commit

Permalink
Migrate Engine FHIR SDK Artifacts
Browse files Browse the repository at this point in the history
- Performance opt.
- Clean up Knowledge Manager Util
- Add unit tests
  • Loading branch information
ndegwamartin committed Nov 13, 2024
1 parent 16bb0cf commit 92601d7
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ constructor(
context = context,
configService = configService,
metadataResource = resource,
filePath =
subFilePath =
"${KnowledgeManagerUtil.KNOWLEDGE_MANAGER_ASSETS_SUBFOLDER}/${resource.resourceType}/${resource.idElement.idPart}.json",
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,26 @@ object KnowledgeManagerUtil {
const val KNOWLEDGE_MANAGER_ASSETS_SUBFOLDER = "km"
private val fhirContext = FhirContext.forR4Cached()

/**
* Util method that creates a physical file and writes the Metadata FHIR resource content to it.
* Note the filepath provided is appended to the apps private directory as returned by
* Context.filesDir
*
* @param subFilePath the path of the file but within the apps private directory
* {Context.filesDir}
* @param metadataResource the actual FHIR Resource of type MetadataResource
* @param configService the configuration service
* @param context the application context
* @return File the file object after creating and writing
*/
fun writeToFile(
filePath: String,
subFilePath: String,
metadataResource: MetadataResource,
configService: ConfigService,
context: Context,
): File =
context
.createFileInPrivateDirectory(filePath)
.createFileInPrivateDirectory(subFilePath)
.also { it.parentFile?.mkdirs() }
.apply {
writeText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

package org.smartregister.fhircore.engine.util

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

/**
* Launch a new coroutine for each map iteration using async. From
Expand All @@ -37,16 +34,10 @@ suspend fun <A, B> Iterable<A>.pmap(f: suspend (A) -> B): Iterable<B> = coroutin
}

/**
* Launch a new coroutine for each loop iteration using launch and the Default Dispatcher for
* computationaly intensive tasks.
* Launch a new coroutine for each loop iteration using async.
*
* @param T the type of elements in the iterable
*/
suspend fun <T> Iterable<T>.forEachAsync(action: suspend (T) -> Unit): Unit = coroutineScope {
forEach { launch(Dispatchers.Default) { action(it) } }
forEach { async { action(it) } }
}

suspend fun <T> Iterable<T>.forEachAsync(
dispatcher: CoroutineDispatcher,
action: suspend (T) -> Unit,
): Unit = coroutineScope { forEach { launch(dispatcher) { action(it) } } }
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ class ConfigurationRegistryTest : RobolectricTest() {
configService = configService,
metadataResource = resource,
context = context,
filePath =
subFilePath =
"${KnowledgeManagerUtil.KNOWLEDGE_MANAGER_ASSETS_SUBFOLDER}/${resource.resourceType}/${resource.idElement.idPart}.json",
)
assertNotNull(resultFile)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.fhircore.engine.util

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import ca.uhn.fhir.context.FhirContext
import java.io.File
import org.hl7.fhir.r4.model.StructureMap
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.smartregister.fhircore.engine.app.AppConfigService
import org.smartregister.fhircore.engine.robolectric.RobolectricTest

class KnowledgeManagerUtilTest : RobolectricTest() {

private lateinit var configService: AppConfigService
private val context = ApplicationProvider.getApplicationContext<Context>()!!

@Before
fun setUp() {
configService = AppConfigService(context)
}

@Test
fun testWriteToFile() {
val structureMap = StructureMap().apply { id = "structure-map-id" }

val filePath =
"${KnowledgeManagerUtil.KNOWLEDGE_MANAGER_ASSETS_SUBFOLDER}/StructureMap/structure-map-id.json"
val absoluteFilePath = "${context.filesDir}/$filePath"

val file = File(absoluteFilePath)
Assert.assertFalse(file.exists())

KnowledgeManagerUtil.writeToFile(filePath, structureMap, configService, context)

Assert.assertTrue(file.exists())

val savedStructureMap =
FhirContext.forR4Cached().newJsonParser().parseResource(file.readText()) as StructureMap
Assert.assertNotNull(savedStructureMap.url)
Assert.assertEquals(
"http://fake.base.url.com/StructureMap/structure-map-id",
savedStructureMap.url,
)
}

@After
fun tearDown() {
val testFile =
File(
"${context.filesDir}/${KnowledgeManagerUtil.KNOWLEDGE_MANAGER_ASSETS_SUBFOLDER}/StructureMap/structure-map-id.json",
)
if (testFile.exists()) {
testFile.delete()
}
}
}
2 changes: 1 addition & 1 deletion android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ espresso-core = "3.6.1"
fhir-sdk-contrib-barcode = "0.1.0-beta3-preview7-rc1-SNAPSHOT"
fhir-sdk-contrib-locationwidget = "0.1.0-alpha01-preview2-rc1-SNAPSHOT"
fhir-sdk-data-capture = "1.2.0-preview4-SNAPSHOT"
fhir-sdk-engine = "1.0.0-preview15-SNAPSHOT"
fhir-sdk-engine = "1.0.0-preview16-SNAPSHOT"
fhir-sdk-knowledge = "0.1.0-alpha03-preview5-rc1-SNAPSHOT"
fhir-sdk-workflow = "0.1.0-alpha04-preview10-rc1-SNAPSHOT"
fragment-ktx = "1.8.3"
Expand Down

0 comments on commit 92601d7

Please sign in to comment.