@@ -19,6 +19,8 @@ package com.google.android.fhir.db.impl
19
19
import android.content.Context
20
20
import androidx.test.core.app.ApplicationProvider
21
21
import androidx.test.filters.MediumTest
22
+ import ca.uhn.fhir.context.FhirContext
23
+ import ca.uhn.fhir.context.FhirVersionEnum
22
24
import ca.uhn.fhir.rest.gclient.StringClientParam
23
25
import ca.uhn.fhir.rest.param.ParamPrefixEnum
24
26
import com.google.android.fhir.DateProvider
@@ -4209,6 +4211,142 @@ class DatabaseImplTest {
4209
4211
assertThat(searchedObservations[0 ].logicalId).isEqualTo(locallyCreatedObservationResourceId)
4210
4212
}
4211
4213
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
+
4212
4350
@Test // https://github.com/google/android-fhir/issues/2512
4213
4351
fun included_results_sort_ascending_should_have_distinct_resources () = runBlocking {
4214
4352
/* *
0 commit comments