From 5cf2e596802e7c7cb37d402e15502e352ca94bde Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Tue, 19 Nov 2024 16:43:54 +1100 Subject: [PATCH] chore(pact-jvm-server): Converted ListServers to kotlin --- .../au/com/dius/pact/server/ListServers.kt | 16 ++++++++++++++++ .../au/com/dius/pact/server/RequestRouter.scala | 2 +- .../scala/au/com/dius/pact/server/Server.scala | 13 ------------- .../com/dius/pact/server/ListServersSpec.groovy | 2 +- 4 files changed, 18 insertions(+), 15 deletions(-) create mode 100644 pact-jvm-server/src/main/kotlin/au/com/dius/pact/server/ListServers.kt diff --git a/pact-jvm-server/src/main/kotlin/au/com/dius/pact/server/ListServers.kt b/pact-jvm-server/src/main/kotlin/au/com/dius/pact/server/ListServers.kt new file mode 100644 index 000000000..e5f14f1c3 --- /dev/null +++ b/pact-jvm-server/src/main/kotlin/au/com/dius/pact/server/ListServers.kt @@ -0,0 +1,16 @@ +package au.com.dius.pact.server + +import au.com.dius.pact.core.model.OptionalBody +import au.com.dius.pact.core.model.Response + +object ListServers { + private val portRegex = Regex("\\d+") + + @JvmStatic + fun apply(oldState: ServerState): Result { + val ports = oldState.state.keys.filter { it.matches(portRegex) }.joinToString(", ") + val paths = oldState.state.keys.filter { !it.matches(portRegex) }.joinToString(", ") { "\"$it\"" } + val body = OptionalBody.body(("{\"ports\": [$ports], \"paths\": [$paths]}").toByteArray()) + return Result(Response(200, mapOf("Content-Type" to listOf("application/json")).toMutableMap(), body), oldState) + } +} diff --git a/pact-jvm-server/src/main/scala/au/com/dius/pact/server/RequestRouter.scala b/pact-jvm-server/src/main/scala/au/com/dius/pact/server/RequestRouter.scala index 0c7d6a363..bfe091b33 100644 --- a/pact-jvm-server/src/main/scala/au/com/dius/pact/server/RequestRouter.scala +++ b/pact-jvm-server/src/main/scala/au/com/dius/pact/server/RequestRouter.scala @@ -34,7 +34,7 @@ object RequestRouter extends StrictLogging { case "create" => Create.apply(request, oldState, config) case "complete" => Complete.apply(request, oldState) case "publish" => Publish(request, oldState, config) - case "" => ListServers(oldState) + case "" => ListServers.apply(oldState) case _ => new Result(pactDispatch(request, oldState), oldState) } } diff --git a/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Server.scala b/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Server.scala index 585678f7e..a982ebce2 100644 --- a/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Server.scala +++ b/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Server.scala @@ -1,21 +1,8 @@ package au.com.dius.pact.server -import au.com.dius.pact.core.model.{OptionalBody, Response} import ch.qos.logback.classic.Level import org.slf4j.{Logger, LoggerFactory} -import scala.collection.JavaConverters._ - -object ListServers { - - def apply(oldState: ServerState): Result = { - val ports = oldState.getState.keySet.asScala.filter(p => p.matches("\\d+")).mkString(", ") - val paths = oldState.getState.keySet.asScala.filter(p => !p.matches("\\d+")).map("\"" + _ + "\"").mkString(", ") - val body = OptionalBody.body(("{\"ports\": [" + ports + "], \"paths\": [" + paths + "]}").getBytes) - new Result(new Response(200, Map("Content-Type" -> List("application/json").asJava).asJava, body), oldState) - } -} - object Server extends App { val parser = new scopt.OptionParser[Config]("pact-jvm-server") { diff --git a/pact-jvm-server/src/test/groovy/au/com/dius/pact/server/ListServersSpec.groovy b/pact-jvm-server/src/test/groovy/au/com/dius/pact/server/ListServersSpec.groovy index 9504430c1..e940d5726 100644 --- a/pact-jvm-server/src/test/groovy/au/com/dius/pact/server/ListServersSpec.groovy +++ b/pact-jvm-server/src/test/groovy/au/com/dius/pact/server/ListServersSpec.groovy @@ -58,6 +58,6 @@ class ListServersSpec extends Specification { then: result.response.status == 200 - result.response.body.valueAsString() == '{"ports": [8765, 1234], "paths": ["/other-path", "/path"]}' + result.response.body.valueAsString() == '{"ports": [1234, 8765], "paths": ["/path", "/other-path"]}' } }