Skip to content

Commit

Permalink
javalinApp: create each time a new server
Browse files Browse the repository at this point in the history
  • Loading branch information
handymenny committed Feb 7, 2024
1 parent dff8970 commit 8e504a6
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class JavalinApp {
private val maxOutputCache = Config.getOrDefault("cache", "0").toInt().takeIf { it >= 0 }
private val store = Config["store"]
private var index = LibraryIndex(mutableListOf())
var app: Javalin

init {
if (store != null) {
Expand All @@ -55,11 +54,10 @@ class JavalinApp {
}
}
}
app = createJavalin()
}

private fun createJavalin(): Javalin {
val app =
fun newServer(): Javalin {
val server =
Javalin.create { config ->
config.compression.gzipOnly(4)
config.http.prefer405over404 = true
Expand All @@ -83,7 +81,7 @@ class JavalinApp {
}
}

app.exception(Exception::class.java) { e, ctx ->
server.exception(Exception::class.java) { e, ctx ->
e.printStackTrace()
if (e is IllegalArgumentException || e is NullPointerException) {
ctx.badRequest()
Expand All @@ -92,15 +90,15 @@ class JavalinApp {
}
}

app.error(HttpStatus.NOT_FOUND) { ctx ->
server.error(HttpStatus.NOT_FOUND) { ctx ->
if (html404 != null) {
ctx.contentType(ContentType.HTML)
ctx.result(html404)
}
}

app.routes(buildRoutes(store, index, compression))
return app
server.routes(buildRoutes(store, index, compression))
return server
}

private suspend fun reparseLibrary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object ServerMode {
* used by the server (useful for input 0)
*/
fun run(port: Int): Int {
val app = JavalinApp().app
val app = JavalinApp().newServer()
app.start(port)
Runtime.getRuntime().addShutdownHook(Thread { app.stop() })
return app.port()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ internal class ServerModeCompressionTest {
json: Boolean = true,
gzip: Boolean = false
) =
JavalinTest.test(JavalinApp().app) { _, client ->
JavalinTest.test(JavalinApp().newServer()) { _, client ->
val response = client.get(url)
Assertions.assertEquals(HttpStatus.OK.code, response.code)
val actualText = response.body?.string() ?: ""
Expand All @@ -140,7 +140,7 @@ internal class ServerModeCompressionTest {
}

private fun storeTest(url: String, request: JsonObject, oraclePath: String) =
JavalinTest.test(JavalinApp().app) { _, client ->
JavalinTest.test(JavalinApp().newServer()) { _, client ->
val response = client.post(url, request)
Assertions.assertEquals(HttpStatus.OK.code, response.code)
capabilitiesAssertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.junit.jupiter.api.Test

internal class ServerModeCsvTest {
private val path = "src/test/resources/server"
private val app = JavalinApp().app
private val app = JavalinApp().newServer()
private val endpoint = arrayOf("/csv/", "/csv").random()

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.smartphonecombo.uecapabilityparser.server

import io.javalin.Javalin
import io.javalin.http.HttpStatus
import io.javalin.testtools.JavalinTest
import it.smartphonecombo.uecapabilityparser.extension.custom
Expand Down Expand Up @@ -41,7 +40,6 @@ import org.junit.jupiter.api.TestMethodOrder
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
internal class ServerModeFilterTest {
private val endpoint = arrayOf("/store/list/filtered/", "/store/list/filtered").random()
private var app: Javalin = JavalinApp().app

@Test
fun emptyResult() {
Expand Down Expand Up @@ -303,7 +301,7 @@ internal class ServerModeFilterTest {
}

private fun javalinTest(request: JsonObject, oraclePath: String) {
JavalinTest.test(app) { _, client ->
JavalinTest.test(app.newServer()) { _, client ->
val response = client.post(endpoint, request)
Assertions.assertEquals(HttpStatus.OK.code, response.code)

Expand All @@ -315,19 +313,22 @@ internal class ServerModeFilterTest {
}

private fun javalinErrorTest(request: JsonObject, errorCode: Int) {
JavalinTest.test(app) { _, client ->
JavalinTest.test(app.newServer()) { _, client ->
val response = client.post(endpoint, request)
Assertions.assertEquals(errorCode, response.code)
}
}

companion object {
private val path = "src/test/resources/server/"
private lateinit var app: JavalinApp

@JvmStatic
@BeforeAll
fun setup() {
Config["store"] = "$path/inputForQuery/"
Config["cache"] = "100"
app = JavalinApp()
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.junit.jupiter.api.Test
internal class ServerModeMultiPartParseTest {
private val inputPath = "src/test/resources/cli/input/"
private val oraclePath = "src/test/resources/server/oracleForMultiParse/"
private val app = JavalinApp().app
private val app = JavalinApp().newServer()
private val endpoint = arrayOf("/parse/multiPart", "/parse/multiPart/").random()

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ internal class ServerModeMultiStoreTest {
}

private fun getTest(url: String, oraclePath: String, json: Boolean = true) =
JavalinTest.test(JavalinApp().app) { _, client ->
JavalinTest.test(JavalinApp().newServer()) { _, client ->
val response = client.get(url)
Assertions.assertEquals(HttpStatus.OK.code, response.code)
val actualText = response.body?.string() ?: ""
Expand All @@ -262,7 +262,7 @@ internal class ServerModeMultiStoreTest {
files: List<String>,
oraclePath: String
) =
JavalinTest.test(JavalinApp().app) { _, client ->
JavalinTest.test(JavalinApp().newServer()) { _, client ->
val response =
client.request(
UtilityForTests.multiPartRequest(client.origin + url, request, files)
Expand Down Expand Up @@ -328,7 +328,7 @@ internal class ServerModeMultiStoreTest {
}

private fun getTestError(url: String, statusCode: Int) =
JavalinTest.test(JavalinApp().app) { _, client ->
JavalinTest.test(JavalinApp().newServer()) { _, client ->
val response = client.get(url)
Assertions.assertEquals(statusCode, response.code)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ internal class ServerModeOthersTest {
}

private fun getTest(url: String, oracle: JsonElement) {
JavalinTest.test(JavalinApp().app) { _, client ->
JavalinTest.test(JavalinApp().newServer()) { _, client ->
val response = client.get(url)
Assertions.assertEquals(HttpStatus.OK.code, response.code)
val actualText = response.body?.string() ?: ""
Expand All @@ -128,7 +128,7 @@ internal class ServerModeOthersTest {
}

private fun getTestText(url: String, oracle: String) {
JavalinTest.test(JavalinApp().app) { _, client ->
JavalinTest.test(JavalinApp().newServer()) { _, client ->
val response = client.get(url)
Assertions.assertEquals(HttpStatus.OK.code, response.code)
val actualText = response.body?.string() ?: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import org.junit.jupiter.api.Test
// This uses the same inputs and oracles of CliJsonOutputTest
internal class ServerModeParseTest {
private val path = "src/test/resources/cli"
private val app = JavalinApp().app
private val app = JavalinApp().newServer()
private val base64 = Base64.getEncoder()
private val endpoint = arrayOf("/parse/", "/parse").random()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ internal class ServerModeStoreTest {
}

private fun getTest(url: String, oraclePath: String, json: Boolean = true) =
JavalinTest.test(JavalinApp().app) { _, client ->
JavalinTest.test(JavalinApp().newServer()) { _, client ->
val response = client.get(url)
Assertions.assertEquals(HttpStatus.OK.code, response.code)
val actualText = response.body?.string() ?: ""
Expand All @@ -203,13 +203,13 @@ internal class ServerModeStoreTest {
}

private fun getTestError(url: String, statusCode: Int) =
JavalinTest.test(JavalinApp().app) { _, client ->
JavalinTest.test(JavalinApp().newServer()) { _, client ->
val response = client.get(url)
Assertions.assertEquals(statusCode, response.code)
}

private fun storeTest(url: String, request: JsonObject, oraclePath: String) =
JavalinTest.test(JavalinApp().app) { _, client ->
JavalinTest.test(JavalinApp().newServer()) { _, client ->
val response = client.post(url, request)
Assertions.assertEquals(HttpStatus.OK.code, response.code)
capabilitiesAssertEquals(File(oraclePath).readText(), response.body?.string() ?: "")
Expand Down

0 comments on commit 8e504a6

Please sign in to comment.