All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.trendyol.stove.testing.e2e.KtorApplicationUnderTest.kt Maven / Gradle / Ivy

There is a newer version: 1f1ca59
Show newest version
@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")
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy