This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial REST service with unit test #1
- Loading branch information
Showing
10 changed files
with
220 additions
and
89 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
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
132 changes: 132 additions & 0 deletions
132
src/main/scala/de/retest/guistatemachine/RestService.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,132 @@ | ||
package de.retest.guistatemachine | ||
|
||
import akka.actor.ActorSystem | ||
import akka.http.scaladsl.Http | ||
import akka.stream.ActorMaterializer | ||
import akka.Done | ||
import akka.http.scaladsl.server.Route | ||
import akka.http.scaladsl.server.Directives._ | ||
import akka.http.scaladsl.model.StatusCodes | ||
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ | ||
import spray.json.DefaultJsonProtocol._ | ||
|
||
import java.util.LinkedList | ||
|
||
// domain model | ||
// TODO Id should use Long and the REST paths as well. Use concurrent hash maps with the IDs and generate new IDs for new items. | ||
final case class Id(id: Int) | ||
final case class GuiApplication() { | ||
val testSuites = TestSuites() | ||
} | ||
final case class GuiApplications() { | ||
val applications = new LinkedList[GuiApplication] | ||
} | ||
final case class TestSuite() | ||
final case class TestSuites() { | ||
val testSuites = new LinkedList[TestSuite] | ||
} | ||
|
||
trait RestService { | ||
implicit val system: ActorSystem | ||
implicit val materializer: ActorMaterializer | ||
|
||
// database | ||
val guiApplications = GuiApplications() | ||
|
||
def addApplication(): Id = { | ||
val apps = guiApplications.applications | ||
apps.synchronized { | ||
apps.add(new GuiApplication) | ||
Id(apps.size() - 1) | ||
} | ||
} | ||
def getApplication(id: Id): Option[GuiApplication] = { | ||
val apps = guiApplications.applications | ||
apps.synchronized { | ||
val index = id.id | ||
if (index >= 0 && index < apps.size()) Some(apps.get(index)) else None | ||
} | ||
} | ||
|
||
def getTestSuites(applicationId: Id): Option[TestSuites] = { | ||
val app = getApplication(applicationId) | ||
app match { | ||
case Some(x) => Some(x.testSuites) | ||
case None => None | ||
} | ||
} | ||
|
||
def addTestSuite(applicationId: Id): Option[Id] = { | ||
val app = getApplication(applicationId) | ||
app match { | ||
case Some(x) => { | ||
val testSuites = x.testSuites.testSuites | ||
testSuites.synchronized { | ||
testSuites.add(new TestSuite) | ||
Some(Id(testSuites.size() - 1)) | ||
} | ||
} | ||
case None => None | ||
} | ||
} | ||
def getTestSuite(applicationId: Id, testSuiteId: Id): Option[TestSuite] = { | ||
val app = getApplication(applicationId) | ||
app match { | ||
case Some(x) => { | ||
val testSuites = x.testSuites.testSuites | ||
testSuites.synchronized { | ||
val index = testSuiteId.id | ||
if (index >= 0 && index < testSuites.size()) Some(testSuites.get(index)) else None | ||
} | ||
} | ||
case None => None | ||
} | ||
} | ||
|
||
// formats for unmarshalling and marshalling | ||
implicit val idFormat = jsonFormat1(Id) | ||
implicit val applicationFormat = jsonFormat0(GuiApplication) | ||
implicit val applicationsFormat = jsonFormat0(GuiApplications) | ||
implicit val testSuiteFormat = jsonFormat0(TestSuite) | ||
implicit val testSuitesFormat = jsonFormat0(TestSuites) | ||
|
||
val route: Route = | ||
get { | ||
path("applications") { | ||
complete(guiApplications) | ||
} ~ | ||
pathPrefix("application" / IntNumber) { id => | ||
val app = getApplication(Id(id)) | ||
app match { | ||
case Some(x) => complete(x) | ||
case None => complete(StatusCodes.NotFound) | ||
} | ||
} ~ | ||
pathPrefix("application" / IntNumber / "test-suites") { id => | ||
val testSuites = getTestSuites(Id(id)) | ||
testSuites match { | ||
case Some(x) => complete(x) | ||
case None => complete(StatusCodes.NotFound) | ||
} | ||
} ~ | ||
pathPrefix("application" / IntNumber / "test-suite" / IntNumber) { (appId, suiteId) => | ||
val suite = getTestSuite(Id(appId), Id(suiteId)) | ||
suite match { | ||
case Some(x) => complete(x) | ||
case None => complete(StatusCodes.NotFound) | ||
} | ||
} | ||
} ~ | ||
post { | ||
path("create-application") { | ||
val id = addApplication() | ||
complete(id) | ||
} ~ | ||
pathPrefix("application" / IntNumber / "create-test-suite") { appId => | ||
{ | ||
val id = addTestSuite(Id(appId)) | ||
complete(id) | ||
} | ||
} | ||
} | ||
} |
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
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
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
4 changes: 2 additions & 2 deletions
4
src/main/scala/de/retest/guistatemachine/model/TestCase.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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
package de.retest.guistatemachine.model | ||
|
||
class TestCase(app: GuiApplication) { | ||
class TestCase(initialState: State) { | ||
private val actions = Seq[UIAction]() | ||
|
||
def length = actions.size | ||
|
||
def isValid = true // it is valid if all GUI actions can be executed | ||
|
||
def getUiPath = new UIPath(new PathState(app.getInitialState)) // TODO generate the correct path, with the common initial state | ||
def getUiPath = new UIPath(new PathState(initialState)) // TODO generate the correct path, with the common initial state | ||
} |
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
68 changes: 68 additions & 0 deletions
68
src/test/scala/de/retest/guistatemachine/RestServiceSpec.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,68 @@ | ||
package de.retest.guistatemachine | ||
|
||
import org.scalatest.{ Matchers, WordSpec } | ||
import akka.http.scaladsl.model.StatusCodes | ||
import akka.http.scaladsl.testkit.ScalatestRouteTest | ||
import akka.http.scaladsl.server._ | ||
import Directives._ | ||
import akka.http.scaladsl.model.HttpEntity | ||
import akka.http.scaladsl.model.ContentTypes | ||
import akka.http.scaladsl.model.HttpCharset | ||
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ | ||
|
||
import akka.actor.ActorSystem | ||
import akka.http.scaladsl.Http | ||
import akka.stream.ActorMaterializer | ||
import akka.Done | ||
import akka.http.scaladsl.server.Route | ||
import akka.http.scaladsl.server.Directives._ | ||
import akka.http.scaladsl.model.StatusCodes | ||
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ | ||
import spray.json.DefaultJsonProtocol._ | ||
|
||
class RestServiceSpec extends WordSpec with Matchers with ScalatestRouteTest with RestService { | ||
|
||
lazy val sut = route | ||
|
||
"The service" should { | ||
"return an empty list for the GET request with the path /applications" in { | ||
Get("/applications") ~> sut ~> check { | ||
val r = responseAs[GuiApplications] | ||
r.applications.size shouldEqual 0 | ||
} | ||
} | ||
|
||
"allow POST for path /create-application" in { | ||
Post("/create-application") ~> sut ~> check { | ||
responseAs[Id] shouldEqual Id(0) | ||
} | ||
} | ||
|
||
"return an empty application for the GET request with the path /application/0" in { | ||
Get("/applications/0") ~> sut ~> check { | ||
val r = responseAs[GuiApplication] | ||
r.testSuites.testSuites.size shouldEqual 0 | ||
} | ||
} | ||
|
||
"return an empty list for the GET request with the path /application/0/test-suites" in { | ||
Get("/applications/0/test-suites") ~> sut ~> check { | ||
val r = responseAs[TestSuites] | ||
r.testSuites.size shouldEqual 0 | ||
} | ||
} | ||
|
||
"allow POST for path /application/0/create-test-suite" in { | ||
Post("/application/0/create-test-suite") ~> sut ~> check { | ||
responseAs[Id] shouldEqual Id(0) | ||
} | ||
} | ||
|
||
"not find any root path" in { | ||
Get() ~> Route.seal(sut) ~> check { | ||
status shouldEqual StatusCodes.NotFound | ||
responseAs[String] shouldEqual "The requested resource could not be found." | ||
} | ||
} | ||
} | ||
} |
45 changes: 0 additions & 45 deletions
45
src/test/scala/de/retest/guistatemachine/WebServerSpec.scala
This file was deleted.
Oops, something went wrong.