From 17f2b8f5775860bea338eca4e00c09187544328a Mon Sep 17 00:00:00 2001 From: Matthias Urhahn <matthias.urhahn@sap.com> Date: Fri, 30 Apr 2021 13:05:51 +0200 Subject: [PATCH] Improve test registration (DEV) (#3004) * Remove duplicate code. * Custom exceptions. Co-authored-by: Mohamed <mohamed.metwalli@sap.com> --- .../coronatest/CoronaTestRepository.kt | 38 +++++-------------- .../errors/DuplicateCoronaTestException.kt | 5 +++ .../errors/UnknownTestTypeException.kt | 5 +++ 3 files changed, 19 insertions(+), 29 deletions(-) create mode 100644 Corona-Warn-App/src/main/java/de/rki/coronawarnapp/coronatest/errors/DuplicateCoronaTestException.kt create mode 100644 Corona-Warn-App/src/main/java/de/rki/coronawarnapp/coronatest/errors/UnknownTestTypeException.kt diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/coronatest/CoronaTestRepository.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/coronatest/CoronaTestRepository.kt index bcac8ebee..740b2545e 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/coronatest/CoronaTestRepository.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/coronatest/CoronaTestRepository.kt @@ -1,8 +1,9 @@ package de.rki.coronawarnapp.coronatest -import androidx.annotation.VisibleForTesting import de.rki.coronawarnapp.bugreporting.reportProblem import de.rki.coronawarnapp.coronatest.errors.CoronaTestNotFoundException +import de.rki.coronawarnapp.coronatest.errors.DuplicateCoronaTestException +import de.rki.coronawarnapp.coronatest.errors.UnknownTestTypeException import de.rki.coronawarnapp.coronatest.migration.PCRTestMigration import de.rki.coronawarnapp.coronatest.qrcode.CoronaTestGUID import de.rki.coronawarnapp.coronatest.qrcode.CoronaTestQRCode @@ -71,44 +72,23 @@ class CoronaTestRepository @Inject constructor( private fun getProcessor(type: CoronaTest.Type) = processors.single { it.type == type } - suspend fun registerTest(registrationRequest: TestRegistrationRequest): CoronaTest = when (registrationRequest) { - is CoronaTestQRCode -> registerTestByQRCode(registrationRequest) - is CoronaTestTAN -> registerTestByTAN(registrationRequest) - else -> throw IllegalArgumentException("Unknown test request: $registrationRequest") - } + suspend fun registerTest(request: TestRegistrationRequest): CoronaTest { + Timber.tag(TAG).i("registerTest(request=%s)", request) - @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) - internal suspend fun registerTestByTAN(request: CoronaTestTAN): CoronaTest { - Timber.tag(TAG).i("registerTestByQRCode(request=%s)", request) // We check early, if there is no processor, crash early, "should" never happen though... val processor = getProcessor(request.type) val currentTests = internalData.updateBlocking { if (values.any { it.type == request.type }) { - throw IllegalStateException("There is already a test of this type: ${request.type}.") + throw DuplicateCoronaTestException("There is already a test of this type: ${request.type}.") } - val test = processor.create(request) - Timber.tag(TAG).i("Adding new test: %s", test) - - toMutableMap().apply { this[test.identifier] = test } - } - - return currentTests[request.identifier]!! - } - - @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) - internal suspend fun registerTestByQRCode(request: CoronaTestQRCode): CoronaTest { - Timber.tag(TAG).i("registerTestByQRCode(request=%s)", request) - // We check early, if there is no processor, crash early, "should" never happen though... - val processor = getProcessor(request.type) - - val currentTests = internalData.updateBlocking { - if (values.any { it.type == request.type }) { - throw IllegalStateException("There is already a test of this type: ${request.type}.") + val test = when (request) { + is CoronaTestQRCode -> processor.create(request) + is CoronaTestTAN -> processor.create(request) + else -> throw UnknownTestTypeException("Unknown test request: $request") } - val test = processor.create(request) Timber.tag(TAG).i("Adding new test: %s", test) toMutableMap().apply { this[test.identifier] = test } diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/coronatest/errors/DuplicateCoronaTestException.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/coronatest/errors/DuplicateCoronaTestException.kt new file mode 100644 index 000000000..13efa1802 --- /dev/null +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/coronatest/errors/DuplicateCoronaTestException.kt @@ -0,0 +1,5 @@ +package de.rki.coronawarnapp.coronatest.errors + +class DuplicateCoronaTestException( + message: String +) : IllegalArgumentException(message) diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/coronatest/errors/UnknownTestTypeException.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/coronatest/errors/UnknownTestTypeException.kt new file mode 100644 index 000000000..f68ab2721 --- /dev/null +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/coronatest/errors/UnknownTestTypeException.kt @@ -0,0 +1,5 @@ +package de.rki.coronawarnapp.coronatest.errors + +class UnknownTestTypeException( + message: String +) : IllegalArgumentException(message) -- GitLab