-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b782b29
commit 2771067
Showing
8 changed files
with
427 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
allure-ktorm/src/main/kotlin/io/qameta/allure/ktorm/AllureKtormLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ | ||
open 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) | ||
} | ||
|
||
protected 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] | ||
) | ||
} | ||
} | ||
|
||
protected enum class MessageType { | ||
SQL, | ||
RESULTS, | ||
PARAMETERS, | ||
} | ||
|
||
protected data class TypedMessage(val type: MessageType, val msg: String) | ||
} |
53 changes: 53 additions & 0 deletions
53
allure-ktorm/src/test/kotlin/io/qameta/allure/ktorm/AllureKtormLoggerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
allure-ktorm/src/test/kotlin/io/qameta/allure/ktorm/AllureKtormLoggerWithCreateStepsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
Oops, something went wrong.