Skip to content

Commit

Permalink
Merge pull request #200 from buildo/tests
Browse files Browse the repository at this point in the history
Add smoke tests for sbt-tapiro
  • Loading branch information
gabro authored Mar 19, 2020
2 parents 372ebfb + 0ea5c7b commit 907381f
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 29 deletions.
9 changes: 2 additions & 7 deletions tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@ lazy val root = (project in file("."))
.enablePlugins(SbtTapiro)
.settings(
version := "0.1",
scalaVersion := "2.12.8",
scalaVersion := "2.12.10",
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-effect" % "1.2.0",
"com.softwaremill.sttp.tapir" %% "tapir-json-circe" % "0.12.15",
"com.softwaremill.sttp.tapir" %% "tapir-http4s-server" % "0.12.15",
"com.softwaremill.sttp.tapir" %% "tapir-openapi-docs" % "0.12.15",
"com.softwaremill.sttp.tapir" %% "tapir-openapi-circe-yaml" % "0.12.15",
"com.softwaremill.sttp.tapir" %% "tapir-sttp-client" % "0.12.15",
"com.softwaremill.sttp.tapir" %% "tapir-core" % "0.12.15",
"org.http4s" %% "http4s-dsl" % http4sVersion,
"org.http4s" %% "http4s-blaze-server" % http4sVersion,
"org.http4s" %% "http4s-blaze-client" % http4sVersion,
"org.http4s" %% "http4s-circe" % http4sVersion,
"ch.qos.logback" % "logback-classic" % "1.2.3",
) ++ Seq(
"io.circe" %% "circe-core",
"io.circe" %% "circe-generic",
"io.circe" %% "circe-parser",
).map(_ % circeVersion),
tapiro / tapiroModelsPaths := List(""),
tapiro / tapiroRoutesPaths := List(""),
Expand Down
14 changes: 14 additions & 0 deletions tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/curlExpect.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import scala.sys.process.Process

val curlExpect = InputKey[Unit]("curlExpect")

curlExpect := {
val args = Def.spaceDelimited("<arg>").parsed
val curlArgs = args.dropRight(1)
println("Executing: curl " + curlArgs.mkString(" "))
val process = Process("curl", curlArgs)
val expected = args.last
val out = (process !!)
if (out.trim != expected) sys.error(s"Expected $expected, but got $out")
()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.3.8
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
addSbtPlugin("io.buildo" %% "sbt-buildo" % "0.11.5")
sys.props.get("plugin.version") match {
case Some(v) => addSbtPlugin("io.buildo" % "sbt-tapiro" % v)
case _ => sys.error("""|The system property 'plugin.version' is not defined.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<configuration>
<root level="info" />
</configuration>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package testo

import cats.implicits._
import cats.effect._
import org.http4s.server.blaze._
import org.http4s.implicits._
import endpoints.ExampleControllerHttp4sEndpoints
import sttp.tapir.json.circe._
import io.circe.generic.auto._

object Boot extends IOApp {
val exampleController = ExampleController.create[IO]
val routes = ExampleControllerHttp4sEndpoints.routes(exampleController)

override def run(args: List[String]): IO[ExitCode] =
BlazeServerBuilder[IO]
.bindHttp(8080, "localhost")
.withHttpApp(routes.orNotFound)
.serve
.compile
.drain
.as(ExitCode.Success)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package testo

import cats._

import scala.annotation.StaticAnnotation
class query extends StaticAnnotation
class command extends StaticAnnotation

case class CustomError(message: String)

trait ExampleController[F[_], T] {
@query
def queryExample(
intParam: Int,
stringParam: String,
): F[Either[CustomError, CustomModel]]

@command
def commandExample(
body: CustomModel,
): F[Either[String, String]]
}

object ExampleController {
def create[F[_]: Applicative] = new ExampleController[F, String] {
override def commandExample(body: CustomModel): F[Either[String, String]] =
Applicative[F].pure(Right(body.name))

override def queryExample(
intParam: Int,
stringParam: String,
): F[Either[CustomError, CustomModel]] =
Applicative[F].pure(Right(CustomModel(stringParam, intParam.toDouble)))
}
}
13 changes: 11 additions & 2 deletions tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/test
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# check if endpoints gets created
> tapiro
$ exists "src/main/scala/endpoints/ControllerEndpoints.scala"
$ exists "src/main/scala/endpoints/ControllerHttp4sEndpoints.scala"
$ exists "src/main/scala/endpoints/ExampleControllerEndpoints.scala"
$ exists "src/main/scala/endpoints/ExampleControllerHttp4sEndpoints.scala"

# compile and start server
> reStart
> waitIsUp localhost:8080

# check that the endpoints respond as expected
# NOTE(gabro): the single quotes surrounding the commands are a workaround for https://github.com/sbt/sbt/issues/4870
> 'curlExpect -s -X GET localhost:8080/ExampleController/queryExample?intParam=1&stringParam=abc {"name":"abc","double":1.0}'
> 'curlExpect -s -X POST localhost:8080/ExampleController/commandExample -d "{\"name\":\"abc\",\"double\":1.0}" abc'
23 changes: 23 additions & 0 deletions tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/waitIsUp.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import scala.sys.process.Process

val waitIsUp = InputKey[Unit]("waitIsUp")

waitIsUp := {
val args = Def.spaceDelimited("<arg>").parsed
val site = args(0)
def checkIsUp(times: Int): Unit = {
if (times <= 0) {
sys.error(s"$site didn't come up in time")
}
println(s"Checking if $site is up...")
val process = Process("curl", List("-s", "-o", "/dev/null", site))
val isUp = process.! == 0
if (!isUp) {
Thread.sleep(500)
checkIsUp(times - 1)
}
()
}
checkIsUp(times = 15)
()
}

0 comments on commit 907381f

Please sign in to comment.