-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Body annotation for retrofit upload api * Logging exception when uploading * Added tests * Updated tests and removed some dependency * Update engine/src/test/java/com/google/android/fhir/sync/remote/RemoteFhirServiceTest.kt * Update engine/src/test/java/com/google/android/fhir/sync/remote/RemoteFhirServiceTest.kt --------- Co-authored-by: PallaviGanorkar <[email protected]> Co-authored-by: Jing Tang <[email protected]>
- Loading branch information
1 parent
848754f
commit f87fa04
Showing
5 changed files
with
147 additions
and
5 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
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
141 changes: 141 additions & 0 deletions
141
engine/src/test/java/com/google/android/fhir/sync/remote/RemoteFhirServiceTest.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,141 @@ | ||
/* | ||
* Copyright 2022 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:https://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.remote | ||
|
||
import ca.uhn.fhir.context.FhirContext | ||
import com.google.android.fhir.NetworkConfiguration | ||
import com.google.android.fhir.logicalId | ||
import com.google.common.truth.Truth.assertThat | ||
import java.net.HttpURLConnection | ||
import kotlinx.coroutines.test.runTest | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import org.hl7.fhir.r4.model.Bundle | ||
import org.hl7.fhir.r4.model.HumanName | ||
import org.hl7.fhir.r4.model.Patient | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class RemoteFhirServiceTest { | ||
|
||
private val mockWebServer = MockWebServer() | ||
|
||
private val apiService by lazy { | ||
RemoteFhirService.Builder(mockWebServer.url("/").toString(), NetworkConfiguration()).build() | ||
} | ||
|
||
private val parser = FhirContext.forR4Cached().newJsonParser() | ||
|
||
@Before | ||
fun setUp() { | ||
mockWebServer.start() | ||
} | ||
|
||
@After | ||
fun shutdown() { | ||
mockWebServer.shutdown() | ||
} | ||
|
||
@Test // https://github.com/google/android-fhir/issues/1892 | ||
fun `should assemble download request correctly`() = runTest { | ||
// checks that a download request can be made successfully with parameters without exception | ||
val mockResponse = | ||
MockResponse().apply { | ||
setResponseCode(HttpURLConnection.HTTP_OK) | ||
setBody( | ||
parser.encodeResourceToString( | ||
Patient().apply { | ||
id = "patient-001" | ||
addName( | ||
HumanName().apply { | ||
addGiven("John") | ||
family = "Doe" | ||
} | ||
) | ||
} | ||
) | ||
) | ||
} | ||
mockWebServer.enqueue(mockResponse) | ||
|
||
val result = apiService.download("Patient/patient-001") | ||
|
||
// No exception should occur | ||
assertThat(result).isInstanceOf(Patient::class.java) | ||
} | ||
|
||
@Test // https://github.com/google/android-fhir/issues/1892 | ||
fun `should assemble upload request correctly`() = runTest { | ||
// checks that a upload request can be made successfully with parameters without exception | ||
val mockResponse = | ||
MockResponse().apply { | ||
setResponseCode(HttpURLConnection.HTTP_OK) | ||
setBody( | ||
parser.encodeResourceToString( | ||
Bundle().apply { | ||
id = "transaction-response-1" | ||
type = Bundle.BundleType.TRANSACTIONRESPONSE | ||
} | ||
) | ||
) | ||
} | ||
mockWebServer.enqueue(mockResponse) | ||
val request = | ||
Bundle().apply { | ||
id = "transaction-1" | ||
type = Bundle.BundleType.TRANSACTION | ||
} | ||
|
||
val result = apiService.upload(request) | ||
|
||
// No exception has occurred | ||
assertThat(result).isInstanceOf(Bundle::class.java) | ||
} | ||
|
||
@Test | ||
fun `should use fhir converter to serialize and deserialize request and response for fhir resources`() = | ||
runTest { | ||
val mockResponse = | ||
MockResponse().apply { | ||
setResponseCode(HttpURLConnection.HTTP_OK) | ||
setBody( | ||
parser.encodeResourceToString( | ||
Bundle().apply { | ||
id = "transaction-response-1" | ||
type = Bundle.BundleType.TRANSACTIONRESPONSE | ||
} | ||
) | ||
) | ||
} | ||
mockWebServer.enqueue(mockResponse) | ||
val request = | ||
Bundle().apply { | ||
id = "transaction-1" | ||
type = Bundle.BundleType.TRANSACTION | ||
} | ||
|
||
val result = apiService.upload(request) | ||
|
||
assertThat(result).isInstanceOf(Bundle::class.java) | ||
assertThat((result as Bundle).type).isEqualTo(Bundle.BundleType.TRANSACTIONRESPONSE) | ||
assertThat((result).logicalId).isEqualTo("transaction-response-1") | ||
} | ||
} |