Skip to content
Snippets Groups Projects
Unverified Commit 17f2b8f5 authored by Matthias Urhahn's avatar Matthias Urhahn Committed by GitHub
Browse files

Improve test registration (DEV) (#3004)


* Remove duplicate code.

* Custom exceptions.

Co-authored-by: default avatarMohamed <mohamed.metwalli@sap.com>
parent d90f5a45
No related branches found
No related tags found
No related merge requests found
package de.rki.coronawarnapp.coronatest package de.rki.coronawarnapp.coronatest
import androidx.annotation.VisibleForTesting
import de.rki.coronawarnapp.bugreporting.reportProblem import de.rki.coronawarnapp.bugreporting.reportProblem
import de.rki.coronawarnapp.coronatest.errors.CoronaTestNotFoundException 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.migration.PCRTestMigration
import de.rki.coronawarnapp.coronatest.qrcode.CoronaTestGUID import de.rki.coronawarnapp.coronatest.qrcode.CoronaTestGUID
import de.rki.coronawarnapp.coronatest.qrcode.CoronaTestQRCode import de.rki.coronawarnapp.coronatest.qrcode.CoronaTestQRCode
...@@ -71,44 +72,23 @@ class CoronaTestRepository @Inject constructor( ...@@ -71,44 +72,23 @@ class CoronaTestRepository @Inject constructor(
private fun getProcessor(type: CoronaTest.Type) = processors.single { it.type == type } private fun getProcessor(type: CoronaTest.Type) = processors.single { it.type == type }
suspend fun registerTest(registrationRequest: TestRegistrationRequest): CoronaTest = when (registrationRequest) { suspend fun registerTest(request: TestRegistrationRequest): CoronaTest {
is CoronaTestQRCode -> registerTestByQRCode(registrationRequest) Timber.tag(TAG).i("registerTest(request=%s)", request)
is CoronaTestTAN -> registerTestByTAN(registrationRequest)
else -> throw IllegalArgumentException("Unknown test request: $registrationRequest")
}
@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... // We check early, if there is no processor, crash early, "should" never happen though...
val processor = getProcessor(request.type) val processor = getProcessor(request.type)
val currentTests = internalData.updateBlocking { val currentTests = internalData.updateBlocking {
if (values.any { it.type == request.type }) { 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) val test = when (request) {
Timber.tag(TAG).i("Adding new test: %s", test) is CoronaTestQRCode -> processor.create(request)
is CoronaTestTAN -> processor.create(request)
toMutableMap().apply { this[test.identifier] = test } else -> throw UnknownTestTypeException("Unknown test request: $request")
}
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 = processor.create(request)
Timber.tag(TAG).i("Adding new test: %s", test) Timber.tag(TAG).i("Adding new test: %s", test)
toMutableMap().apply { this[test.identifier] = test } toMutableMap().apply { this[test.identifier] = test }
......
package de.rki.coronawarnapp.coronatest.errors
class DuplicateCoronaTestException(
message: String
) : IllegalArgumentException(message)
package de.rki.coronawarnapp.coronatest.errors
class UnknownTestTypeException(
message: String
) : IllegalArgumentException(message)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment