Skip to content

Commit 907381f

Browse files
authored
Merge pull request #200 from buildo/tests
Add smoke tests for sbt-tapiro
2 parents 372ebfb + 0ea5c7b commit 907381f

File tree

11 files changed

+113
-29
lines changed

11 files changed

+113
-29
lines changed

tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/build.sbt

+2-7
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,18 @@ lazy val root = (project in file("."))
88
.enablePlugins(SbtTapiro)
99
.settings(
1010
version := "0.1",
11-
scalaVersion := "2.12.8",
11+
scalaVersion := "2.12.10",
1212
libraryDependencies ++= Seq(
1313
"org.typelevel" %% "cats-effect" % "1.2.0",
1414
"com.softwaremill.sttp.tapir" %% "tapir-json-circe" % "0.12.15",
1515
"com.softwaremill.sttp.tapir" %% "tapir-http4s-server" % "0.12.15",
16-
"com.softwaremill.sttp.tapir" %% "tapir-openapi-docs" % "0.12.15",
17-
"com.softwaremill.sttp.tapir" %% "tapir-openapi-circe-yaml" % "0.12.15",
18-
"com.softwaremill.sttp.tapir" %% "tapir-sttp-client" % "0.12.15",
1916
"com.softwaremill.sttp.tapir" %% "tapir-core" % "0.12.15",
20-
"org.http4s" %% "http4s-dsl" % http4sVersion,
2117
"org.http4s" %% "http4s-blaze-server" % http4sVersion,
22-
"org.http4s" %% "http4s-blaze-client" % http4sVersion,
2318
"org.http4s" %% "http4s-circe" % http4sVersion,
19+
"ch.qos.logback" % "logback-classic" % "1.2.3",
2420
) ++ Seq(
2521
"io.circe" %% "circe-core",
2622
"io.circe" %% "circe-generic",
27-
"io.circe" %% "circe-parser",
2823
).map(_ % circeVersion),
2924
tapiro / tapiroModelsPaths := List(""),
3025
tapiro / tapiroRoutesPaths := List(""),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.sys.process.Process
2+
3+
val curlExpect = InputKey[Unit]("curlExpect")
4+
5+
curlExpect := {
6+
val args = Def.spaceDelimited("<arg>").parsed
7+
val curlArgs = args.dropRight(1)
8+
println("Executing: curl " + curlArgs.mkString(" "))
9+
val process = Process("curl", curlArgs)
10+
val expected = args.last
11+
val out = (process !!)
12+
if (out.trim != expected) sys.error(s"Expected $expected, but got $out")
13+
()
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.3.8

tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/project/plugins.sbt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
addSbtPlugin("io.buildo" %% "sbt-buildo" % "0.11.5")
12
sys.props.get("plugin.version") match {
23
case Some(v) => addSbtPlugin("io.buildo" % "sbt-tapiro" % v)
34
case _ => sys.error("""|The system property 'plugin.version' is not defined.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<configuration>
2+
<root level="info" />
3+
</configuration>

tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/Api.scala

-20
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package testo
2+
3+
import cats.implicits._
4+
import cats.effect._
5+
import org.http4s.server.blaze._
6+
import org.http4s.implicits._
7+
import endpoints.ExampleControllerHttp4sEndpoints
8+
import sttp.tapir.json.circe._
9+
import io.circe.generic.auto._
10+
11+
object Boot extends IOApp {
12+
val exampleController = ExampleController.create[IO]
13+
val routes = ExampleControllerHttp4sEndpoints.routes(exampleController)
14+
15+
override def run(args: List[String]): IO[ExitCode] =
16+
BlazeServerBuilder[IO]
17+
.bindHttp(8080, "localhost")
18+
.withHttpApp(routes.orNotFound)
19+
.serve
20+
.compile
21+
.drain
22+
.as(ExitCode.Success)
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package testo
2+
3+
import cats._
4+
5+
import scala.annotation.StaticAnnotation
6+
class query extends StaticAnnotation
7+
class command extends StaticAnnotation
8+
9+
case class CustomError(message: String)
10+
11+
trait ExampleController[F[_], T] {
12+
@query
13+
def queryExample(
14+
intParam: Int,
15+
stringParam: String,
16+
): F[Either[CustomError, CustomModel]]
17+
18+
@command
19+
def commandExample(
20+
body: CustomModel,
21+
): F[Either[String, String]]
22+
}
23+
24+
object ExampleController {
25+
def create[F[_]: Applicative] = new ExampleController[F, String] {
26+
override def commandExample(body: CustomModel): F[Either[String, String]] =
27+
Applicative[F].pure(Right(body.name))
28+
29+
override def queryExample(
30+
intParam: Int,
31+
stringParam: String,
32+
): F[Either[CustomError, CustomModel]] =
33+
Applicative[F].pure(Right(CustomModel(stringParam, intParam.toDouble)))
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
# check if endpoints gets created
22
> tapiro
3-
$ exists "src/main/scala/endpoints/ControllerEndpoints.scala"
4-
$ exists "src/main/scala/endpoints/ControllerHttp4sEndpoints.scala"
3+
$ exists "src/main/scala/endpoints/ExampleControllerEndpoints.scala"
4+
$ exists "src/main/scala/endpoints/ExampleControllerHttp4sEndpoints.scala"
5+
6+
# compile and start server
7+
> reStart
8+
> waitIsUp localhost:8080
9+
10+
# check that the endpoints respond as expected
11+
# NOTE(gabro): the single quotes surrounding the commands are a workaround for https://github.com/sbt/sbt/issues/4870
12+
> 'curlExpect -s -X GET localhost:8080/ExampleController/queryExample?intParam=1&stringParam=abc {"name":"abc","double":1.0}'
13+
> 'curlExpect -s -X POST localhost:8080/ExampleController/commandExample -d "{\"name\":\"abc\",\"double\":1.0}" abc'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import scala.sys.process.Process
2+
3+
val waitIsUp = InputKey[Unit]("waitIsUp")
4+
5+
waitIsUp := {
6+
val args = Def.spaceDelimited("<arg>").parsed
7+
val site = args(0)
8+
def checkIsUp(times: Int): Unit = {
9+
if (times <= 0) {
10+
sys.error(s"$site didn't come up in time")
11+
}
12+
println(s"Checking if $site is up...")
13+
val process = Process("curl", List("-s", "-o", "/dev/null", site))
14+
val isUp = process.! == 0
15+
if (!isUp) {
16+
Thread.sleep(500)
17+
checkIsUp(times - 1)
18+
}
19+
()
20+
}
21+
checkIsUp(times = 15)
22+
()
23+
}

0 commit comments

Comments
 (0)