From 595d199f5e41c8e48131cec98b23452bc7ed6ef1 Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Wed, 1 Nov 2017 09:24:16 -0700 Subject: Add DriverRouteTest --- .../scala/xyz/driver/core/rest/DriverRoute.scala | 3 + .../driver/core/rest/errors/serviceException.scala | 4 +- src/test/scala/xyz/driver/core/RestTest.scala | 16 ---- .../xyz/driver/core/rest/DriverRouteTest.scala | 89 ++++++++++++++++++++++ src/test/scala/xyz/driver/core/rest/RestTest.scala | 15 ++++ 5 files changed, 109 insertions(+), 18 deletions(-) delete mode 100644 src/test/scala/xyz/driver/core/RestTest.scala create mode 100644 src/test/scala/xyz/driver/core/rest/DriverRouteTest.scala create mode 100644 src/test/scala/xyz/driver/core/rest/RestTest.scala (limited to 'src') diff --git a/src/main/scala/xyz/driver/core/rest/DriverRoute.scala b/src/main/scala/xyz/driver/core/rest/DriverRoute.scala index 9af6657..eb9a31a 100644 --- a/src/main/scala/xyz/driver/core/rest/DriverRoute.scala +++ b/src/main/scala/xyz/driver/core/rest/DriverRoute.scala @@ -82,6 +82,9 @@ trait DriverRoute { case e: ResourceNotFoundException => log.info("Resource not found error", e) StatusCodes.NotFound + case e: ExternalServiceException => + log.error("Error while calling another service", e) + StatusCodes.InternalServerError case e: ExternalServiceTimeoutException => log.error("Service timeout error", e) StatusCodes.GatewayTimeout diff --git a/src/main/scala/xyz/driver/core/rest/errors/serviceException.scala b/src/main/scala/xyz/driver/core/rest/errors/serviceException.scala index ca1f759..e91a3c2 100644 --- a/src/main/scala/xyz/driver/core/rest/errors/serviceException.scala +++ b/src/main/scala/xyz/driver/core/rest/errors/serviceException.scala @@ -1,6 +1,6 @@ package xyz.driver.core.rest.errors -abstract class ServiceException extends Exception { +sealed abstract class ServiceException extends Exception { def message: String } @@ -13,7 +13,7 @@ final case class ResourceNotFoundException(override val message: String = "Resou extends ServiceException final case class ExternalServiceException(serviceName: String, serviceMessage: String) extends ServiceException { - override def message = s"Error while calling another service: $serviceMessage" + override def message = s"Error while calling '$serviceName': $serviceMessage" } final case class ExternalServiceTimeoutException(serviceName: String) extends ServiceException { diff --git a/src/test/scala/xyz/driver/core/RestTest.scala b/src/test/scala/xyz/driver/core/RestTest.scala deleted file mode 100644 index efb9d07..0000000 --- a/src/test/scala/xyz/driver/core/RestTest.scala +++ /dev/null @@ -1,16 +0,0 @@ -package xyz.driver.core.rest - -import org.scalatest.{FlatSpec, Matchers} - -import akka.util.ByteString - -class RestTest extends FlatSpec with Matchers { - "`escapeScriptTags` function" should "escap script tags properly" in { - val dirtyString = " akkaComplete} +import akka.http.scaladsl.server.Route +import akka.http.scaladsl.testkit.ScalatestRouteTest +import com.typesafe.scalalogging.Logger +import org.scalatest.{AsyncFlatSpec, Matchers} +import xyz.driver.core.logging.NoLogger +import xyz.driver.core.rest.errors._ + +import scala.concurrent.Future + +class DriverRouteTest extends AsyncFlatSpec with ScalatestRouteTest with Matchers { + class TestRoute(override val route: Route) extends DriverRoute { + override def log: Logger = NoLogger + } + + "DriverRoute" should "respond with 200 OK for a basic route" in { + val route = new TestRoute(akkaComplete(StatusCodes.OK)) + + Get("/api/v1/foo/bar") ~> route.routeWithDefaults ~> check { + handled shouldBe true + status shouldBe StatusCodes.OK + } + } + + it should "respond with a 400 for an InvalidInputException" in { + val route = new TestRoute(akkaComplete(Future.failed[String](InvalidInputException()))) + + Post("/api/v1/foo/bar") ~> route.routeWithDefaults ~> check { + handled shouldBe true + status shouldBe StatusCodes.BadRequest + responseAs[String] shouldBe "Invalid input" + } + } + + it should "respond with a 400 for InvalidActionException" in { + val route = new TestRoute(akkaComplete(Future.failed[String](InvalidActionException()))) + + Post("/api/v1/foo/bar") ~> route.routeWithDefaults ~> check { + handled shouldBe true + status shouldBe StatusCodes.Forbidden + responseAs[String] shouldBe "This action is not allowed" + } + } + + it should "respond with a 404 for ResourceNotFoundException" in { + val route = new TestRoute(akkaComplete(Future.failed[String](ResourceNotFoundException()))) + + Post("/api/v1/foo/bar") ~> route.routeWithDefaults ~> check { + handled shouldBe true + status shouldBe StatusCodes.NotFound + responseAs[String] shouldBe "Resource not found" + } + } + + it should "respond with a 500 for ExternalServiceException" in { + val error = ExternalServiceException("GET /api/v1/users/", "Permission denied") + val route = new TestRoute(akkaComplete(Future.failed[String](error))) + + Post("/api/v1/foo/bar") ~> route.routeWithDefaults ~> check { + handled shouldBe true + status shouldBe StatusCodes.InternalServerError + responseAs[String] shouldBe "Error while calling 'GET /api/v1/users/': Permission denied" + } + } + + it should "respond with a 503 for ExternalServiceTimeoutException" in { + val error = ExternalServiceTimeoutException("GET /api/v1/users/") + val route = new TestRoute(akkaComplete(Future.failed[String](error))) + + Post("/api/v1/foo/bar") ~> route.routeWithDefaults ~> check { + handled shouldBe true + status shouldBe StatusCodes.GatewayTimeout + responseAs[String] shouldBe "GET /api/v1/users/ took too long to respond" + } + } + + it should "respond with a 500 for DatabaseException" in { + val route = new TestRoute(akkaComplete(Future.failed[String](DatabaseException()))) + + Post("/api/v1/foo/bar") ~> route.routeWithDefaults ~> check { + handled shouldBe true + status shouldBe StatusCodes.InternalServerError + responseAs[String] shouldBe "Database access error" + } + } +} diff --git a/src/test/scala/xyz/driver/core/rest/RestTest.scala b/src/test/scala/xyz/driver/core/rest/RestTest.scala new file mode 100644 index 0000000..2c3fb7f --- /dev/null +++ b/src/test/scala/xyz/driver/core/rest/RestTest.scala @@ -0,0 +1,15 @@ +package xyz.driver.core.rest + +import akka.util.ByteString +import org.scalatest.{FlatSpec, Matchers} + +class RestTest extends FlatSpec with Matchers { + "`escapeScriptTags` function" should "escap script tags properly" in { + val dirtyString = "