-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from buildo/tests
Add smoke tests for sbt-tapiro
- Loading branch information
Showing
11 changed files
with
113 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/curlExpect.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
() | ||
} |
1 change: 1 addition & 0 deletions
1
tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/project/build.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=1.3.8 |
1 change: 1 addition & 0 deletions
1
tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/project/plugins.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/resources/logback.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<configuration> | ||
<root level="info" /> | ||
</configuration> |
20 changes: 0 additions & 20 deletions
20
tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/Api.scala
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/Boot.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
File renamed without changes.
35 changes: 35 additions & 0 deletions
35
tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/scala/ExampleController.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/waitIsUp.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
() | ||
} |