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

com.bybutter.sisyphus.test.SisyphusTestEngineContext.kt Maven / Gradle / Ivy

There is a newer version: 2.1.22
Show newest version
package com.bybutter.sisyphus.test

import com.bybutter.sisyphus.dsl.cel.CelEngine
import com.bybutter.sisyphus.test.extension.Extension
import io.grpc.Channel
import io.grpc.ManagedChannelBuilder
import org.junit.platform.engine.support.hierarchical.EngineExecutionContext

interface SisyphusTestEngineContext : EngineExecutionContext {
    fun cel(): CelEngine

    fun channel(authority: String): Channel

    fun results(): Map

    fun extensions(): List
}

inline fun  SisyphusTestEngineContext.extensions(block: T.() -> Unit) {
    extensions().forEach {
        if (it !is T) return@forEach
        it.block()
    }
}

class SisyphusTestRootContext(extensions: List) : SisyphusTestEngineContext {
    private val celEngine: CelEngine = CelEngine()
    private val channels: MutableMap = mutableMapOf()
    private val extensions: MutableList = extensions.toMutableList()

    override fun cel(): CelEngine {
        return celEngine
    }

    override fun channel(authority: String): Channel {
        return channels.getOrPut(authority) {
            ManagedChannelBuilder.forTarget(authority).usePlaintext().build()
        }
    }

    override fun results(): Map {
        return mapOf()
    }

    override fun extensions(): List {
        return extensions
    }

    fun forCase(): SisyphusTestCaseContext {
        return SisyphusTestCaseContext(this)
    }
}

class SisyphusTestCaseContext(val parent: SisyphusTestRootContext) : SisyphusTestEngineContext {
    private val _results: MutableMap = mutableMapOf()

    override fun cel(): CelEngine {
        return parent.cel()
    }

    override fun channel(authority: String): Channel {
        return parent.channel(authority)
    }

    override fun results(): Map {
        return _results
    }

    override fun extensions(): List {
        return parent.extensions()
    }

    fun record(result: TestResult) {
        val id = result.step?.id?.takeIf { it.isNotEmpty() } ?: return
        _results[id] = result
    }

    fun forStep(callContext: CallContext): SisyphusTestStepContext {
        return SisyphusTestStepContext(this, callContext)
    }
}

class SisyphusTestStepContext(
    val parent: SisyphusTestCaseContext,
    val callContext: CallContext
) : SisyphusTestEngineContext {
    override fun cel(): CelEngine {
        return parent.cel()
    }

    override fun channel(authority: String): Channel {
        return parent.channel(authority)
    }

    override fun results(): Map {
        return parent.results()
    }

    override fun extensions(): List {
        return parent.extensions()
    }

    fun record(result: TestResult) {
        return parent.record(result)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy