From 0ea5c7b63b860747222976b6c6963dde2ffbbffd Mon Sep 17 00:00:00 2001 From: Gabriele Petronella Date: Sun, 15 Mar 2020 15:03:47 +0100 Subject: [PATCH] Add smoke tests for sbt-tapiro --- .../src/sbt-test/sbt-tapiro/simple/build.sbt | 9 ++--- .../sbt-test/sbt-tapiro/simple/curlExpect.sbt | 14 ++++++++ .../simple/project/build.properties | 1 + .../sbt-tapiro/simple/project/plugins.sbt | 1 + .../simple/src/main/resources/logback.xml | 3 ++ .../simple/src/main/scala/Api.scala | 20 ----------- .../simple/src/main/scala/Boot.scala | 23 ++++++++++++ .../scala/{Models.scala => CustomModel.scala} | 0 .../src/main/scala/ExampleController.scala | 35 +++++++++++++++++++ .../src/sbt-test/sbt-tapiro/simple/test | 13 +++++-- .../sbt-test/sbt-tapiro/simple/waitIsUp.sbt | 23 ++++++++++++ 11 files changed, 113 insertions(+), 29 deletions(-) create mode 100644 tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/curlExpect.sbt create mode 100644 tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/project/build.properties create mode 100644 tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/resources/logback.xml delete mode 100644 tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/Api.scala create mode 100644 tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/Boot.scala rename tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/{Models.scala => CustomModel.scala} (100%) create mode 100644 tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/ExampleController.scala create mode 100644 tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/waitIsUp.sbt diff --git a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/build.sbt b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/build.sbt index 8b7e7792..6b2a0c85 100644 --- a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/build.sbt +++ b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/build.sbt @@ -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(""), diff --git a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/curlExpect.sbt b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/curlExpect.sbt new file mode 100644 index 00000000..a43ca5b7 --- /dev/null +++ b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/curlExpect.sbt @@ -0,0 +1,14 @@ +import scala.sys.process.Process + +val curlExpect = InputKey[Unit]("curlExpect") + +curlExpect := { + val args = Def.spaceDelimited("").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") + () +} diff --git a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/project/build.properties b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/project/build.properties new file mode 100644 index 00000000..a919a9b5 --- /dev/null +++ b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.3.8 diff --git a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/project/plugins.sbt b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/project/plugins.sbt index 1548cc89..d202bf98 100644 --- a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/project/plugins.sbt +++ b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/project/plugins.sbt @@ -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. diff --git a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/resources/logback.xml b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/resources/logback.xml new file mode 100644 index 00000000..d0cec6d3 --- /dev/null +++ b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/resources/logback.xml @@ -0,0 +1,3 @@ + + + diff --git a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/Api.scala b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/Api.scala deleted file mode 100644 index 053576e8..00000000 --- a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/Api.scala +++ /dev/null @@ -1,20 +0,0 @@ -package testo - -import scala.annotation.StaticAnnotation -class query extends StaticAnnotation -class command extends StaticAnnotation - -case class CustomError(message: String) - -trait Controller[F[_]] { - @query - def queryExample( - intParam: Int, - stringParam: String, - ): F[Either[CustomError, CustomModel]] - - @command - def commandExample( - body: CustomModel, - ): F[Either[String, String]] -} diff --git a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/Boot.scala b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/Boot.scala new file mode 100644 index 00000000..e76b6cfe --- /dev/null +++ b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/Boot.scala @@ -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) +} diff --git a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/Models.scala b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/CustomModel.scala similarity index 100% rename from tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/Models.scala rename to tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/CustomModel.scala diff --git a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/ExampleController.scala b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/ExampleController.scala new file mode 100644 index 00000000..44215767 --- /dev/null +++ b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/ExampleController.scala @@ -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))) + } +} diff --git a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/test b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/test index 1e705585..3645d5b0 100644 --- a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/test +++ b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/test @@ -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' diff --git a/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/waitIsUp.sbt b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/waitIsUp.sbt new file mode 100644 index 00000000..b316e141 --- /dev/null +++ b/tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/waitIsUp.sbt @@ -0,0 +1,23 @@ +import scala.sys.process.Process + +val waitIsUp = InputKey[Unit]("waitIsUp") + +waitIsUp := { + val args = Def.spaceDelimited("").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) + () +}