Skip to content

Commit

Permalink
refactor(fossid-webapp): Make factory functions suspending
Browse files Browse the repository at this point in the history
Make the factory functions of the FossID service suspending to leave the
creation of the coroutine context to the consumer of the service.

Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@bosch.com>
  • Loading branch information
mnonnenmacher authored and sschuberth committed Aug 20, 2024
1 parent 9b3cb85 commit a061b06
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion clients/fossid-webapp/src/main/kotlin/FossIdRestService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ interface FossIdRestService {
* Create the [FossIdServiceWithVersion] to interact with the FossID instance running at the given [url],
* optionally using a pre-built OkHttp [client].
*/
fun create(url: String, client: OkHttpClient? = null): FossIdServiceWithVersion {
suspend fun create(url: String, client: OkHttpClient? = null): FossIdServiceWithVersion {
logger.info { "The FossID server URL is $url." }

val retrofit = Retrofit.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

package org.ossreviewtoolkit.clients.fossid

import kotlinx.coroutines.runBlocking

import org.ossreviewtoolkit.clients.fossid.model.status.UnversionedScanDescription

abstract class FossIdServiceWithVersion(val version: String) : FossIdRestService {
Expand All @@ -29,15 +27,14 @@ abstract class FossIdServiceWithVersion(val version: String) : FossIdRestService
* Construct a new instance of [FossIdServiceWithVersion] for given [delegate]. The implementation matching
* FossID version will be instantiated and returned.
*/
fun create(delegate: FossIdRestService): FossIdServiceWithVersion =
runBlocking {
val version = delegate.getFossIdVersion().orEmpty()
suspend fun create(delegate: FossIdRestService): FossIdServiceWithVersion {
val version = delegate.getFossIdVersion().orEmpty()

when {
version >= "2021.2" -> VersionedFossIdService2021dot2(delegate, version)
else -> VersionedFossIdService(delegate, version)
}
return when {
version >= "2021.2" -> VersionedFossIdService2021dot2(delegate, version)
else -> VersionedFossIdService(delegate, version)
}
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions clients/fossid-webapp/src/test/kotlin/FossId2021dot2Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf

import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.coEvery
import io.mockk.mockkObject

import org.ossreviewtoolkit.clients.fossid.model.status.ScanStatus
Expand All @@ -51,7 +51,7 @@ class FossId2021dot2Test : StringSpec({
server.start()

mockkObject(FossIdServiceWithVersion)
every { FossIdServiceWithVersion.create(any()) } answers {
coEvery { FossIdServiceWithVersion.create(any()) } answers {
VersionedFossIdService2021dot2(firstArg(), "2021.2.2")
}

Expand Down
4 changes: 2 additions & 2 deletions clients/fossid-webapp/src/test/kotlin/FossId2023dot1Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf

import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.coEvery
import io.mockk.mockkObject

private const val PROJECT_CODE = "semver4j"
Expand All @@ -52,7 +52,7 @@ class FossId2023dot1Test : StringSpec({
server.start()

mockkObject(FossIdServiceWithVersion.Companion)
every { FossIdServiceWithVersion.Companion.create(any()) } answers {
coEvery { FossIdServiceWithVersion.Companion.create(any()) } answers {
VersionedFossIdService2021dot2(firstArg(), "2023.2.0")
}

Expand Down
4 changes: 2 additions & 2 deletions clients/fossid-webapp/src/test/kotlin/FossIdRulesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.nulls.shouldNotBeNull

import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.coEvery
import io.mockk.mockkObject

import org.ossreviewtoolkit.clients.fossid.model.rules.IgnoreRule
Expand All @@ -49,7 +49,7 @@ class FossIdRulesTest : StringSpec({
server.start()

mockkObject(FossIdServiceWithVersion)
every { FossIdServiceWithVersion.create(any()) } answers {
coEvery { FossIdServiceWithVersion.create(any()) } answers {
VersionedFossIdService2021dot2(firstArg(), "2021.2.2")
}

Expand Down
3 changes: 1 addition & 2 deletions plugins/reporters/fossid/src/main/kotlin/FossIdReporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ class FossIdReporter : Reporter {
}.getOrNull()
} ?: SelectionType.INCLUDE_ALL_LICENSES

val service = FossIdRestService.create(serverUrl)

return runBlocking(Dispatchers.IO) {
val service = FossIdRestService.create(serverUrl)
val scanResults = input.ortResult.getScanResults().values.flatten()
val scanCodes = scanResults.flatMapTo(mutableSetOf()) {
it.additionalData[SCAN_CODE_KEY]?.split(',').orEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import io.mockk.clearAllMocks
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.coVerifyAll
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.mockkStatic
Expand Down Expand Up @@ -273,7 +272,7 @@ private fun createReporterMock(): Pair<FossIdRestService, FossIdReporter> {

val serviceMock = mockk<FossIdServiceWithVersion>()
val reporterMock = spyk<FossIdReporter>()
every { FossIdRestService.create(any()) } returns serviceMock
coEvery { FossIdRestService.create(any()) } returns serviceMock

coEvery {
serviceMock.generateReport(any(), any(), any(), any(), any(), any())
Expand Down
4 changes: 2 additions & 2 deletions plugins/scanners/fossid/src/main/kotlin/FossId.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeoutOrNull

import org.apache.logging.log4j.kotlin.logger
Expand Down Expand Up @@ -97,6 +96,7 @@ import org.ossreviewtoolkit.scanner.provenance.NestedProvenance
import org.ossreviewtoolkit.utils.common.Options
import org.ossreviewtoolkit.utils.common.enumSetOf
import org.ossreviewtoolkit.utils.common.replaceCredentialsInUri
import org.ossreviewtoolkit.utils.ort.runBlocking
import org.ossreviewtoolkit.utils.ort.showStackTrace

import org.semver4j.Semver
Expand Down Expand Up @@ -223,7 +223,7 @@ class FossId internal constructor(
// of scans for each package.
private val createdScans = mutableSetOf<String>()

private val service = FossIdRestService.create(config.serverUrl)
private val service = runBlocking { FossIdRestService.create(config.serverUrl) }

override val version = service.version
override val configuration = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class FossIdSnippetChoiceTest : WordSpec({
mockkObject(FossIdRestService)
mockkObject(VersionControlSystem)

every { FossIdRestService.create(any()) } returns createServiceMock()
coEvery { FossIdRestService.create(any()) } returns createServiceMock()
every { VersionControlSystem.forUrl(any()) } returns createVersionControlSystemMock()
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/scanners/fossid/src/test/kotlin/FossIdTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class FossIdTest : WordSpec({
mockkObject(FossIdRestService)
mockkObject(VersionControlSystem)

every { FossIdRestService.create(any()) } returns createServiceMock()
coEvery { FossIdRestService.create(any()) } returns createServiceMock()
every { VersionControlSystem.forUrl(any()) } returns createVersionControlSystemMock()
}

Expand Down

0 comments on commit a061b06

Please sign in to comment.