Skip to content

Commit

Permalink
fix up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Sep 29, 2024
1 parent 27311fa commit c8e7285
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 25 deletions.
10 changes: 10 additions & 0 deletions client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

plugins {
id("Entropy.kotlin-common-conventions")
id("com.ncorti.ktfmt.gradle") version "0.15.1"
Expand Down Expand Up @@ -31,3 +34,10 @@ task<JavaExec>("runDev") {
}
)
}

tasks.withType<Test> {
testLogging {
events = mutableSetOf(TestLogEvent.STARTED, TestLogEvent.FAILED)
exceptionFormat = TestExceptionFormat.FULL
}
}
14 changes: 7 additions & 7 deletions client/src/test/kotlin/http/HttpClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class HttpClientTest : AbstractTest() {

val responseLog = verifyLog("http.response", Level.ERROR)
responseLog.message shouldBe "Received 404 for GET /test-endpoint"
responseLog.getLogFields()["responseCode"] shouldBe 404
responseLog.findLogField("responseCode") shouldBe 404
}

@Test
Expand All @@ -101,9 +101,9 @@ class HttpClientTest : AbstractTest() {

val responseLog = verifyLog("http.response", Level.ERROR)
responseLog.message shouldBe "Received 409 for GET /test-endpoint"
responseLog.getLogFields()["responseCode"] shouldBe 409
responseLog.getLogFields()["clientErrorCode"] shouldBe ClientErrorCode("oh.dear")
responseLog.getLogFields()["clientErrorMessage"] shouldBe "a bid already exists"
responseLog.findLogField("responseCode") shouldBe 409
responseLog.findLogField("clientErrorCode") shouldBe ClientErrorCode("oh.dear")
responseLog.findLogField("clientErrorMessage") shouldBe "a bid already exists"
}

@Test
Expand Down Expand Up @@ -156,9 +156,9 @@ class HttpClientTest : AbstractTest() {

val responseLog = verifyLog("http.response", Level.INFO)
responseLog.message shouldBe "Received 204 for POST /test-endpoint"
responseLog.getLogFields()["requestId"] shouldBe requestLog.getLogFields()["requestId"]
responseLog.getLogFields()["responseCode"] shouldBe 204
responseLog.getLogFields()["responseBody"] shouldBe ""
responseLog.findLogField("requestId") shouldBe requestLog.findLogField("requestId")
responseLog.findLogField("responseCode") shouldBe 204
responseLog.findLogField("responseBody") shouldBe ""
}

@Test
Expand Down
19 changes: 10 additions & 9 deletions client/src/test/kotlin/util/UpdateManagerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import kong.unirest.Unirest
import kong.unirest.UnirestException
import kong.unirest.json.JSONException
import kong.unirest.json.JSONObject
import logging.getLogFields
import logging.errorObject
import logging.findLogField
import main.kotlin.testCore.getDialogMessage
import main.kotlin.testCore.getErrorDialog
import main.kotlin.testCore.getInfoDialog
Expand Down Expand Up @@ -56,7 +57,7 @@ class UpdateManagerTest : AbstractTest() {

val log = verifyLog("updateError", Level.ERROR)
log.message shouldBe "Received non-success HTTP status: 404 - Not Found"
log.getLogFields()["responseBody"].toString() shouldContain """"message":"Not Found""""
log.findLogField("responseBody").toString() shouldContain """"message":"Not Found""""

findWindow<LoadingDialog>()!!.shouldNotBeVisible()
}
Expand All @@ -71,7 +72,7 @@ class UpdateManagerTest : AbstractTest() {
errorMessage shouldBe "Failed to check for updates (unable to connect)."

val errorLog = verifyLog("updateError", Level.ERROR)
errorLog.throwableProxy.shouldBeInstanceOf<UnirestException>()
errorLog.errorObject().shouldBeInstanceOf<UnirestException>()

findWindow<LoadingDialog>()!!.shouldNotBeVisible()
}
Expand Down Expand Up @@ -130,8 +131,8 @@ class UpdateManagerTest : AbstractTest() {
metadata shouldBe null

val log = verifyLog("parseError", Level.ERROR)
log.throwableProxy.shouldBeInstanceOf<JSONException>()
log.getLogFields()["responseBody"].toString() shouldBe json
log.errorObject().shouldBeInstanceOf<JSONException>()
log.findLogField("responseBody").toString() shouldBe json
}

@Test
Expand All @@ -141,8 +142,8 @@ class UpdateManagerTest : AbstractTest() {
metadata shouldBe null

val log = verifyLog("parseError", Level.ERROR)
log.throwableProxy.shouldBeInstanceOf<JSONException>()
log.getLogFields()["responseBody"].toString() shouldBe json
log.errorObject().shouldBeInstanceOf<JSONException>()
log.findLogField("responseBody").toString() shouldBe json
}

/** Should update? */
Expand Down Expand Up @@ -228,7 +229,7 @@ class UpdateManagerTest : AbstractTest() {

UpdateManager().prepareBatchFile()

updateFile.readText() shouldBe javaClass.getResource("/update/update.bat").readText()
updateFile.readText() shouldBe javaClass.getResource("/update/update.bat")?.readText()
updateFile.delete()
}

Expand All @@ -246,7 +247,7 @@ class UpdateManagerTest : AbstractTest() {
"Failed to launch update.bat - call the following manually to perform the update: \n\nupdate.bat foo"

val log = verifyLog("batchError", Level.ERROR)
log.throwableProxy shouldBe error
log.errorObject() shouldBe error
}

