Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic HermesMock listener for kotest #1836

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[versions]
kotest = "5.5.4"
kotlin-logging = "6.0.3"

[libraries]
kotest-assertions = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" }
kotest-api = { module = "io.kotest:kotest-framework-api", version.ref = "kotest" }
kotest-runner = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" }
kotlin-logging = { module = "io.github.oshai:kotlin-logging-jvm", version.ref = "kotlin-logging" }
19 changes: 19 additions & 0 deletions hermes-mock-kotest-extension/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
kotlin("jvm") version "1.9.22"
}

repositories {
mavenCentral()
}

dependencies {
api(project(":hermes-mock"))
implementation(libs.kotest.api)
implementation(libs.kotlin.logging)
testImplementation(libs.kotest.runner)
testImplementation(libs.kotest.assertions)
}

tasks.test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package pl.allegro.tech.hermes.mock.kotest

import io.github.oshai.kotlinlogging.KotlinLogging
import io.kotest.core.listeners.ProjectListener
import io.kotest.core.listeners.TestListener
import io.kotest.core.spec.Spec
import io.kotest.core.test.TestCase
import io.kotest.core.test.TestResult
import pl.allegro.tech.hermes.mock.HermesMock

private val logger = KotlinLogging.logger { }

class HermesMockListener(
private val hermesMock: HermesMock,
private val listenerMode: ListenerMode
) : TestListener, ProjectListener {
override suspend fun beforeTest(testCase: TestCase) {
if (listenerMode == ListenerMode.PER_TEST) {
logger.debug { "Started Mock Hermes $listenerMode" }
hermesMock.start()
}
}

override suspend fun afterTest(testCase: TestCase, result: TestResult) {
if (listenerMode == ListenerMode.PER_TEST) {
logger.debug { "Stopped Mock Hermes $listenerMode" }
hermesMock.stop()
}
}

override suspend fun beforeSpec(spec: Spec) {
if (listenerMode == ListenerMode.PER_SPEC) {
logger.debug { "Started Mock Hermes $listenerMode" }
hermesMock.start()
}
}

override suspend fun afterSpec(spec: Spec) {
if (listenerMode == ListenerMode.PER_SPEC) {
logger.debug { "Stopped Mock Hermes $listenerMode" }
hermesMock.stop()
}
}

override suspend fun beforeProject() {
if (listenerMode == ListenerMode.PER_PROJECT) {
logger.debug { "Started Mock Hermes $listenerMode" }
hermesMock.start()
}
}

override suspend fun afterProject() {
if (listenerMode == ListenerMode.PER_PROJECT) {
logger.debug { "Stopped Mock Hermes $listenerMode" }
hermesMock.stop()
}
}

companion object {
fun perSpec(hermesMock: HermesMock) = HermesMockListener(hermesMock, ListenerMode.PER_SPEC)
fun perTest(hermesMock: HermesMock) = HermesMockListener(hermesMock, ListenerMode.PER_TEST)
fun perProject(hermesMock: HermesMock) = HermesMockListener(hermesMock, ListenerMode.PER_PROJECT)
}
}

enum class ListenerMode {
PER_TEST,
PER_SPEC,
PER_PROJECT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pl.allegro.tech.hermes.mock.kotest

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.net.HttpURLConnection
import java.net.URL

class HermesMockListenerPerProjectTest : FunSpec({

test("should have started HermesMock") {
val connection = URL("http://localhost:${ProjectConfig.HERMES_MOCK_PORT}").openConnection() as HttpURLConnection

connection.responseCode shouldBe 404
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pl.allegro.tech.hermes.mock.kotest

import io.kotest.core.config.AbstractProjectConfig
import io.kotest.core.extensions.Extension
import pl.allegro.tech.hermes.mock.HermesMock

object ProjectConfig : AbstractProjectConfig() {
const val HERMES_MOCK_PORT = 9001

val hermesMock = HermesMock.Builder().withPort(HERMES_MOCK_PORT).build()
val hermesMockListener = HermesMockListener(hermesMock, ListenerMode.PER_PROJECT)

override fun extensions(): List<Extension> = listOf(hermesMockListener)
}
6 changes: 5 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.5.0'
}
rootProject.name='hermes'

include 'hermes-common',
Expand All @@ -13,5 +16,6 @@ include 'hermes-common',
'hermes-schema',
'hermes-benchmark',
'hermes-mock',
'integration-tests'
'integration-tests',
'hermes-mock-kotest-extension'

Loading