com.trendyol.stove.testing.e2e.KtorApplicationUnderTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stove-ktor-testing-e2e Show documentation
Show all versions of stove-ktor-testing-e2e Show documentation
The easiest way of e2e testing in Kotlin
@file:Suppress("UNCHECKED_CAST")
package com.trendyol.stove.testing.e2e
import com.trendyol.stove.testing.e2e.system.*
import com.trendyol.stove.testing.e2e.system.abstractions.*
import com.trendyol.stove.testing.e2e.system.annotations.StoveDsl
import io.ktor.server.application.*
import kotlinx.coroutines.*
/**
* Definition for Application Under Test for Ktor enabled application
*/
@StoveDsl
internal fun TestSystem.systemUnderTest(
runner: Runner,
withParameters: List = listOf()
): ReadyTestSystem = applicationUnderTest(KtorApplicationUnderTest(this, runner, withParameters))
@StoveDsl
fun WithDsl.ktor(
runner: Runner,
withParameters: List = listOf()
): ReadyTestSystem = this.testSystem.systemUnderTest(runner, withParameters)
@StoveDsl
class KtorApplicationUnderTest(
private val testSystem: TestSystem,
private val runner: Runner,
private val parameters: List
) : ApplicationUnderTest {
private lateinit var application: Application
override suspend fun start(configurations: List): Application = coroutineScope {
val allConfigurations = (configurations + defaultConfigurations() + parameters)
.map { "--$it" }
.distinct()
.toTypedArray()
application = runner(allConfigurations)
testSystem.activeSystems
.map { it.value }
.filter { it is RunnableSystemWithContext<*> || it is AfterRunAwareWithContext<*> }
.map { it as AfterRunAwareWithContext }
.map { async { it.afterRun(application) } }
.awaitAll()
application
}
override suspend fun stop(): Unit = application.dispose()
private fun defaultConfigurations(): Array = arrayOf("test-system=true")
}