@Test
Expand Down
3 changes: 2 additions & 1 deletion client/src/test/kotlin/util/UrlUtilTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import java.io.IOException
import logging.errorObject
import org.junit.jupiter.api.Test
import testCore.AbstractTest

Expand All @@ -29,6 +30,6 @@ class UrlUtilTest : AbstractTest() {

val log = verifyLog("urlError", Level.ERROR)
log.message shouldBe "Failed to launch foo.bar"
log.throwableProxy shouldBe error
log.errorObject() shouldBe error
}
}
10 changes: 10 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

plugins {
id("Entropy.kotlin-common-conventions")
id("com.ncorti.ktfmt.gradle") version "0.15.1"
Expand All @@ -13,3 +16,10 @@ dependencies {
implementation("net.logstash.logback:logstash-logback-encoder:8.0")
testImplementation(project(":test-core"))
}

tasks.withType<Test> {
testLogging {
events = mutableSetOf(TestLogEvent.STARTED, TestLogEvent.FAILED)
exceptionFormat = TestExceptionFormat.FULL
}
}
8 changes: 7 additions & 1 deletion core/src/main/kotlin/logging/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logging

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.classic.spi.ThrowableProxy
import getPercentage
import java.lang.reflect.Field
import java.util.concurrent.ConcurrentHashMap
Expand Down Expand Up @@ -99,4 +100,9 @@ fun ILoggingEvent.getLogFields(): Map<String, Any> {
}
}

fun ILoggingEvent.level() = this.throwableProxy
fun ILoggingEvent.errorObject(): Throwable? {
val proxy = this.throwableProxy
return if (proxy is ThrowableProxy) {
proxy.throwable
} else null
}
8 changes: 4 additions & 4 deletions core/src/test/kotlin/logging/LoggerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LoggerTest : AbstractTest() {
record.level shouldBe Level.INFO
record.loggingCode shouldBe loggingCode
record.message shouldBe "A thing happened"
record.throwableProxy shouldBe null
record.errorObject() shouldBe null
record.shouldContainKeyValues("loggingCode" to "some.event")
}

Expand All @@ -35,7 +35,7 @@ class LoggerTest : AbstractTest() {
record.level shouldBe Level.INFO
record.loggingCode shouldBe loggingCode
record.message shouldBe "A thing happened"
record.throwableProxy shouldBe null
record.errorObject() shouldBe null
record.shouldContainKeyValues("Key" to "Value")
}

Expand All @@ -50,7 +50,7 @@ class LoggerTest : AbstractTest() {
record.level shouldBe Level.WARN
record.loggingCode shouldBe loggingCode
record.message shouldBe "A slightly bad thing happened"
record.throwableProxy shouldBe null
record.errorObject() shouldBe null
}

@Test
Expand All @@ -63,7 +63,7 @@ class LoggerTest : AbstractTest() {

val record = getLastLog()
record.level shouldBe Level.ERROR
record.throwableProxy.message shouldBe "Boo"
record.errorObject() shouldBe throwable
record.loggingCode shouldBe loggingCode
record.shouldContainKeyValues("other.info" to 60, KEY_EXCEPTION_MESSAGE to "Boo")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LoggerUncaughtExceptionHandlerTest : AbstractTest() {
handler.uncaughtException(Thread.currentThread(), ex)

val log = verifyLog("uncaughtException", Level.WARN)
log.throwableProxy shouldBe null
log.errorObject() shouldBe null
log.message shouldBe "Suppressing uncaught exception: $ex"
log.shouldContainKeyValues(
KEY_THREAD to Thread.currentThread().toString(),
Expand All @@ -34,7 +34,7 @@ class LoggerUncaughtExceptionHandlerTest : AbstractTest() {
handler.uncaughtException(Thread.currentThread(), ex)

val log = verifyLog("uncaughtException", Level.ERROR)
log.throwableProxy.message shouldBe null
log.errorObject() shouldBe ex
log.shouldContainKeyValues(KEY_THREAD to Thread.currentThread().toString())
log.getLogFields().shouldNotContainKey(KEY_EXCEPTION_MESSAGE)
log.message shouldContain "Uncaught exception: $ex"
Expand All @@ -49,7 +49,7 @@ class LoggerUncaughtExceptionHandlerTest : AbstractTest() {
handler.uncaughtException(t, ex)

val log = verifyLog("uncaughtException", Level.ERROR)
log.throwableProxy.message shouldBe "Argh"
log.errorObject() shouldBe ex
log.shouldContainKeyValues(KEY_THREAD to t.toString(), KEY_EXCEPTION_MESSAGE to "Argh")
log.message shouldContain "Uncaught exception: $ex"
}
Expand Down
10 changes: 10 additions & 0 deletions server/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

plugins {
id("Entropy.kotlin-common-conventions")
id("com.ncorti.ktfmt.gradle") version "0.15.1"
Expand Down Expand Up @@ -28,3 +31,10 @@ application {
// Define the main class for the application.
mainClass.set("io.ktor.server.netty.EngineMain")
}

tasks.withType<Test> {
testLogging {
events = mutableSetOf(TestLogEvent.STARTED, TestLogEvent.FAILED)
exceptionFormat = TestExceptionFormat.FULL
}
}

0 comments on commit c8e7285

Please sign in to comment.