Skip to content

Commit

Permalink
KM: Use resourcetype when searching by url (#2518)
Browse files Browse the repository at this point in the history
* Use resourcetype when searching by url

* Add tests for km loading
  • Loading branch information
MJ1998 committed May 9, 2024
1 parent 63151b5 commit 67c9529
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ internal constructor(
val resourceEntities =
when {
url != null && version != null ->
listOfNotNull(knowledgeDao.getResourceWithUrlAndVersion(url, version))
url != null -> listOfNotNull(knowledgeDao.getResourceWithUrl(url))
listOfNotNull(knowledgeDao.getResourceWithUrlAndVersion(resType, url, version))
url != null -> listOfNotNull(knowledgeDao.getResourceWithUrl(resType, url))
id != null -> listOfNotNull(knowledgeDao.getResourcesWithId(id.toLong()))
name != null && version != null ->
listOfNotNull(knowledgeDao.getResourcesWithNameAndVersion(resType, name, version))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ abstract class KnowledgeDao {
): Long {
val resourceMetadata =
if (resource.url != null && resource.version != null) {
getResourceWithUrlAndVersion(resource.url, resource.version)
getResourceWithUrlAndVersion(resource.resourceType, resource.url, resource.version)
} else if (resource.url != null) {
getResourceWithUrl(resource.url)
getResourceWithUrl(resource.resourceType, resource.url)
} else {
getResourcesWithNameAndVersion(resource.resourceType, resource.name, resource.version)
}
Expand Down Expand Up @@ -84,14 +84,18 @@ abstract class KnowledgeDao {
resourceType: ResourceType,
): List<ResourceMetadataEntity>

@Query("SELECT * from ResourceMetadataEntity WHERE url = :url AND version = :version")
@Query(
"SELECT * from ResourceMetadataEntity WHERE resourceType =:resourceType AND url = :url AND version = :version",
)
internal abstract suspend fun getResourceWithUrlAndVersion(
resourceType: ResourceType,
url: String,
version: String,
): ResourceMetadataEntity?

@Query("SELECT * from ResourceMetadataEntity WHERE url = :url")
@Query("SELECT * from ResourceMetadataEntity WHERE resourceType = :resourceType AND url = :url")
internal abstract suspend fun getResourceWithUrl(
resourceType: ResourceType,
url: String,
): ResourceMetadataEntity?

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2023-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,8 @@ import com.google.common.truth.Truth.assertThat
import java.io.File
import kotlinx.coroutines.test.runTest
import org.hl7.fhir.r4.model.Library
import org.hl7.fhir.r4.model.MetadataResource
import org.hl7.fhir.r4.model.PlanDefinition
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -164,10 +166,74 @@ internal class KnowledgeManagerTest {
.isNotNull()
}

private fun writeToFile(library: Library): File {
return File(context.filesDir, library.id).apply {
@Test
fun `for different resources with URL loading by URL should be correct`() = runTest {
val commonUrl = "www.sample-url.com"
val libraryWithSameUrl =
Library().apply {
id = "Library/lId"
name = "LibraryName"
url = commonUrl
}
val planDefinitionWithSameUrl =
PlanDefinition().apply {
id = "PlanDefinition/pdId"
name = "PlanDefinitionName"
url = commonUrl
}

knowledgeManager.install(writeToFile(libraryWithSameUrl))
knowledgeManager.install(writeToFile(planDefinitionWithSameUrl))

val resources = knowledgeDb.knowledgeDao().getResources()
assertThat(resources).hasSize(2)

val libraryLoadedByUrl =
knowledgeManager.loadResources(resourceType = "Library", url = commonUrl).single()
assertThat(libraryLoadedByUrl.idElement.toString()).isEqualTo("Library/1")

val planDefinitionLoadedByUrl =
knowledgeManager.loadResources(resourceType = "PlanDefinition", url = commonUrl).single()
assertThat(planDefinitionLoadedByUrl.idElement.toString()).isEqualTo("PlanDefinition/2")
}

@Test
fun `for different resources with URL and Version loading by URL should be correct`() = runTest {
val commonUrl = "www.sample-url.com"
val libraryWithSameUrl =
Library().apply {
id = "Library/lId"
name = "LibraryName"
url = commonUrl
version = "0"
}
val planDefinitionWithSameUrl =
PlanDefinition().apply {
id = "PlanDefinition/pdId"
name = "PlanDefinitionName"
url = commonUrl
version = "0"
}

knowledgeManager.install(writeToFile(libraryWithSameUrl))
knowledgeManager.install(writeToFile(planDefinitionWithSameUrl))

val resources = knowledgeDb.knowledgeDao().getResources()
assertThat(resources).hasSize(2)

val libraryLoadedByUrl =
knowledgeManager.loadResources(resourceType = "Library", url = commonUrl).single()
assertThat(libraryLoadedByUrl.idElement.toString()).isEqualTo("Library/1")

val planDefinitionLoadedByUrl =
knowledgeManager.loadResources(resourceType = "PlanDefinition", url = commonUrl).single()
assertThat(planDefinitionLoadedByUrl.idElement.toString()).isEqualTo("PlanDefinition/2")
}

private fun writeToFile(metadataResource: MetadataResource): File {
return File(context.filesDir, metadataResource.id).apply {
this.parentFile?.mkdirs()
writeText(jsonParser.encodeResourceToString(library))
writeText(jsonParser.encodeResourceToString(metadataResource))
}
}
}

0 comments on commit 67c9529

Please sign in to comment.