Skip to content
This repository has been archived by the owner on Apr 23, 2019. It is now read-only.

Add example test code #7

Merged
merged 2 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ matrix:
- scala: 2.11.12
jdk: oraclejdk11
allow_failures:
# Fails, and we don't care right now
- jdk: oraclejdk9
- jdk: oraclejdk10
# We should allow failures here since Java 11 removed some modules including
# java.xml.bind which we are adding when running with Java 9+. For more details
# see http://openjdk.java.net/jeps/320
Expand Down
7 changes: 7 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ libraryDependencies += guice
dependencyOverrides += "com.typesafe.akka" %% "akka-http-core" % "10.1.3"
dependencyOverrides += "com.typesafe.akka" %% "akka-http" % "10.1.3"

// Test libraries
resolvers += Resolver.sonatypeRepo("releases")
libraryDependencies += "com.lightbend.akka.grpc" %% "akka-grpc-play-testkit" % "0.4.1" % Test
libraryDependencies += "com.typesafe.play" %% "play-test" % "2.7.0-M3" % Test
libraryDependencies += "com.typesafe.play" %% "play-specs2" % "2.7.0-M3" % Test
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "4.0.0-M1" % Test

// Test Database
libraryDependencies += "com.h2database" % "h2" % "1.4.197"

Expand Down
6 changes: 3 additions & 3 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resolvers += Resolver.file("local-plugins", file("/Users/ktoso/.ivy2/local"))(Resolver.ivyStylePatterns)
resolvers += Resolver.sonatypeRepo("releases")

// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.0-M2")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.0-M3")

// Play enhancer - this automatically generates getters/setters for public fields
// and rewrites accessors of these fields to use the getters/setters. Remove this
Expand All @@ -10,5 +10,5 @@ addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.0-M2")
addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.2.2")

// Akka GRPC
addSbtPlugin("com.lightbend.akka.grpc" %% "sbt-akka-grpc" % "0.2")
addSbtPlugin("com.lightbend.akka.grpc" %% "sbt-akka-grpc" % "0.4.1")

44 changes: 44 additions & 0 deletions test/test/HelloScalaTestSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package test

import akka.grpc.scalatestplus.play.ServerGrpcClient

import org.scalatest.concurrent.{ IntegrationPatience, ScalaFutures }
import org.scalatestplus.play.{ NewGuiceOneServerPerTest, PlaySpec }

import play.api.Application
import play.api.inject.bind
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSClient
import play.api.routing.Router

import example.myapp.helloworld.grpc.{ GreeterService, GreeterServiceClient, HelloRequest }
import routers.HelloWorldRouter

class HelloScalaTestSpec extends PlaySpec with ServerGrpcClient
with NewGuiceOneServerPerTest with ScalaFutures with IntegrationPatience {

override def fakeApplication(): Application =
GuiceApplicationBuilder().overrides(bind[Router].to[HelloWorldRouter]).build()

implicit def ws: WSClient = app.injector.instanceOf(classOf[WSClient])

"A Play server bound to a gRPC router" must {
"give a 404 when routing a non-gRPC request" in {
val result = wsUrl("/").get.futureValue
result.status must be(404) // Maybe should be a 426, see #396
}
"give an Ok header (and hopefully a not implemented trailer) when routing a non-existent gRPC method" in {
val result = wsUrl(s"/${GreeterService.name}/FooBar").get.futureValue
result.status must be(200) // Maybe should be a 426, see #396
// TODO: Test that trailer has a not implemented status
}
"give a 500 when routing an empty request to a gRPC method" in {
val result = wsUrl(s"/${GreeterService.name}/SayHello").get.futureValue
result.status must be(500) // Maybe should be a 426, see #396
}
"work with a gRPC client" in withGrpcClient[GreeterServiceClient] { client: GreeterServiceClient =>
val reply = client.sayHello(HelloRequest("Alice")).futureValue
reply.message must be("Hello, Alice!")
}
}
}
46 changes: 46 additions & 0 deletions test/test/HelloSpecs2Spec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package test

import akka.grpc.play.api.specs2.ServerGrpcClient

import play.api.inject.bind
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.{ WSClient, WSRequest }
import play.api.routing.Router
import play.api.test.{ ApplicationFactories, ApplicationFactory, NewForServer, PlaySpecification, RunningServer }

import example.myapp.helloworld.grpc.{ GreeterService, GreeterServiceClient, HelloRequest }
import routers.HelloWorldRouter

class HelloSpecs2Spec extends NewForServer with ServerGrpcClient with PlaySpecification with ApplicationFactories {

protected def applicationFactory: ApplicationFactory =
appFromGuice(GuiceApplicationBuilder().overrides(bind[Router].to[HelloWorldRouter]))

def wsUrl(path: String)(implicit running: RunningServer): WSRequest = {
val ws = running.app.injector.instanceOf[WSClient]
val url = running.endpoints.httpEndpoint.get.pathUrl(path)
ws.url(url)
}

"A Play server bound to a gRPC router" should {
"give a 404 when routing a non-gRPC request" >> { implicit rs: RunningServer =>
val result = await(wsUrl("/").get)
result.status must ===(404)
}
"give an Ok header when routing a non-existent gRPC method" >> { implicit rs: RunningServer =>
val result = await(wsUrl(s"/${GreeterService.name}/FooBar").get)
result.status must ===(200)
}
"give a 500 when routing an empty request to a gRPC method" >> { implicit rs: RunningServer =>
val result = await(wsUrl(s"/${GreeterService.name}/SayHello").get)
result.status must ===(500)
}
"work with a gRPC client" >> { implicit rs: RunningServer =>
withGrpcClient[GreeterServiceClient] { client: GreeterServiceClient =>
val reply = await(client.sayHello(HelloRequest("Alice")))
reply.message must ===("Hello, Alice!")
}
}
}

}