Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
Add --maxtime and increase coverage #1
Browse files Browse the repository at this point in the history
- Cover all missing cases for the resources.
- Cover the web server app.
- Cover Id.compare.
  • Loading branch information
tdauth committed Oct 26, 2018
1 parent b303394 commit 7e7da73
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ organization := "tdauth"

scalaVersion := "2.12.7"

libraryDependencies += "com.github.scopt" % "scopt_2.12" % "3.7.0"
libraryDependencies += "io.spray" % "spray-json_2.12" % "1.3.4"
libraryDependencies += "com.typesafe.akka" %% "akka-http" % "10.1.5"
libraryDependencies += "com.typesafe.akka" %% "akka-http-core" % "10.1.5"
Expand Down
5 changes: 2 additions & 3 deletions src/main/scala/de/retest/guistatemachine/RestService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,12 @@ trait RestService {
} ~ delete {
path("application" / LongNumber / "test-suite" / LongNumber) { (appId, suiteId) =>
val r = persistence.deleteTestSuite(Id(appId), Id(suiteId))
complete(StatusCodes.OK)
complete(if (r) StatusCodes.OK else StatusCodes.NotFound)
}
} ~ delete {
path("application" / LongNumber) { id =>
val r = persistence.deleteApplication(Id(id))
complete(StatusCodes.OK)
complete(if (r) StatusCodes.OK else StatusCodes.NotFound)
}
}

}
40 changes: 33 additions & 7 deletions src/main/scala/de/retest/guistatemachine/WebServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import de.retest.guistatemachine.persistence.Persistence
import scopt.OptionParser

case class Config(maxtime: Long = -1)

object WebServer extends App with RestService {
final val HOST = "localhost"
Expand All @@ -16,13 +19,36 @@ object WebServer extends App with RestService {
// needed for the future flatMap/onComplete in the end
implicit val executionContext = system.dispatcher

val persistence = new Persistence
val parser = new OptionParser[Config]("scopt") {
head("gui-state-machine-api", "1.0")

opt[Long]('t', "maxtime") action { (x, c) =>
c.copy(maxtime = x)
} text ("maxtime specifies the maximum up time for the HTTP server in ms. This option can be helpful to run tests over a specific period of time.")

help("help").text("prints this usage text")
}
// parser.parse returns Option[C]
parser.parse(args, Config()) match {
case Some(config) =>
val persistence = new Persistence

val bindingFuture = Http().bindAndHandle(getRoute(persistence), HOST, PORT)

println(s"Server online at http://${HOST}:${PORT}/")

val bindingFuture = Http().bindAndHandle(getRoute(persistence), HOST, PORT)
if (config.maxtime < 0) {
println("Press RETURN to stop...")
StdIn.readLine() // let it run until user presses return
} else {
println(s"The server will shutdown automatically after ${config.maxtime / 1000} seconds.")
Thread.sleep(config.maxtime)
}
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done

println(s"Server online at http://${HOST}:${PORT}/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
case None =>
// arguments are bad, error message will have been displayed
}
}
44 changes: 44 additions & 0 deletions src/test/scala/de/retest/guistatemachine/RestServiceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ class RestServiceSpec extends WordSpec with Matchers with ScalatestRouteTest wit
}
}

"fail for the GET request with the path /application/0" in {
Get("/application/0") ~> sut ~> check {
handled shouldEqual true
status shouldEqual StatusCodes.NotFound
}
}

"fail for the DELETE request with the path /application/0" in {
Delete("/application/0") ~> sut ~> check {
handled shouldEqual true
status shouldEqual StatusCodes.NotFound
}
}

"fail for the GET request with the path /application/0/test-suites" in {
Get("/application/0/test-suites") ~> sut ~> check {
handled shouldEqual true
status shouldEqual StatusCodes.NotFound
}
}

"allow POST for path /create-application" in {
Post("/create-application") ~> sut ~> check {
handled shouldEqual true
Expand All @@ -68,6 +89,20 @@ class RestServiceSpec extends WordSpec with Matchers with ScalatestRouteTest wit
}
}

"fail for the GET request with the path /application/0/test-suite/0" in {
Get("/application/0/test-suite/0") ~> sut ~> check {
handled shouldEqual true
status shouldEqual StatusCodes.NotFound
}
}

"fail for the DELETE request with the path /application/0/test-suite/0" in {
Delete("/application/0/test-suite/0") ~> sut ~> check {
handled shouldEqual true
status shouldEqual StatusCodes.NotFound
}
}

"allow POST for path /application/0/create-test-suite" in {
Post("/application/0/create-test-suite") ~> sut ~> check {
handled shouldEqual true
Expand Down Expand Up @@ -102,5 +137,14 @@ class RestServiceSpec extends WordSpec with Matchers with ScalatestRouteTest wit
persistence.getApplications().apps.values.size shouldEqual 0
}
}

"not handle the GET request with the path /applications/bla/hello/bla" in {
Get("/applications/bla/hello/bla") ~> sut ~> check {
handled shouldEqual false
//mediaType shouldEqual MediaTypes.`application/json`
//val r = responseAs[GuiApplications]
//r.apps.values.size shouldEqual 0
}
}
}
}
13 changes: 13 additions & 0 deletions src/test/scala/de/retest/guistatemachine/WebServerSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.retest.guistatemachine

import org.scalatest.WordSpec
import org.scalatest.Matchers

class WebServerSpec extends WordSpec with Matchers {

"The web server" should {
"start and end successfully" in {
WebServer.main(Array("--maxtime=1000"))
}
}
}
17 changes: 17 additions & 0 deletions src/test/scala/de/retest/guistatemachine/model/IdSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.retest.guistatemachine.model

import org.scalatest.WordSpec
import org.scalatest.Matchers

class IdSpec extends WordSpec with Matchers {

"Id" should {
"compare" in {
val id0 = Id(0)
val id1 = Id(1)
id0.compare(id0) shouldEqual 0
id0.compare(id1) shouldEqual -1
id1.compare(id0) shouldEqual 1
}
}
}

0 comments on commit 7e7da73

Please sign in to comment.