-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resource Creation using POST http verb (SingleResourcePost) (#2464)
* 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 * Address review comments. * Fix version id and use existing api. * Address review comments. * Add unit tests. * Address review comments. --------- Co-authored-by: Santosh Pingle <spingle@google.com>
- Loading branch information
1 parent
8e2aaf0
commit d83692e
Showing
13 changed files
with
456 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 231 additions & 0 deletions
231
.../androidTest/java/com/google/android/fhir/sync/upload/HttpPostResourceConsolidatorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
/* | ||
* Copyright 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. | ||
* 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 com.google.android.fhir.sync.upload | ||
|
||
import android.content.Context | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import ca.uhn.fhir.context.FhirContext | ||
import com.google.android.fhir.FhirServices | ||
import com.google.android.fhir.db.Database | ||
import com.google.android.fhir.db.ResourceNotFoundException | ||
import com.google.android.fhir.logicalId | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.runBlocking | ||
import org.hl7.fhir.r4.model.DomainResource | ||
import org.hl7.fhir.r4.model.InstantType | ||
import org.hl7.fhir.r4.model.Observation | ||
import org.hl7.fhir.r4.model.ResourceType | ||
import org.junit.After | ||
import org.junit.Assert.assertThrows | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class HttpPostResourceConsolidatorTest { | ||
@JvmField @Parameterized.Parameter(0) var encrypted: Boolean = false | ||
|
||
private val context: Context = ApplicationProvider.getApplicationContext() | ||
private lateinit var database: Database | ||
private lateinit var resourceConsolidator: ResourceConsolidator | ||
|
||
@Before | ||
fun setupDatabase() = runBlocking { | ||
database = | ||
FhirServices.builder(context) | ||
.inMemory() | ||
.apply { | ||
if (encrypted) enableEncryptionIfSupported() | ||
setSearchParameters(null) | ||
} | ||
.build() | ||
.database | ||
resourceConsolidator = HttpPostResourceConsolidator(database) | ||
} | ||
|
||
@After | ||
fun closeDatabase() { | ||
database.close() | ||
} | ||
|
||
@Test | ||
fun consolidate_shouldUpdateResourceId() = runBlocking { | ||
val patientJsonString = | ||
""" | ||
{ | ||
"resourceType": "Patient", | ||
"id": "patient1" | ||
} | ||
""" | ||
.trimIndent() | ||
val patient = | ||
FhirContext.forR4Cached().newJsonParser().parseResource(patientJsonString) as DomainResource | ||
database.insert(patient) | ||
val localChanges = database.getLocalChanges(patient.resourceType, patient.logicalId) | ||
|
||
val postSyncPatientJsonString = | ||
""" | ||
{ | ||
"resourceType": "Patient", | ||
"id": "patient2", | ||
"meta": { | ||
"versionId": "1" | ||
} | ||
} | ||
""" | ||
.trimIndent() | ||
val postSyncPatient = | ||
FhirContext.forR4Cached().newJsonParser().parseResource(postSyncPatientJsonString) | ||
as DomainResource | ||
postSyncPatient.meta.lastUpdatedElement = InstantType.now() | ||
val uploadRequestResult = | ||
UploadRequestResult.Success( | ||
listOf(ResourceUploadResponseMapping(localChanges, postSyncPatient)), | ||
) | ||
resourceConsolidator.consolidate(uploadRequestResult) | ||
|
||
assertThat(database.select(ResourceType.Patient, "patient2").logicalId) | ||
.isEqualTo(postSyncPatient.logicalId) | ||
|
||
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 consolidate_dependentResources_shouldUpdateReferenceValue() = runBlocking { | ||
val patientJsonString = | ||
""" | ||
{ | ||
"resourceType": "Patient", | ||
"id": "patient1" | ||
} | ||
""" | ||
.trimIndent() | ||
val patient = | ||
FhirContext.forR4Cached().newJsonParser().parseResource(patientJsonString) as DomainResource | ||
val observationJsonString = | ||
""" | ||
{ | ||
"resourceType": "Observation", | ||
"id": "observation1", | ||
"subject": { | ||
"reference": "Patient/patient1" | ||
} | ||
} | ||
""" | ||
.trimIndent() | ||
val observation = | ||
FhirContext.forR4Cached().newJsonParser().parseResource(observationJsonString) | ||
as DomainResource | ||
database.insert(patient, observation) | ||
val postSyncPatientJsonString = | ||
""" | ||
{ | ||
"resourceType": "Patient", | ||
"id": "patient2", | ||
"meta": { | ||
"versionId": "1" | ||
} | ||
} | ||
""" | ||
.trimIndent() | ||
val postSyncPatient = | ||
FhirContext.forR4Cached().newJsonParser().parseResource(postSyncPatientJsonString) | ||
as DomainResource | ||
postSyncPatient.meta.lastUpdatedElement = InstantType.now() | ||
val localChanges = database.getLocalChanges(patient.resourceType, patient.logicalId) | ||
val uploadRequestResult = | ||
UploadRequestResult.Success( | ||
listOf(ResourceUploadResponseMapping(localChanges, postSyncPatient)), | ||
) | ||
|
||
resourceConsolidator.consolidate(uploadRequestResult) | ||
|
||
assertThat( | ||
(database.select(ResourceType.Observation, "observation1") as Observation) | ||
.subject | ||
.reference, | ||
) | ||
.isEqualTo("Patient/patient2") | ||
} | ||
|
||
@Test | ||
fun consolidate_localChanges_shouldUpdateReferenceValue() = runBlocking { | ||
val patientJsonString = | ||
""" | ||
{ | ||
"resourceType": "Patient", | ||
"id": "patient1" | ||
} | ||
""" | ||
.trimIndent() | ||
val patient = | ||
FhirContext.forR4Cached().newJsonParser().parseResource(patientJsonString) as DomainResource | ||
val observationJsonString = | ||
""" | ||
{ | ||
"resourceType": "Observation", | ||
"id": "observation1", | ||
"subject": { | ||
"reference": "Patient/patient1" | ||
} | ||
} | ||
""" | ||
.trimIndent() | ||
val observation = | ||
FhirContext.forR4Cached().newJsonParser().parseResource(observationJsonString) | ||
as DomainResource | ||
database.insert(patient, observation) | ||
val postSyncPatientJsonString = | ||
""" | ||
{ | ||
"resourceType": "Patient", | ||
"id": "patient2", | ||
"meta": { | ||
"versionId": "1" | ||
} | ||
} | ||
""" | ||
.trimIndent() | ||
val postSyncPatient = | ||
FhirContext.forR4Cached().newJsonParser().parseResource(postSyncPatientJsonString) | ||
as DomainResource | ||
postSyncPatient.meta.lastUpdatedElement = InstantType.now() | ||
val localChanges = database.getLocalChanges(patient.resourceType, patient.logicalId) | ||
val uploadRequestResult = | ||
UploadRequestResult.Success( | ||
listOf(ResourceUploadResponseMapping(localChanges, postSyncPatient)), | ||
) | ||
|
||
resourceConsolidator.consolidate(uploadRequestResult) | ||
|
||
val localChange = database.getLocalChanges(ResourceType.Observation, "observation1").last() | ||
assertThat( | ||
(FhirContext.forR4Cached().newJsonParser().parseResource(localChange.payload) | ||
as Observation) | ||
.subject | ||
.reference, | ||
) | ||
.isEqualTo("Patient/patient2") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.