Skip to content

Commit

Permalink
Added ktorm listener
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyMoonkin committed Apr 8, 2021
1 parent b782b29 commit b4f8fbc
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 1 deletion.
46 changes: 46 additions & 0 deletions allure-ktorm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
description = "Allure Ktorm Integration"

plugins {
id("org.jetbrains.kotlin.jvm") version "1.4.31"
}
apply(plugin = "kotlin")

val agent: Configuration by configurations.creating

val ktormVersion = "3.3.0"
val assertK = "0.23.1"
val h2 = "1.4.197"

dependencies {
agent("org.aspectj:aspectjweaver")
api(project(":allure-attachments"))

implementation("org.ktorm:ktorm-core:$ktormVersion")

testImplementation("com.h2database:h2:$h2")

testImplementation("com.willowtreeapps.assertk:assertk-jvm:$assertK")
testImplementation("org.junit.jupiter:junit-jupiter-api")
testImplementation("org.mockito:mockito-core")
testImplementation("org.slf4j:slf4j-simple")
testImplementation(project(":allure-java-commons-test"))
testImplementation(project(":allure-junit-platform"))
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}

tasks.jar {
manifest {
attributes(
mapOf(
"Automatic-Module-Name" to "io.qameta.allure.ktorm"
)
)
}
}

tasks.test {
useJUnitPlatform()
doFirst {
jvmArgs("-javaagent:${agent.singleFile}")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2021 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.ktorm

import io.qameta.allure.Allure
import io.qameta.allure.model.Status
import io.qameta.allure.model.StepResult
import org.ktorm.logging.Logger
import java.util.UUID

/**
* Allure logger for Ktorm.
*
* Create attachments with sql, parameters and results.
* Unfortunately now we can't add parameters and results to created step.
*
* @author crazyMoonkin(Sergey Frolov)
*
* @property createSqlSteps enable creating steps
*/
class AllureKtormLogger(
private val createSqlSteps: Boolean = false,
private val attachParams: Boolean = true,
private val attachResults: Boolean = true,
) : Logger {

override fun isTraceEnabled(): Boolean = false

override fun trace(msg: String, e: Throwable?) {
log(msg, e)
}

override fun isDebugEnabled(): Boolean = true

override fun debug(msg: String, e: Throwable?) {
log(msg, e)
}

override fun isInfoEnabled(): Boolean = false

override fun info(msg: String, e: Throwable?) {
log(msg, e)
}

override fun isWarnEnabled(): Boolean = false

override fun warn(msg: String, e: Throwable?) {
log(msg, e)
}

override fun isErrorEnabled(): Boolean = true

override fun error(msg: String, e: Throwable?) {
log(msg, e)
}

private fun log(msg: String, e: Throwable?) {
val typedMessage = msg.toTypedMessage()
lateinit var stepUUID: String

if (createSqlSteps && typedMessage?.type == MessageType.SQL) {
stepUUID = UUID.randomUUID().toString()
createStep(stepUUID = stepUUID)
}
try {
typedMessage?.let {
when {
typedMessage.type == MessageType.SQL
|| typedMessage.type == MessageType.PARAMETERS && attachParams
|| typedMessage.type == MessageType.RESULTS && attachResults -> {
Allure.addAttachment(typedMessage.type.name, typedMessage.msg)
}
}
}

} finally {
if (createSqlSteps && typedMessage?.type == MessageType.SQL) {
e?.let { Allure.getLifecycle().updateStep(stepUUID) { it.status = Status.FAILED } }
Allure.getLifecycle().stopStep(stepUUID)
}
}
}

private fun createStep(stepUUID: String) = Allure.getLifecycle().startStep(
stepUUID,
StepResult().apply {
name = "Executed SQL query"
status = Status.PASSED
}
)

/**
* Split logged messages and convert it to TypedMessage
* Logged messages is type and message separated by colon
*/
private fun String.toTypedMessage() = split(": ").takeIf { it.size == 2 }?.let { msgParts ->
MessageType.values().firstOrNull { it.name == msgParts[0].toUpperCase() }?.let {
TypedMessage(
type = it,
msg = msgParts[1]
)
}
}

internal enum class MessageType {
SQL,
RESULTS,
PARAMETERS,
}

internal data class TypedMessage(val type: MessageType, val msg: String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2021 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.qameta.allure.ktorm

import assertk.assertThat
import assertk.assertions.containsExactly
import assertk.assertions.extracting
import io.qameta.allure.model.Attachment
import io.qameta.allure.model.TestResult
import org.junit.jupiter.api.Test
import org.ktorm.dsl.from
import org.ktorm.dsl.insert
import org.ktorm.dsl.select

internal class AllureKtormLoggerTest : BaseTest() {

@Test
fun selectShouldCreateAttachments() {
val results = execute(database.from(Departments).select()::rowSet)

assertThat(results.testResults.flatMap(TestResult::getAttachments))
.extracting(Attachment::getName)
.containsExactly("SQL", "PARAMETERS", "RESULTS")
}

@Test
fun insertShouldCreateAttachments() {
val results = execute {
database.insert(Departments) {
set(it.name, "John")
set(it.location, "Moscow")
}
}

assertThat(results.testResults.flatMap(TestResult::getAttachments))
.extracting(Attachment::getName)
.containsExactly("SQL", "PARAMETERS")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2021 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.ktorm

import assertk.assertThat
import assertk.assertions.containsExactly
import assertk.assertions.extracting
import io.qameta.allure.model.Attachment
import io.qameta.allure.model.StepResult
import io.qameta.allure.model.TestResult
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.ktorm.database.Database
import org.ktorm.dsl.from
import org.ktorm.dsl.insert
import org.ktorm.dsl.select

internal class AllureKtormLoggerWithCreateStepsTest : BaseTest() {

@BeforeEach
override fun setup() {
database = Database.connect(
url = "jdbc:h2:mem:ktorm;DB_CLOSE_DELAY=-1",
driver = "org.h2.Driver",
logger = AllureKtormLogger(createSqlSteps = true),
alwaysQuoteIdentifiers = true
)

execSqlScript("init-data.sql")
}

@Test
fun selectShouldCreateStepAndAttachments() {
val results = execute(database.from(Departments).select()::rowSet)

assertThat(results.testResults.flatMap(TestResult::getSteps))
.extracting(StepResult::getName)
.containsExactly("Executed SQL query")

assertThat(results.testResults.flatMap(TestResult::getSteps).flatMap(StepResult::getAttachments))
.extracting(Attachment::getName)
.containsExactly("SQL")

assertThat(results.testResults.flatMap(TestResult::getAttachments))
.extracting(Attachment::getName)
.containsExactly("PARAMETERS", "RESULTS")
}

@Test
fun insertShouldCreateStepAndAttachments() {
val results = execute {
database.insert(Departments) {
set(it.name, "John")
set(it.location, "Moscow")
}
}

assertThat(results.testResults.flatMap(TestResult::getSteps))
.extracting(StepResult::getName)
.containsExactly("Executed SQL query")

assertThat(results.testResults.flatMap(TestResult::getSteps).flatMap(StepResult::getAttachments))
.extracting(Attachment::getName)
.containsExactly("SQL")

assertThat(results.testResults.flatMap(TestResult::getAttachments))
.extracting(Attachment::getName)
.containsExactly("PARAMETERS")
}
}
Loading

0 comments on commit b4f8fbc

Please sign in to comment.