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

Commit

Permalink
Simplify case classes which use a Map by inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
tdauth committed Oct 25, 2018
1 parent 98d5d4a commit b400c42
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package de.retest.guistatemachine.model

final case class GuiApplications(applications: Map[GuiApplication]) {
// TODO Generate IDs in a better way. Maybe random numbers until one unused element is found?
def generateId: Id = this.synchronized { if (applications.values.isEmpty) Id(0) else Id(applications.values.keySet.max.id + 1) }
}
final case class GuiApplications(var values: scala.collection.immutable.Map[Id, GuiApplication]) extends Map[GuiApplication](values)
9 changes: 8 additions & 1 deletion src/main/scala/de/retest/guistatemachine/model/Map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ package de.retest.guistatemachine.model
* [[JsonFormatForIdMap]] implements marshalling and unmarshalling for JSON for this type.
* We cannot extend immutable maps in Scala, so we have to keep it as field.
*/
case class Map[T](var values: scala.collection.immutable.Map[Id, T]) {
class Map[T](var values: scala.collection.immutable.Map[Id, T]) {
/**
* Generates a new ID based on the existing entries.
* TODO Generate IDs in a better way. Maybe random numbers until one unused element is found?
*/
def generateId: Id = this.synchronized { if (values.isEmpty) Id(0) else Id(values.keySet.max.id + 1) }

def setValues(v: scala.collection.immutable.Map[Id, T]) { values = v }
def getValues: scala.collection.immutable.Map[Id, T] = values
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package de.retest.guistatemachine.model

final case class TestSuites(testSuites: Map[TestSuite]) {
// TODO Generate IDs in a better way. Maybe random numbers until one unused element is found?
def generateId: Id = this.synchronized { if (testSuites.values.isEmpty) Id(0) else Id(testSuites.values.keySet.max.id + 1) }
}
final case class TestSuites(var values: scala.collection.immutable.Map[Id, TestSuite]) extends Map[TestSuite](values)
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ import de.retest.guistatemachine.model.Map
*/
class Persistence {
// database
private val guiApplications = GuiApplications(Map[GuiApplication](new HashMap[Id, GuiApplication]))
private val guiApplications = GuiApplications(new HashMap[Id, GuiApplication])

def getApplications(): GuiApplications = guiApplications

def addApplication(): Id = {
val apps = guiApplications
apps.synchronized {
val id = apps.generateId
apps.applications.values = apps.applications.values + (id -> new GuiApplication(TestSuites(Map[TestSuite](new HashMap[Id, TestSuite]))))
apps.setValues(apps.getValues + (id -> new GuiApplication(TestSuites(new HashMap[Id, TestSuite]))))
id
}
}
def getApplication(id: Id): Option[GuiApplication] = {
val apps = guiApplications
apps.synchronized {
if (apps.applications.values.contains(id)) Some(apps.applications.values(id)) else { None }
if (apps.values.contains(id)) Some(apps.values(id)) else { None }
}
}

Expand All @@ -49,7 +49,7 @@ class Persistence {
val testSuites = x.testSuites
testSuites.synchronized {
val id = testSuites.generateId
testSuites.testSuites.values = testSuites.testSuites.values + (id -> TestSuite())
testSuites.setValues(testSuites.getValues + (id -> TestSuite()))
Some(id)
}
}
Expand All @@ -62,7 +62,7 @@ class Persistence {
case Some(x) => {
val testSuites = x.testSuites
testSuites.synchronized {
if (testSuites.testSuites.values.contains(testSuiteId)) Some(testSuites.testSuites.values(testSuiteId)) else None
if (testSuites.values.contains(testSuiteId)) Some(testSuites.values(testSuiteId)) else None
}
}
case None => None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class RestServiceSpec extends WordSpec with Matchers with ScalatestRouteTest wit
handled shouldEqual true
mediaType shouldEqual MediaTypes.`application/json`
val r = responseAs[GuiApplications]
r.applications.values.size shouldEqual 0
r.values.size shouldEqual 0
}
}

Expand All @@ -47,15 +47,16 @@ class RestServiceSpec extends WordSpec with Matchers with ScalatestRouteTest wit
"return an empty application for the GET request with the path /application/0" in {
Get("/applications/0") ~> sut ~> check {
// TODO Print response here
println("Response: " + responseAs[String])
val r = responseAs[GuiApplication]
r.testSuites.testSuites.values.size shouldEqual 0
r.testSuites.values.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.values.size shouldEqual 0
r.values.size shouldEqual 0
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class PersistenceSpec extends WordSpec with Matchers {

"The pesistence" should {
"allow adding one test suite" in {
sut.getApplications().applications.values.size shouldEqual 0
sut.getApplications().values.size shouldEqual 0
sut.addApplication().id shouldEqual 0
sut.getApplications().applications.values.size shouldEqual 1
sut.getTestSuites(Id(0)).get.testSuites.values.size shouldEqual 0
sut.getApplications().values.size shouldEqual 1
sut.getTestSuites(Id(0)).get.values.size shouldEqual 0
sut.addTestSuite(Id(0)).get.id shouldEqual 0
sut.getTestSuites(Id(0)).get.testSuites.values.size shouldEqual 1
sut.getTestSuites(Id(0)).get.values.size shouldEqual 1
val s = sut.getTestSuite(Id(0), Id(0))
s.isEmpty shouldEqual false
}
Expand Down

0 comments on commit b400c42

Please sign in to comment.