Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ package de.rki.coronawarnapp.datadonation.analytics.modules.testresult

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import de.rki.coronawarnapp.coronatest.server.CoronaTestResult
import de.rki.coronawarnapp.datadonation.analytics.common.AnalyticsExposureWindow
import de.rki.coronawarnapp.datadonation.analytics.modules.keysubmission.AnalyticsRAKeySubmissionStorage
import de.rki.coronawarnapp.server.protocols.internal.ppdd.PpaData
import de.rki.coronawarnapp.util.datastore.clear
import de.rki.coronawarnapp.tag
import de.rki.coronawarnapp.util.datastore.dataRecovering
import de.rki.coronawarnapp.util.datastore.distinctUntilChanged
import de.rki.coronawarnapp.util.datastore.trySetValue
import de.rki.coronawarnapp.util.serialization.BaseJackson
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import timber.log.Timber
import java.time.Instant
import javax.inject.Inject
import javax.inject.Singleton
Expand Down Expand Up @@ -185,7 +189,17 @@ open class AnalyticsTestResultSettings(
value = mapper.writeValueAsString(value)
)

suspend fun clear() = dataStore.clear()
suspend fun clear() {
Timber.tag(TAG).d("clear()")
val keys = dataStore.data.first().asMap().keys.filter {
if (sharedPrefKeySuffix.isEmpty()) {
it.name.endsWith(AnalyticsRAKeySubmissionStorage.sharedPrefKeySuffix).not()
} else {
it.name.endsWith(AnalyticsRAKeySubmissionStorage.sharedPrefKeySuffix)
}
}
dataStore.edit { prefs -> keys.forEach { key -> prefs.remove(key) } }
}

companion object {
private const val PREFS_KEY_TEST_RESULT = "testResultDonor.testResultAtRegistration" // wrong name legacy
Expand Down Expand Up @@ -214,3 +228,5 @@ open class AnalyticsTestResultSettings(
"testResultDonor.exposureWindowsUntilTestResult"
}
}

private val TAG = tag<AnalyticsTestResultSettings>()
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import testhelpers.coroutines.runTest2
import testhelpers.preferences.FakeDataStore

