Skip to content

Commit

Permalink
tests for some of the generic route handling
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Sep 10, 2024
1 parent 1a3b7d5 commit 1017c7d
Show file tree
Hide file tree
Showing 24 changed files with 121 additions and 33 deletions.
4 changes: 2 additions & 2 deletions client/src/main/kotlin/screen/HelpDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class HelpDialog : JFrame(), TreeSelectionListener, WindowListener, Registry {
populateTools("")
populateMisc("")

for (i in 0 ..< tree.rowCount) {
for (i in 0..<tree.rowCount) {
tree.expandRow(i)
}

Expand All @@ -196,7 +196,7 @@ class HelpDialog : JFrame(), TreeSelectionListener, WindowListener, Registry {
treePane.setViewportView(noSearchResults)
helpPane.setViewportView(JPanel())
} else {
for (i in 0 ..< 50) {
for (i in 0..<50) {
tree.expandRow(i)
}

Expand Down
2 changes: 1 addition & 1 deletion client/src/test/kotlin/bean/HyperlinkAdaptorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import java.awt.Cursor
import java.awt.event.MouseEvent
import javax.swing.JButton
import javax.swing.JPanel
import main.kotlin.testCore.AbstractTest
import org.junit.jupiter.api.Test
import testCore.AbstractTest

private val mouseEventOverLink = makeMouseEvent(JButton())
private val mouseEventNotOverLink = makeMouseEvent(JButton())
Expand Down
2 changes: 1 addition & 1 deletion client/src/test/kotlin/bean/LinkLabelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import io.mockk.mockk
import io.mockk.verify
import java.awt.Color
import java.awt.Cursor
import main.kotlin.testCore.AbstractTest
import org.junit.jupiter.api.Test
import testCore.AbstractTest

class LinkLabelTest : AbstractTest() {
@Test
Expand Down
2 changes: 1 addition & 1 deletion client/src/test/kotlin/help/AbstractHelpPanelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package help

import io.kotest.matchers.shouldBe
import javax.swing.JTextPane
import main.kotlin.testCore.AbstractTest
import org.junit.jupiter.api.Test
import testCore.AbstractTest
import utils.getAllChildComponentsForType

abstract class AbstractHelpPanelTest<T : HelpPanel> : AbstractTest() {
Expand Down
2 changes: 1 addition & 1 deletion client/src/test/kotlin/help/HelpPanelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import io.kotest.matchers.shouldBe
import java.awt.Color
import java.awt.Font
import javax.swing.JTextPane
import main.kotlin.testCore.AbstractTest
import org.junit.jupiter.api.Test
import testCore.AbstractTest
import util.EntropyColour

class HelpPanelTest : AbstractTest() {
Expand Down
2 changes: 1 addition & 1 deletion client/src/test/kotlin/util/DialogUtilNewTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import io.kotest.matchers.shouldBe
import javax.swing.JDialog
import javax.swing.SwingUtilities
import logging.Severity
import main.kotlin.testCore.AbstractTest
import main.kotlin.testCore.getErrorDialog
import main.kotlin.testCore.getInfoDialog
import main.kotlin.testCore.getQuestionDialog
import main.kotlin.testCore.runAsync
import org.junit.jupiter.api.Test
import testCore.AbstractTest

class DialogUtilNewTest : AbstractTest() {
@Test
Expand Down
2 changes: 1 addition & 1 deletion client/src/test/kotlin/util/UpdateManagerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import kong.unirest.UnirestException
import kong.unirest.json.JSONException
import kong.unirest.json.JSONObject
import logging.Severity
import main.kotlin.testCore.AbstractTest
import main.kotlin.testCore.getDialogMessage
import main.kotlin.testCore.getErrorDialog
import main.kotlin.testCore.getInfoDialog
Expand All @@ -34,6 +33,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
import screen.LoadingDialog
import testCore.AbstractTest
import testCore.assertDoesNotExit
import testCore.assertExits

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

class UrlUtilTest : AbstractTest() {
@Test
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/kotlin/bean/FocusableWindowTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package bean
import io.kotest.matchers.shouldBe
import io.mockk.mockk
import logging.KEY_ACTIVE_WINDOW
import main.kotlin.testCore.AbstractTest
import org.junit.jupiter.api.Test
import testCore.AbstractTest
import utils.InjectedThings.logger

class FocusableWindowTest : AbstractTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package logging
import io.kotest.matchers.string.shouldContain
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import main.kotlin.testCore.AbstractTest
import main.kotlin.testCore.CURRENT_TIME_STRING
import main.kotlin.testCore.makeLogRecord
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import testCore.AbstractTest

class LogDestinationSystemOutTest : AbstractTest() {
private val originalOut = System.out
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/kotlin/logging/LoggerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.shouldBe
import io.mockk.mockk
import io.mockk.verify
import main.kotlin.testCore.AbstractTest
import main.kotlin.testCore.CURRENT_TIME
import main.kotlin.testCore.FakeLogDestination
import main.kotlin.testCore.shouldContainKeyValues
import org.junit.jupiter.api.Test
import testCore.AbstractTest

class LoggerTest : AbstractTest() {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package logging

import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import main.kotlin.testCore.AbstractTest
import main.kotlin.testCore.shouldContainKeyValues
import org.junit.jupiter.api.Test
import testCore.AbstractTest

class LoggerUncaughtExceptionHandlerTest : AbstractTest() {
@Test
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/kotlin/logging/LoggingConsoleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import io.kotest.matchers.string.shouldContain
import java.awt.Color
import javax.swing.JLabel
import javax.swing.text.StyleConstants
import main.kotlin.testCore.AbstractTest
import main.kotlin.testCore.makeLogRecord
import org.junit.jupiter.api.Test
import testCore.AbstractTest
import utils.getAllChildComponentsForType

class LoggingConsoleTest : AbstractTest() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/kotlin/logging/LoggingUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package logging

import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import main.kotlin.testCore.AbstractTest
import org.junit.jupiter.api.Test
import testCore.AbstractTest

class LoggingUtilsTest : AbstractTest() {
@Test
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/kotlin/utils/MathsUtilTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package utils

import getPercentage
import io.kotest.matchers.shouldBe
import main.kotlin.testCore.AbstractTest
import org.junit.jupiter.api.Test
import testCore.AbstractTest

class MathsUtilTest : AbstractTest() {
@Test
Expand Down
21 changes: 20 additions & 1 deletion server/src/main/kotlin/plugins/Routing.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
package plugins

import dto.ClientErrorResponse
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.logging.toLogString
import io.ktor.server.plugins.statuspages.*
import io.ktor.server.response.*
import routes.ClientException
import routes.health.HealthCheckController
import utils.InjectedThings.logger

fun Application.configureRouting() {
install(StatusPages) {
exception<ClientException> { call, cause ->
logger.info(
"clientError",
"Error handling ${call.request.toLogString()}: ${cause.message}",
"clientErrorCode" to cause.errorCode,
"clientErrorMessage" to cause.message
)
call.respond(cause.statusCode, ClientErrorResponse(cause.errorCode, cause.message))
}

exception<Throwable> { call, cause ->
call.respondText(text = "500: $cause", status = HttpStatusCode.InternalServerError)
val errorMessage = "Error handling ${call.request.toLogString()}"
logger.error("internalServerError", errorMessage, cause)
call.respond(
HttpStatusCode.InternalServerError,
ClientErrorResponse("internalServerError", errorMessage)
)
}
}

Expand Down
10 changes: 10 additions & 0 deletions server/src/main/kotlin/routes/ClientException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package routes

import io.ktor.http.HttpStatusCode

class ClientException(
val statusCode: HttpStatusCode,
val errorCode: String,
override val message: String,
override val cause: Throwable? = null
) : RuntimeException(message)
13 changes: 0 additions & 13 deletions server/src/test/kotlin/TestUtils.kt

This file was deleted.

61 changes: 61 additions & 0 deletions server/src/test/kotlin/plugins/RoutingTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package plugins

import io.kotest.matchers.shouldBe
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.Application
import io.ktor.server.routing.get
import io.ktor.server.routing.routing
import io.ktor.server.testing.testApplication
import org.junit.jupiter.api.Test
import routes.ClientException
import testCore.AbstractTest
import testCore.shouldMatchJson

class RoutingTest : AbstractTest() {
@Test
fun `Should handle client errors`() = testApplication {
application { ErrorThrowingController.installRoutes(this) }

val response = client.get("/client-error")
response.status shouldBe HttpStatusCode.Conflict
response.bodyAsText() shouldMatchJson
"""{
"errorCode": "conflictingEntity",
"errorMessage": "Entity conflicts with another"
}"""
.trimIndent()
}

@Test
fun `Should handle unexpected errors`() = testApplication {
application { ErrorThrowingController.installRoutes(this) }

val response = client.get("/internal-error")
response.status shouldBe HttpStatusCode.InternalServerError
response.bodyAsText() shouldMatchJson
"""{
"errorCode": "internalServerError",
"errorMessage": "Error handling GET - /internal-error"
}"""
.trimIndent()

errorLogged() shouldBe true
}
}

private object ErrorThrowingController {
fun installRoutes(application: Application) {
application.routing {
get("/internal-error") { throw NullPointerException("Test error") }
get("/client-error") {
throw ClientException(
HttpStatusCode.Conflict,
"conflictingEntity",
"Entity conflicts with another"
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.testing.*
import main.kotlin.testCore.AbstractTest
import org.junit.jupiter.api.Test
import shouldMatchJson
import testCore.AbstractTest
import testCore.shouldMatchJson
import util.OnlineConstants

class HealthCheckControllerTest : AbstractTest() {
Expand Down
1 change: 1 addition & 0 deletions test-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ dependencies {
implementation("io.mockk:mockk:1.13.4")
implementation("io.kotest:kotest-assertions-core:5.5.4")
implementation("com.github.alexburlton:swing-test:4.0.0")
implementation("org.skyscreamer:jsonassert:1.5.3")
}
4 changes: 3 additions & 1 deletion test-core/src/main/kotlin/testCore/AbstractTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main.kotlin.testCore
package testCore

import com.github.alyssaburlton.swingtest.purgeWindows
import io.kotest.assertions.fail
Expand All @@ -9,6 +9,8 @@ import logging.LogDestinationSystemOut
import logging.LogRecord
import logging.Logger
import logging.Severity
import main.kotlin.testCore.BeforeAllTestsExtension
import main.kotlin.testCore.FakeLogDestination
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.extension.ExtendWith
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.time.ZoneId
import logging.LoggerUncaughtExceptionHandler
import org.junit.jupiter.api.extension.BeforeAllCallback
import org.junit.jupiter.api.extension.ExtensionContext
import testCore.logger
import utils.InjectedThings

var doneOneTimeSetup = false
Expand Down
7 changes: 7 additions & 0 deletions test-core/src/main/kotlin/testCore/JsonUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package testCore

import org.skyscreamer.jsonassert.JSONAssert

infix fun String.shouldMatchJson(expected: String) {
JSONAssert.assertEquals(expected, this, false)
}

0 comments on commit 1017c7d

Please sign in to comment.