From 90e9a6ae800af9b2438a56d8805ddb2ef2e41d99 Mon Sep 17 00:00:00 2001 From: Philipp Woessner <philipp.woessner@sap.com> Date: Fri, 7 Aug 2020 15:47:09 +0200 Subject: [PATCH] Fix: use the exposure summary directly from Google instead of the local db entry (EXPOSUREAPP-1837) --- .../storage/ExposureSummaryRepository.kt | 14 +++++----- .../transaction/RiskLevelTransaction.kt | 22 ++------------- .../ui/viewmodel/TracingViewModel.kt | 7 +++-- .../storage/ExposureSummaryRepositoryTest.kt | 27 +++++++++++++++++-- .../transaction/RiskLevelTransactionTest.kt | 8 +++--- 5 files changed, 44 insertions(+), 34 deletions(-) diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/storage/ExposureSummaryRepository.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/storage/ExposureSummaryRepository.kt index 60eb7bae1..f13165bce 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/storage/ExposureSummaryRepository.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/storage/ExposureSummaryRepository.kt @@ -3,6 +3,7 @@ package de.rki.coronawarnapp.storage import androidx.lifecycle.MutableLiveData import com.google.android.gms.nearby.exposurenotification.ExposureSummary import de.rki.coronawarnapp.CoronaWarnApplication +import de.rki.coronawarnapp.nearby.InternalExposureNotificationClient class ExposureSummaryRepository(private val exposureSummaryDao: ExposureSummaryDao) { companion object { @@ -46,13 +47,12 @@ class ExposureSummaryRepository(private val exposureSummaryDao: ExposureSummaryD ExposureSummaryRepository.daysSinceLastExposure.postValue(daysSinceLastExposure) } - suspend fun getLatestExposureSummary() = exposureSummaryDao - .getLatestExposureSummary() - ?.convertToExposureSummary() - .also { - matchedKeyCount.postValue(it?.matchedKeyCount) - daysSinceLastExposure.postValue(it?.daysSinceLastExposure) - } + suspend fun getLatestExposureSummary(token: String) = + InternalExposureNotificationClient.asyncGetExposureSummary(token) + .also { + matchedKeyCount.postValue(it.matchedKeyCount) + daysSinceLastExposure.postValue(it.daysSinceLastExposure) + } private fun ExposureSummaryEntity.convertToExposureSummary() = ExposureSummary.ExposureSummaryBuilder() diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/transaction/RiskLevelTransaction.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/transaction/RiskLevelTransaction.kt index bb272906f..fd019cbe9 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/transaction/RiskLevelTransaction.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/transaction/RiskLevelTransaction.kt @@ -19,7 +19,6 @@ import de.rki.coronawarnapp.risk.RiskLevelCalculation import de.rki.coronawarnapp.risk.TimeVariables import de.rki.coronawarnapp.server.protocols.ApplicationConfigurationOuterClass import de.rki.coronawarnapp.service.applicationconfiguration.ApplicationConfigurationService -import de.rki.coronawarnapp.storage.ExposureSummaryRepository import de.rki.coronawarnapp.storage.LocalData import de.rki.coronawarnapp.storage.RiskLevelRepository import de.rki.coronawarnapp.transaction.RiskLevelTransaction.RiskLevelTransactionState.CHECK_APP_CONNECTIVITY @@ -355,9 +354,9 @@ object RiskLevelTransaction : Transaction() { */ private suspend fun executeRetrieveExposureSummary(): ExposureSummary = executeState(RETRIEVE_EXPOSURE_SUMMARY) { - val lastExposureSummary = getLastExposureSummary() ?: getNewExposureSummary() + val exposureSummary = getNewExposureSummary() - return@executeState lastExposureSummary.also { + return@executeState exposureSummary.also { Timber.v(TAG, "$transactionId - get the exposure summary for further calculation") } } @@ -466,20 +465,6 @@ object RiskLevelTransaction : Transaction() { return false } - /** - * Returns the last stored ExposureSummary from the storage. - * The ExposureSummary will be updated in the [de.rki.coronawarnapp.receiver.ExposureStateUpdateReceiver] - * once the BroadcastReceiver is triggered from the Google Exposure Notification API - * - * @return exposure summary from Google Exposure Notification API - */ - private suspend fun getLastExposureSummary(): ExposureSummary? { - return ExposureSummaryRepository.getExposureSummaryRepository() - .getLatestExposureSummary().also { - Timber.v("used exposure summary for the risk level calculation: $it") - } - } - /** * Make a call to the backend to retrieve the current application configuration values * @@ -540,9 +525,6 @@ object RiskLevelTransaction : Transaction() { val exposureSummary = InternalExposureNotificationClient.asyncGetExposureSummary(googleToken) - ExposureSummaryRepository.getExposureSummaryRepository() - .insertExposureSummaryEntity(exposureSummary) - return exposureSummary.also { Timber.v("$transactionId - generated new exposure summary with $googleToken") } diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/viewmodel/TracingViewModel.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/viewmodel/TracingViewModel.kt index 145486acd..8d3b8e0c5 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/viewmodel/TracingViewModel.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/viewmodel/TracingViewModel.kt @@ -158,8 +158,11 @@ class TracingViewModel : ViewModel() { fun refreshExposureSummary() { viewModelScope.launch { try { - ExposureSummaryRepository.getExposureSummaryRepository() - .getLatestExposureSummary() + val token = LocalData.googleApiToken() + if (token != null) { + ExposureSummaryRepository.getExposureSummaryRepository() + .getLatestExposureSummary(token) + } Timber.v("retrieved latest exposure summary from db") } catch (e: Exception) { e.report( diff --git a/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/storage/ExposureSummaryRepositoryTest.kt b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/storage/ExposureSummaryRepositoryTest.kt index 0fbfc2dcf..ed25927dd 100644 --- a/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/storage/ExposureSummaryRepositoryTest.kt +++ b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/storage/ExposureSummaryRepositoryTest.kt @@ -1,17 +1,20 @@ package de.rki.coronawarnapp.storage import com.google.android.gms.nearby.exposurenotification.ExposureSummary +import de.rki.coronawarnapp.nearby.InternalExposureNotificationClient import io.mockk.MockKAnnotations import io.mockk.coEvery import io.mockk.coVerify import io.mockk.every import io.mockk.impl.annotations.MockK import io.mockk.mockk +import io.mockk.mockkObject import io.mockk.unmockkAll import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before import org.junit.Test +import java.util.UUID /** * ExposureSummaryRepository test. @@ -27,6 +30,9 @@ class ExposureSummaryRepositoryTest { MockKAnnotations.init(this) repository = ExposureSummaryRepository(dao) + mockkObject(InternalExposureNotificationClient) + coEvery { InternalExposureNotificationClient.asyncGetExposureSummary(any()) } returns buildSummary() + coEvery { dao.getExposureSummaryEntities() } returns listOf() coEvery { dao.getLatestExposureSummary() } returns null coEvery { dao.insertExposureSummaryEntity(any()) } returns 0 @@ -52,10 +58,11 @@ class ExposureSummaryRepositoryTest { @Test fun testGetLatest() { runBlocking { - repository.getLatestExposureSummary() + val token = UUID.randomUUID().toString() + repository.getLatestExposureSummary(token) coVerify { - dao.getLatestExposureSummary() + InternalExposureNotificationClient.asyncGetExposureSummary(token) } } } @@ -81,6 +88,22 @@ class ExposureSummaryRepositoryTest { } } + private fun buildSummary( + maxRisk: Int = 0, + lowAttenuation: Int = 0, + midAttenuation: Int = 0, + highAttenuation: Int = 0 + ): ExposureSummary { + val intArray = IntArray(3) + intArray[0] = lowAttenuation + intArray[1] = midAttenuation + intArray[2] = highAttenuation + return ExposureSummary.ExposureSummaryBuilder() + .setMaximumRiskScore(maxRisk) + .setAttenuationDurations(intArray) + .build() + } + @After fun cleanUp() { unmockkAll() diff --git a/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/transaction/RiskLevelTransactionTest.kt b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/transaction/RiskLevelTransactionTest.kt index 288032cc3..3e30b2306 100644 --- a/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/transaction/RiskLevelTransactionTest.kt +++ b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/transaction/RiskLevelTransactionTest.kt @@ -34,6 +34,7 @@ import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before import org.junit.Test +import java.util.UUID import java.util.concurrent.TimeUnit class RiskLevelTransactionTest { @@ -68,6 +69,7 @@ class RiskLevelTransactionTest { every { RiskLevel.riskLevelChangedBetweenLowAndHigh(any(), any()) } returns false every { LocalData.lastTimeRiskLevelCalculation() } returns System.currentTimeMillis() every { LocalData.lastTimeRiskLevelCalculation(any()) } just Runs + every { LocalData.googleApiToken() } returns UUID.randomUUID().toString() every { ConnectivityHelper.isNetworkEnabled(any()) } returns true every { CoronaWarnApplication.getAppContext() } returns context } @@ -247,7 +249,7 @@ class RiskLevelTransactionTest { // the risk score of the last exposure summary is above the high min threshold coEvery { ApplicationConfigurationService.asyncRetrieveApplicationConfiguration() } returns testAppConfig - coEvery { esRepositoryMock.getLatestExposureSummary() } returns testExposureSummary + coEvery { InternalExposureNotificationClient.asyncGetExposureSummary(any()) } returns testExposureSummary runBlocking { @@ -309,7 +311,7 @@ class RiskLevelTransactionTest { // the exposure summary risk score is not below high min score coEvery { ApplicationConfigurationService.asyncRetrieveApplicationConfiguration() } returns testAppConfig - coEvery { esRepositoryMock.getLatestExposureSummary() } returns testExposureSummary + coEvery { InternalExposureNotificationClient.asyncGetExposureSummary(any()) } returns testExposureSummary runBlocking { @@ -373,7 +375,7 @@ class RiskLevelTransactionTest { every { TimeVariables.getTimeActiveTracingDuration() } returns twoHoursAboveMinActiveTracingDuration coEvery { ApplicationConfigurationService.asyncRetrieveApplicationConfiguration() } returns testAppConfig - coEvery { esRepositoryMock.getLatestExposureSummary() } returns testExposureSummary + coEvery { InternalExposureNotificationClient.asyncGetExposureSummary(any()) } returns testExposureSummary runBlocking { -- GitLab