Skip to content

Commit d5826d0

Browse files
author
Johannes Duesing
committed
Added the posting of matching results to DAO
updated interface, implementation and tests
1 parent fcbaf27 commit d5826d0

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/daos/DynamicInstanceDAO.scala

+30
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import scala.io.Source
2323
class DynamicInstanceDAO (configuration : Configuration) extends InstanceDAO with AppLogging with JsonSupport {
2424

2525
private val instances : mutable.Set[Instance] = new mutable.HashSet[Instance]()
26+
private val instanceMatchingResults : mutable.Map[Long, mutable.MutableList[Boolean]] = new mutable.HashMap[Long,mutable.MutableList[Boolean]]()
2627

2728
implicit val system : ActorSystem = Server.system
2829
implicit val materializer : ActorMaterializer = ActorMaterializer()
@@ -39,6 +40,7 @@ class DynamicInstanceDAO (configuration : Configuration) extends InstanceDAO wit
3940
//Verify id is not already present in instances!
4041
if(!hasInstance(instance.id.get)){
4142
instances.add(instance)
43+
instanceMatchingResults.put(instance.id.get, mutable.MutableList())
4244
dumpToRecoveryFile()
4345
Success(log.info(s"Added instance ${instance.name} with id ${instance.id} to database."))
4446
} else {
@@ -59,6 +61,7 @@ class DynamicInstanceDAO (configuration : Configuration) extends InstanceDAO wit
5961
if(hasInstance(id)){
6062
//AddInstance verifies that id is always present, hasInstance verifies that find will return an instance
6163
instances.remove(instances.find(i => i.id.get == id).get)
64+
instanceMatchingResults.remove(id)
6265
dumpToRecoveryFile()
6366
Success(log.info(s"Successfully removed instance with id $id."))
6467
} else {
@@ -88,11 +91,38 @@ class DynamicInstanceDAO (configuration : Configuration) extends InstanceDAO wit
8891

8992
override def removeAll() : Unit = {
9093
instances.clear()
94+
instanceMatchingResults.clear()
9195
dumpToRecoveryFile()
9296
}
9397

98+
override def addMatchingResult(id: Long, matchingSuccessful: Boolean): Try[Unit] = {
99+
if(hasInstance(id)){
100+
instanceMatchingResults.get(id) match {
101+
case Some(resultList) =>
102+
resultList += matchingSuccessful
103+
Success(log.info(s"Successfully added matching result $matchingSuccessful to instance with id $id."))
104+
case None =>
105+
log.warning(s"Could not add matching result, list for instance with id $id not present!")
106+
Failure(new RuntimeException("No matching result list present"))
107+
}
108+
} else {
109+
log.warning(s"Cannot add matching result, instance with id $id not present.")
110+
Failure(new RuntimeException(s"Cannot add matching result, instance with id $id not present."))
111+
}
112+
}
113+
114+
override def getMatchingResultsFor(id: Long): Try[List[Boolean]] = {
115+
if(hasInstance(id) && instanceMatchingResults.contains(id)){
116+
Success(List() ++ instanceMatchingResults(id))
117+
} else {
118+
log.warning(s"Cannot get matching results, id $id not present!")
119+
Failure(new RuntimeException(s"Cannot get matching results, id $id not present!"))
120+
}
121+
}
122+
94123
private[daos] def clearData() : Unit = {
95124
instances.clear()
125+
instanceMatchingResults.clear()
96126
}
97127

98128
private[daos] def dumpToRecoveryFile() : Unit = {

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/daos/InstanceDAO.scala

+14
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,18 @@ trait InstanceDAO {
5656
*/
5757
def removeAll() : Unit
5858

59+
/**
60+
* Add a matching result for the specified instance
61+
* @param id Id of the instance to post a result for
62+
* @param matchingSuccessful Boolean indicating whether the matching was successful
63+
*/
64+
def addMatchingResult(id: Long, matchingSuccessful : Boolean) : Try[Unit]
65+
66+
/**
67+
* Gets the list of matching results for the instance with the specified id
68+
* @param id Id of the instance
69+
* @return List of boolean values
70+
*/
71+
def getMatchingResultsFor(id: Long) : Try[List[Boolean]]
72+
5973
}

src/test/scala/de/upb/cs/swt/delphi/instanceregistry/daos/DynamicInstanceDAOTest.scala

+26-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package de.upb.cs.swt.delphi.instanceregistry.daos
33
import de.upb.cs.swt.delphi.instanceregistry.Configuration
44
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.Instance
55
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.InstanceEnums.ComponentType
6-
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, FlatSpec, Matchers}
6+
import org.scalatest.{BeforeAndAfterEach, FlatSpec, Matchers}
77

88
class DynamicInstanceDAOTest extends FlatSpec with Matchers with BeforeAndAfterEach{
99

@@ -81,6 +81,29 @@ class DynamicInstanceDAOTest extends FlatSpec with Matchers with BeforeAndAfterE
8181
assert(dao.getAllInstances().isEmpty)
8282
}
8383

84+
it must "have an empty list of matching results for newly added instances" in {
85+
dao.addInstance(buildInstance(4))
86+
assert(dao.getMatchingResultsFor(4).isSuccess)
87+
assert(dao.getMatchingResultsFor(4).get.isEmpty)
88+
}
89+
90+
it must "keep the correct order of matching results posted" in {
91+
assert(dao.addMatchingResult(3, matchingSuccessful = true).isSuccess)
92+
assert(dao.addMatchingResult(3, matchingSuccessful = true).isSuccess)
93+
assert(dao.addMatchingResult(3, matchingSuccessful = false).isSuccess)
94+
95+
assert(dao.getMatchingResultsFor(3).isSuccess)
96+
assert(dao.getMatchingResultsFor(3).get.head)
97+
assert(dao.getMatchingResultsFor(3).get (1))
98+
assert(!dao.getMatchingResultsFor(3).get (2))
99+
100+
}
101+
102+
it must "remove the matching results when the instance is removed" in {
103+
assert(dao.removeInstance(3).isSuccess)
104+
assert(dao.getMatchingResultsFor(3).isFailure)
105+
}
106+
84107
"The DAO" must "be able to read multiple instances from the recovery file" in {
85108
dao.dumpToRecoveryFile()
86109
dao.clearData()
@@ -113,6 +136,8 @@ class DynamicInstanceDAOTest extends FlatSpec with Matchers with BeforeAndAfterE
113136
}
114137

115138

139+
140+
116141
override protected def afterEach() : Unit = {
117142
dao.removeAll()
118143
dao.deleteRecoveryFile()

0 commit comments

Comments
 (0)