Skip to content

Commit

Permalink
Resource consolidation after using the AllChangesSquashedBundlePost u…
Browse files Browse the repository at this point in the history
…pload strategy for resource creation. (google#2509)

* draft singleresourcepost

* Remove dead code.

* resource consolidation after  post http verb request

* Remove local changes.

* fix unit tests.

* unit tests

* Update kotlin api docs.

* revert local changes.

* Resource consolidation as per http verb

* address review comments.

* order of arguments

* code to string conversion

* AllChangesBundlePost upload strategy.

* remove localchange reference updates code.

* unit tests

* Address review comments.

* Fix unit test.

* rename tests.

* Code cleaning.

* code cleaning.

* Address review comments.

* Address review comments.

* Address review comments.

* spotless apply.

* build failure.

* cleanup.

* Address review comments.

* Address review comments.

* Address review comments.

---------

Co-authored-by: Santosh Pingle <[email protected]>
  • Loading branch information
santosh-pingle and Santosh Pingle authored Sep 9, 2024
1 parent d9653b6 commit 8301d62
Show file tree
Hide file tree
Showing 15 changed files with 579 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package com.google.android.fhir.db.impl
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.filters.MediumTest
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import ca.uhn.fhir.rest.gclient.StringClientParam
import ca.uhn.fhir.rest.param.ParamPrefixEnum
import com.google.android.fhir.DateProvider
Expand Down Expand Up @@ -4209,6 +4211,142 @@ class DatabaseImplTest {
assertThat(searchedObservations[0].logicalId).isEqualTo(locallyCreatedObservationResourceId)
}

@Test
fun updateResourcePostSync_shouldUpdateResourceId() = runBlocking {
val preSyncPatient = Patient().apply { id = "patient1" }
database.insert(preSyncPatient)
val postSyncResourceId = "patient2"
val newVersionId = "1"
val lastUpdatedRemote = Instant.now()

database.updateResourcePostSync(
preSyncPatient.logicalId,
postSyncResourceId,
preSyncPatient.resourceType,
newVersionId,
lastUpdatedRemote,
)

val patientResourceEntityPostSync =
database.selectEntity(preSyncPatient.resourceType, postSyncResourceId)
assertThat(patientResourceEntityPostSync.resourceId).isEqualTo(postSyncResourceId)
}

@Test
fun updateResourcePostSync_shouldUpdateResourceMeta() = runBlocking {
val preSyncPatient = Patient().apply { id = "patient1" }
database.insert(preSyncPatient)
val postSyncResourceId = "patient2"
val newVersionId = "1"
val lastUpdatedRemote = Instant.now()

database.updateResourcePostSync(
preSyncPatient.logicalId,
postSyncResourceId,
preSyncPatient.resourceType,
newVersionId,
lastUpdatedRemote,
)

val patientResourceEntityPostSync =
database.selectEntity(preSyncPatient.resourceType, postSyncResourceId)
assertThat(patientResourceEntityPostSync.versionId).isEqualTo(newVersionId)
assertThat(patientResourceEntityPostSync.lastUpdatedRemote?.toEpochMilli())
.isEqualTo(lastUpdatedRemote.toEpochMilli())
}

@Test
fun updateResourcePostSync_shouldDeleteOldResourceId() = runBlocking {
val preSyncPatient = Patient().apply { id = "patient1" }
database.insert(preSyncPatient)
val postSyncResourceId = "patient2"

database.updateResourcePostSync(
preSyncPatient.logicalId,
postSyncResourceId,
preSyncPatient.resourceType,
null,
null,
)

val exception =
assertThrows(ResourceNotFoundException::class.java) {
runBlocking { database.select(ResourceType.Patient, "patient1") }
}
assertThat(exception.message).isEqualTo("Resource not found with type Patient and id patient1!")
}

@Test
fun updateResourcePostSync_shouldUpdateReferringResourceReferenceValue() = runBlocking {
val preSyncPatient = Patient().apply { id = "patient1" }
val observation =
Observation().apply {
id = "observation1"
subject = Reference().apply { reference = "Patient/patient1" }
}
database.insert(preSyncPatient, observation)
val postSyncResourceId = "patient2"
val newVersionId = "1"
val lastUpdatedRemote = Instant.now()

database.updateResourcePostSync(
preSyncPatient.logicalId,
postSyncResourceId,
preSyncPatient.resourceType,
newVersionId,
lastUpdatedRemote,
)

assertThat(
(database.select(ResourceType.Observation, "observation1") as Observation)
.subject
.reference,
)
.isEqualTo("Patient/patient2")
}

@Test
fun updateResourcePostSync_shouldUpdateReferringResourceReferenceValueInLocalChange() =
runBlocking {
val preSyncPatient = Patient().apply { id = "patient1" }
val observation =
Observation().apply {
id = "observation1"
subject = Reference().apply { reference = "Patient/patient1" }
}
database.insert(preSyncPatient, observation)
val postSyncResourceId = "patient2"
val newVersionId = "1"
val lastUpdatedRemote = Instant.now()

database.updateResourcePostSync(
preSyncPatient.logicalId,
postSyncResourceId,
preSyncPatient.resourceType,
newVersionId,
lastUpdatedRemote,
)

assertThat(
(database.select(ResourceType.Observation, "observation1") as Observation)
.subject
.reference,
)
.isEqualTo("Patient/patient2")
val observationLocalChanges =
database.getLocalChanges(
observation.resourceType,
observation.logicalId,
)
val observationReferenceValue =
(FhirContext.forCached(FhirVersionEnum.R4)
.newJsonParser()
.parseResource(observationLocalChanges.first().payload) as Observation)
.subject
.reference
assertThat(observationReferenceValue).isEqualTo("Patient/$postSyncResourceId")
}

@Test // https://github.com/google/android-fhir/issues/2512
fun included_results_sort_ascending_should_have_distinct_resources() = runBlocking {
/**
Expand Down
Loading

0 comments on commit 8301d62

Please sign in to comment.