class AnalyticsTestResultSettingsTest : BaseTest() {
lateinit var pcrStorage: AnalyticsPCRTestResultSettings
lateinit var raStorage: AnalyticsRATestResultSettings
private lateinit var pcrStorage: AnalyticsPCRTestResultSettings
private lateinit var raStorage: AnalyticsRATestResultSettings
@MockK lateinit var analyticsExposureWindow: AnalyticsExposureWindow
@MockK lateinit var analyticsScanInstance: AnalyticsScanInstance

Expand Down Expand Up @@ -99,4 +99,174 @@ class AnalyticsTestResultSettingsTest : BaseTest() {
pcrStorage.exposureWindowsUntilTestResult.first() shouldBe listOf(analyticsExposureWindow)
raStorage.exposureWindowsUntilTestResult.first() shouldBe null
}

@Test
fun `dataMixedRatPcr - clear PCR does not clear RAT`() = runTest2 {
pcrStorage.updateTestRegisteredAt(Instant.ofEpochMilli(1000))
pcrStorage.testRegisteredAt.first() shouldBe Instant.ofEpochMilli(1000)
raStorage.updateTestRegisteredAt(Instant.ofEpochMilli(1000))
raStorage.testRegisteredAt.first() shouldBe Instant.ofEpochMilli(1000)

pcrStorage.updateFinalTestResultReceivedAt(Instant.ofEpochMilli(3000))
pcrStorage.finalTestResultReceivedAt.first() shouldBe Instant.ofEpochMilli(3000)
raStorage.updateFinalTestResultReceivedAt(Instant.ofEpochMilli(3000))
raStorage.finalTestResultReceivedAt.first() shouldBe Instant.ofEpochMilli(3000)

pcrStorage.updateEwDaysSinceMostRecentDateAtRiskLevelAtTestRegistration(3)
pcrStorage.ewDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe 3
raStorage.updateEwDaysSinceMostRecentDateAtRiskLevelAtTestRegistration(3)
raStorage.ewDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe 3

pcrStorage.updatePtDaysSinceMostRecentDateAtRiskLevelAtTestRegistration(2)
pcrStorage.ptDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe 2
raStorage.updatePtDaysSinceMostRecentDateAtRiskLevelAtTestRegistration(2)
raStorage.ptDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe 2

pcrStorage.updateEwHoursSinceHighRiskWarningAtTestRegistration(10)
pcrStorage.ewHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe 10
raStorage.updateEwHoursSinceHighRiskWarningAtTestRegistration(10)
raStorage.ewHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe 10

pcrStorage.updatePtHoursSinceHighRiskWarningAtTestRegistration(10)
pcrStorage.ptHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe 10
raStorage.updatePtHoursSinceHighRiskWarningAtTestRegistration(10)
raStorage.ptHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe 10

pcrStorage.updateEwRiskLevelAtTestRegistration(PpaData.PPARiskLevel.RISK_LEVEL_HIGH)
pcrStorage.ewRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_HIGH
raStorage.updateEwRiskLevelAtTestRegistration(PpaData.PPARiskLevel.RISK_LEVEL_HIGH)
raStorage.ewRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_HIGH

pcrStorage.updatePtRiskLevelAtTestRegistration(PpaData.PPARiskLevel.RISK_LEVEL_HIGH)
pcrStorage.ptRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_HIGH
raStorage.updatePtRiskLevelAtTestRegistration(PpaData.PPARiskLevel.RISK_LEVEL_HIGH)
raStorage.ptRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_HIGH

pcrStorage.updateExposureWindowsAtTestRegistration(listOf(analyticsExposureWindow))
pcrStorage.exposureWindowsAtTestRegistration.first() shouldBe listOf(analyticsExposureWindow)
raStorage.updateExposureWindowsAtTestRegistration(listOf(analyticsExposureWindow))
raStorage.exposureWindowsAtTestRegistration.first() shouldBe listOf(analyticsExposureWindow)

pcrStorage.updateExposureWindowsUntilTestResult(listOf(analyticsExposureWindow))
pcrStorage.exposureWindowsUntilTestResult.first() shouldBe listOf(analyticsExposureWindow)
raStorage.updateExposureWindowsUntilTestResult(listOf(analyticsExposureWindow))
raStorage.exposureWindowsUntilTestResult.first() shouldBe listOf(analyticsExposureWindow)

pcrStorage.clear()

raStorage.testRegisteredAt.first() shouldBe Instant.ofEpochMilli(1000)
pcrStorage.testRegisteredAt.first() shouldBe null

raStorage.finalTestResultReceivedAt.first() shouldBe Instant.ofEpochMilli(3000)
pcrStorage.finalTestResultReceivedAt.first() shouldBe null

raStorage.ewDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe 3
pcrStorage.ewDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe -1

raStorage.ptDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe 2
pcrStorage.ptDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe -1

raStorage.ewHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe 10
pcrStorage.ewHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe -1

raStorage.ptHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe 10
pcrStorage.ptHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe -1

raStorage.ewRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_HIGH
pcrStorage.ewRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_LOW

raStorage.ptRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_HIGH
pcrStorage.ptRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_LOW

raStorage.exposureWindowsAtTestRegistration.first() shouldBe listOf(analyticsExposureWindow)
pcrStorage.exposureWindowsAtTestRegistration.first() shouldBe null

raStorage.exposureWindowsUntilTestResult.first() shouldBe listOf(analyticsExposureWindow)
pcrStorage.exposureWindowsUntilTestResult.first() shouldBe null
}

@Test
fun `dataMixedRatPcr - clear RAT does clear PCR`() = runTest2 {
pcrStorage.updateTestRegisteredAt(Instant.ofEpochMilli(1000))
pcrStorage.testRegisteredAt.first() shouldBe Instant.ofEpochMilli(1000)
raStorage.updateTestRegisteredAt(Instant.ofEpochMilli(1000))
raStorage.testRegisteredAt.first() shouldBe Instant.ofEpochMilli(1000)

pcrStorage.updateFinalTestResultReceivedAt(Instant.ofEpochMilli(3000))
pcrStorage.finalTestResultReceivedAt.first() shouldBe Instant.ofEpochMilli(3000)
raStorage.updateFinalTestResultReceivedAt(Instant.ofEpochMilli(3000))
raStorage.finalTestResultReceivedAt.first() shouldBe Instant.ofEpochMilli(3000)

pcrStorage.updateEwDaysSinceMostRecentDateAtRiskLevelAtTestRegistration(3)
pcrStorage.ewDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe 3
raStorage.updateEwDaysSinceMostRecentDateAtRiskLevelAtTestRegistration(3)
raStorage.ewDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe 3

pcrStorage.updatePtDaysSinceMostRecentDateAtRiskLevelAtTestRegistration(2)
pcrStorage.ptDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe 2
raStorage.updatePtDaysSinceMostRecentDateAtRiskLevelAtTestRegistration(2)
raStorage.ptDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe 2

pcrStorage.updateEwHoursSinceHighRiskWarningAtTestRegistration(10)
pcrStorage.ewHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe 10
raStorage.updateEwHoursSinceHighRiskWarningAtTestRegistration(10)
raStorage.ewHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe 10

pcrStorage.updatePtHoursSinceHighRiskWarningAtTestRegistration(10)
pcrStorage.ptHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe 10
raStorage.updatePtHoursSinceHighRiskWarningAtTestRegistration(10)
raStorage.ptHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe 10

pcrStorage.updateEwRiskLevelAtTestRegistration(PpaData.PPARiskLevel.RISK_LEVEL_HIGH)
pcrStorage.ewRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_HIGH
raStorage.updateEwRiskLevelAtTestRegistration(PpaData.PPARiskLevel.RISK_LEVEL_HIGH)
raStorage.ewRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_HIGH

pcrStorage.updatePtRiskLevelAtTestRegistration(PpaData.PPARiskLevel.RISK_LEVEL_HIGH)
pcrStorage.ptRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_HIGH
raStorage.updatePtRiskLevelAtTestRegistration(PpaData.PPARiskLevel.RISK_LEVEL_HIGH)
raStorage.ptRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_HIGH

pcrStorage.updateExposureWindowsAtTestRegistration(listOf(analyticsExposureWindow))
pcrStorage.exposureWindowsAtTestRegistration.first() shouldBe listOf(analyticsExposureWindow)
raStorage.updateExposureWindowsAtTestRegistration(listOf(analyticsExposureWindow))
raStorage.exposureWindowsAtTestRegistration.first() shouldBe listOf(analyticsExposureWindow)

pcrStorage.updateExposureWindowsUntilTestResult(listOf(analyticsExposureWindow))
pcrStorage.exposureWindowsUntilTestResult.first() shouldBe listOf(analyticsExposureWindow)
raStorage.updateExposureWindowsUntilTestResult(listOf(analyticsExposureWindow))
raStorage.exposureWindowsUntilTestResult.first() shouldBe listOf(analyticsExposureWindow)

raStorage.clear()

pcrStorage.testRegisteredAt.first() shouldBe Instant.ofEpochMilli(1000)
raStorage.testRegisteredAt.first() shouldBe null

pcrStorage.finalTestResultReceivedAt.first() shouldBe Instant.ofEpochMilli(3000)
raStorage.finalTestResultReceivedAt.first() shouldBe null

pcrStorage.ewDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe 3
raStorage.ewDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe -1

pcrStorage.ptDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe 2
raStorage.ptDaysSinceMostRecentDateAtRiskLevelAtTestRegistration.first() shouldBe -1

pcrStorage.ewHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe 10
raStorage.ewHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe -1

pcrStorage.ptHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe 10
raStorage.ptHoursSinceHighRiskWarningAtTestRegistration.first() shouldBe -1

pcrStorage.ewRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_HIGH
raStorage.ewRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_LOW

pcrStorage.ptRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_HIGH
raStorage.ptRiskLevelAtTestRegistration.first() shouldBe PpaData.PPARiskLevel.RISK_LEVEL_LOW

pcrStorage.exposureWindowsAtTestRegistration.first() shouldBe listOf(analyticsExposureWindow)
raStorage.exposureWindowsAtTestRegistration.first() shouldBe null

pcrStorage.exposureWindowsUntilTestResult.first() shouldBe listOf(analyticsExposureWindow)
raStorage.exposureWindowsUntilTestResult.first() shouldBe null
}
}