From b886649c3d9ba736b869a94844277f969a98324d Mon Sep 17 00:00:00 2001 From: Owais <62104757+owais-vd@users.noreply.github.com> Date: Wed, 11 Oct 2023 02:07:01 +0500 Subject: [PATCH] fixed NoSuchElementException when list has no item (#2804) Co-authored-by: Peter Lubell-Doughtie --- .../ui/appsetting/AppSettingViewModel.kt | 4 +-- .../fhircore/quest/ui/login/LoginViewModel.kt | 2 +- .../quest/ui/login/LoginViewModelTest.kt | 36 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/appsetting/AppSettingViewModel.kt b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/appsetting/AppSettingViewModel.kt index a0cec55b2b..42424d7b07 100644 --- a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/appsetting/AppSettingViewModel.kt +++ b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/appsetting/AppSettingViewModel.kt @@ -278,10 +278,10 @@ constructor( resourceIds.forEach { val responseBundle = fhirResourceDataSource.getResource("$resourceType?${Composition.SP_RES_ID}=$it") - responseBundle?.let { + responseBundle.let { bundleEntryComponents.add( Bundle.BundleEntryComponent().apply { - resource = responseBundle.entry?.first()?.resource + resource = responseBundle.entry?.firstOrNull()?.resource }, ) } diff --git a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/login/LoginViewModel.kt b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/login/LoginViewModel.kt index 5d8a174b70..3f62d20947 100644 --- a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/login/LoginViewModel.kt +++ b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/login/LoginViewModel.kt @@ -344,7 +344,7 @@ constructor( } else { // The assumption here is that only 1 practitioner is returned from the server in the // practitioner details - practitioners.first().identifier.forEach { identifier -> + practitioners.firstOrNull()?.identifier?.forEach { identifier -> if ( identifier.hasUse() && identifier.use == org.hl7.fhir.r4.model.Identifier.IdentifierUse.SECONDARY && diff --git a/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/login/LoginViewModelTest.kt b/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/login/LoginViewModelTest.kt index 2c1376795a..e47ae9917b 100644 --- a/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/login/LoginViewModelTest.kt +++ b/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/login/LoginViewModelTest.kt @@ -517,6 +517,42 @@ internal class LoginViewModelTest : RobolectricTest() { ) } + @Test + fun testSavePractitionerDetailsChaRoleWithIdentifier() { + coEvery { defaultRepository.createRemote(true, any()) } just runs + loginViewModel.savePractitionerDetails( + Bundle() + .addEntry( + Bundle.BundleEntryComponent().apply { + resource = + practitionerDetails().apply { + fhirPractitionerDetails = + FhirPractitionerDetails().apply { + practitioners = + listOf( + Practitioner().apply { + identifier = + listOf( + Identifier().apply { + use = Identifier.IdentifierUse.SECONDARY + value = "cha" + }, + ) + }, + ) + } + } + }, + ), + UserInfo( + keycloakUuid = "cha", + ), + ) {} + Assert.assertNotNull( + sharedPreferencesHelper.read(SharedPreferenceKey.PRACTITIONER_DETAILS.name), + ) + } + @Test fun testSavePractitionerDetailsSupervisorRole() { coEvery { defaultRepository.createRemote(false, any()) } just runs