Skip to content

Commit bc764a6

Browse files
author
Johannes Duesing
committed
Implemented instance DAO and first tests
Test cases need to be extended
1 parent c176c32 commit bc764a6

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package de.upb.cs.swt.delphi.instanceregistry.daos
2+
3+
import akka.actor.ActorSystem
4+
import de.upb.cs.swt.delphi.instanceregistry.{AppLogging, Server}
5+
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.Instance
6+
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.InstanceEnums.ComponentType
7+
8+
import scala.collection.mutable
9+
import scala.util.{Failure, Success, Try}
10+
11+
/**
12+
* Implementation of the instance data access object that keeps its data in memory
13+
* instead of using a persistent storage.
14+
*/
15+
class DynamicInstanceDAO extends InstanceDAO with AppLogging {
16+
17+
private val instances : mutable.Set[Instance] = new mutable.HashSet[Instance]()
18+
implicit val system : ActorSystem = Server.system
19+
20+
override def addInstance(instance: Instance): Try[Unit] = {
21+
//Verify ID is present in instance
22+
if(instance.id.isEmpty){
23+
val msg = s"Cannot add instance ${instance.name}, id is empty!"
24+
log.warning(msg)
25+
Failure(new RuntimeException(msg))
26+
} else {
27+
//Verify id is not already present in instances!
28+
if(!hasInstance(instance.id.get)){
29+
instances.add(instance)
30+
Success(log.info(s"Added instance ${instance.name} with id ${instance.id} to database."))
31+
} else {
32+
val msg = s"Cannot add instance ${instance.name}, id ${instance.id} already present."
33+
log.warning(msg)
34+
Failure(new RuntimeException(msg))
35+
}
36+
}
37+
}
38+
39+
override def hasInstance(id: Long): Boolean = {
40+
//addInstance verifies that id : Option[Long] is not empty, so can apply .get here!
41+
val query = instances filter {i => i.id.get == id}
42+
query.nonEmpty
43+
}
44+
45+
override def removeInstance(id: Long): Try[Unit] = {
46+
if(hasInstance(id)){
47+
//AddInstance verifies that id is always present, hasInstance verifies that find will return an instance
48+
instances.remove(instances.find(i => i.id.get == id).get)
49+
Success(log.info(s"Successfully removed instance with id $id."))
50+
} else {
51+
val msg = s"Cannot remove instance with id $id, that id is not present."
52+
log.warning(msg)
53+
Failure(new RuntimeException(msg))
54+
}
55+
}
56+
57+
override def getInstance(id: Long): Option[Instance] = {
58+
if(hasInstance(id)) {
59+
val query = instances filter {i => i.id.get == id}
60+
val instance = query.iterator.next()
61+
Some(instance)
62+
} else {
63+
None
64+
}
65+
}
66+
67+
override def getInstancesOfType(componentType: ComponentType): List[Instance] = {
68+
List() ++ instances filter {i => i.componentType == componentType}
69+
}
70+
71+
override def getAllInstances(): List[Instance] = {
72+
List() ++ instances
73+
}
74+
75+
override def clearAll() : Unit = {
76+
instances.clear()
77+
}
78+
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package de.upb.cs.swt.delphi.instanceregistry.daos
2+
3+
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.Instance
4+
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.InstanceEnums.ComponentType
5+
6+
import scala.util.Try
7+
8+
/**
9+
* An Data Access Object to access the set of registered instances
10+
*/
11+
trait InstanceDAO {
12+
13+
/***
14+
* Add a new instance to the DAO.
15+
* @param instance Instance to add (attribute 'id' must not be empty!)
16+
* @return Success if id was not already present, Failure otherwise
17+
*/
18+
def addInstance(instance : Instance) : Try[Unit]
19+
20+
/**
21+
* Checks whether the DAO holds an instance with the specified id.
22+
* @param id Id to look for
23+
* @return True if id is present, false otherwise
24+
*/
25+
def hasInstance(id: Long) : Boolean
26+
27+
/**
28+
* Removes the instance with the given id from the DAO.
29+
* @param id Id of the instance that will be removed
30+
* @return Success if id was present, Failure otherwise
31+
*/
32+
def removeInstance(id: Long) : Try[Unit]
33+
34+
/**
35+
* Gets the instance with the specified id from the DAO
36+
* @param id Id of the instance to retrieve
37+
* @return Some(instance) if present, else None
38+
*/
39+
def getInstance(id: Long) : Option[Instance]
40+
41+
/**
42+
* Retrieves all instances of the specified ComponentType from the DAO
43+
* @param componentType ComponentType to look for
44+
* @return A list of instances with the specified type
45+
*/
46+
def getInstancesOfType(componentType : ComponentType) : List[Instance]
47+
48+
/**
49+
* Retrieves all instances from the DAO
50+
* @return A list of all instances in the DAO
51+
*/
52+
def getAllInstances() : List[Instance]
53+
54+
/**
55+
* Removes all instances from the DAO
56+
*/
57+
def clearAll() : Unit
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package de.upb.cs.swt.delphi.instanceregistry.daos
2+
3+
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.Instance
4+
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.InstanceEnums.ComponentType
5+
import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers}
6+
7+
class DynamicInstanceDAOTest extends FlatSpec with Matchers with BeforeAndAfterAll{
8+
9+
val dao : InstanceDAO = new DynamicInstanceDAO()
10+
11+
private def buildInstance(id : Int) : Instance = {
12+
Instance(Some(id), "https://localhost", 12345, "TestInstance", ComponentType.Crawler)
13+
}
14+
15+
override protected def beforeAll() : Unit = {
16+
dao.clearAll()
17+
for(i <- 1 to 3){
18+
dao.addInstance(buildInstance(i))
19+
}
20+
}
21+
22+
"The instance DAO" must "be able to add an get an instance with a new id" in {
23+
assert(dao.addInstance(buildInstance(4)).isSuccess)
24+
assert(dao.getAllInstances().size == 4)
25+
assert(dao.hasInstance(4))
26+
assert(dao.removeInstance(4).isSuccess)
27+
}
28+
29+
}

0 commit comments

Comments
 (0)