Skip to content

Commit

Permalink
chore(pact-jvm-server): Converted ListServers to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Nov 19, 2024
1 parent 22ba232 commit 5cf2e59
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}'
}
}

0 comments on commit 5cf2e59

Please sign in to comment.