Skip to content

Commit 8301d62

Browse files
santosh-pingleSantosh Pingle
and
Santosh Pingle
authored
Resource consolidation after using the AllChangesSquashedBundlePost upload 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]>
1 parent d9653b6 commit 8301d62

File tree

15 files changed

+579
-210
lines changed

15 files changed

+579
-210
lines changed

engine/src/androidTest/java/com/google/android/fhir/db/impl/DatabaseImplTest.kt

+138
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package com.google.android.fhir.db.impl
1919
import android.content.Context
2020
import androidx.test.core.app.ApplicationProvider
2121
import androidx.test.filters.MediumTest
22+
import ca.uhn.fhir.context.FhirContext
23+
import ca.uhn.fhir.context.FhirVersionEnum
2224
import ca.uhn.fhir.rest.gclient.StringClientParam
2325
import ca.uhn.fhir.rest.param.ParamPrefixEnum
2426
import com.google.android.fhir.DateProvider
@@ -4209,6 +4211,142 @@ class DatabaseImplTest {
42094211
assertThat(searchedObservations[0].logicalId).isEqualTo(locallyCreatedObservationResourceId)
42104212
}
42114213

4214+
@Test
4215+
fun updateResourcePostSync_shouldUpdateResourceId() = runBlocking {
4216+
val preSyncPatient = Patient().apply { id = "patient1" }
4217+
database.insert(preSyncPatient)
4218+
val postSyncResourceId = "patient2"
4219+
val newVersionId = "1"
4220+
val lastUpdatedRemote = Instant.now()
4221+
4222+
database.updateResourcePostSync(
4223+
preSyncPatient.logicalId,
4224+
postSyncResourceId,
4225+
preSyncPatient.resourceType,
4226+
newVersionId,
4227+
lastUpdatedRemote,
4228+
)
4229+
4230+
val patientResourceEntityPostSync =
4231+
database.selectEntity(preSyncPatient.resourceType, postSyncResourceId)
4232+
assertThat(patientResourceEntityPostSync.resourceId).isEqualTo(postSyncResourceId)
4233+
}
4234+
4235+
@Test
4236+
fun updateResourcePostSync_shouldUpdateResourceMeta() = runBlocking {
4237+
val preSyncPatient = Patient().apply { id = "patient1" }
4238+
database.insert(preSyncPatient)
4239+
val postSyncResourceId = "patient2"
4240+
val newVersionId = "1"
4241+
val lastUpdatedRemote = Instant.now()
4242+
4243+
database.updateResourcePostSync(
4244+
preSyncPatient.logicalId,
4245+
postSyncResourceId,
4246+
preSyncPatient.resourceType,
4247+
newVersionId,
4248+
lastUpdatedRemote,
4249+
)
4250+
4251+
val patientResourceEntityPostSync =
4252+
database.selectEntity(preSyncPatient.resourceType, postSyncResourceId)
4253+
assertThat(patientResourceEntityPostSync.versionId).isEqualTo(newVersionId)
4254+
assertThat(patientResourceEntityPostSync.lastUpdatedRemote?.toEpochMilli())
4255+
.isEqualTo(lastUpdatedRemote.toEpochMilli())
4256+
}
4257+
4258+
@Test
4259+
fun updateResourcePostSync_shouldDeleteOldResourceId() = runBlocking {
4260+
val preSyncPatient = Patient().apply { id = "patient1" }
4261+
database.insert(preSyncPatient)
4262+
val postSyncResourceId = "patient2"
4263+
4264+
database.updateResourcePostSync(
4265+
preSyncPatient.logicalId,
4266+
postSyncResourceId,
4267+
preSyncPatient.resourceType,
4268+
null,
4269+
null,
4270+
)
4271+
4272+
val exception =
4273+
assertThrows(ResourceNotFoundException::class.java) {
4274+
runBlocking { database.select(ResourceType.Patient, "patient1") }
4275+
}
4276+
assertThat(exception.message).isEqualTo("Resource not found with type Patient and id patient1!")
4277+
}
4278+
4279+
@Test
4280+
fun updateResourcePostSync_shouldUpdateReferringResourceReferenceValue() = runBlocking {
4281+
val preSyncPatient = Patient().apply { id = "patient1" }
4282+
val observation =
4283+
Observation().apply {
4284+
id = "observation1"
4285+
subject = Reference().apply { reference = "Patient/patient1" }
4286+
}
4287+
database.insert(preSyncPatient, observation)
4288+
val postSyncResourceId = "patient2"
4289+
val newVersionId = "1"
4290+
val lastUpdatedRemote = Instant.now()
4291+
4292+
database.updateResourcePostSync(
4293+
preSyncPatient.logicalId,
4294+
postSyncResourceId,
4295+
preSyncPatient.resourceType,
4296+
newVersionId,
4297+
lastUpdatedRemote,
4298+
)
4299+
4300+
assertThat(
4301+
(database.select(ResourceType.Observation, "observation1") as Observation)
4302+
.subject
4303+
.reference,
4304+
)
4305+
.isEqualTo("Patient/patient2")
4306+
}
4307+
4308+
@Test
4309+
fun updateResourcePostSync_shouldUpdateReferringResourceReferenceValueInLocalChange() =
4310+
runBlocking {
4311+
val preSyncPatient = Patient().apply { id = "patient1" }
4312+
val observation =
4313+
Observation().apply {
4314+
id = "observation1"
4315+
subject = Reference().apply { reference = "Patient/patient1" }
4316+
}
4317+
database.insert(preSyncPatient, observation)
4318+
val postSyncResourceId = "patient2"
4319+
val newVersionId = "1"
4320+
val lastUpdatedRemote = Instant.now()
4321+
4322+
database.updateResourcePostSync(
4323+
preSyncPatient.logicalId,
4324+
postSyncResourceId,
4325+
preSyncPatient.resourceType,
4326+
newVersionId,
4327+
lastUpdatedRemote,
4328+
)
4329+
4330+
assertThat(
4331+
(database.select(ResourceType.Observation, "observation1") as Observation)
4332+
.subject
4333+
.reference,
4334+
)
4335+
.isEqualTo("Patient/patient2")
4336+
val observationLocalChanges =
4337+
database.getLocalChanges(
4338+
observation.resourceType,
4339+
observation.logicalId,
4340+
)
4341+
val observationReferenceValue =
4342+
(FhirContext.forCached(FhirVersionEnum.R4)
4343+
.newJsonParser()
4344+
.parseResource(observationLocalChanges.first().payload) as Observation)
4345+
.subject
4346+
.reference
4347+
assertThat(observationReferenceValue).isEqualTo("Patient/$postSyncResourceId")
4348+
}
4349+
42124350
@Test // https://github.com/google/android-fhir/issues/2512
42134351
fun included_results_sort_ascending_should_have_distinct_resources() = runBlocking {
42144352
/**

0 commit comments

Comments
 (0)