Skip to content

Commit 30988b2

Browse files
author
Johannes Duesing
committed
Implemented missing RequestHandlerTest. Fixed some code style issues. Ignored recovery file.
1 parent 86d4bdb commit 30988b2

File tree

7 files changed

+107
-28
lines changed

7 files changed

+107
-28
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.idea
44
project/target/
55
target/
6+
dump.temp

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/RequestHandler.scala

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.upb.cs.swt.delphi.instanceregistry
22

3+
import akka.actor.ActorSystem
34
import de.upb.cs.swt.delphi.instanceregistry.daos.{DynamicInstanceDAO, InstanceDAO}
45
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.Instance
56
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.InstanceEnums.ComponentType
@@ -8,15 +9,17 @@ import scala.util.{Failure, Success, Try}
89

910
class RequestHandler (configuration: Configuration) extends AppLogging {
1011

11-
implicit val system = Registry.system
12+
implicit val system : ActorSystem = Registry.system
1213

1314
private val instanceDao : InstanceDAO = new DynamicInstanceDAO(configuration)
1415

1516
def initialize() : Unit = {
1617
log.info("Initializing request handler...")
1718
instanceDao.initialize()
18-
//Add default ES instance
19-
registerNewInstance(Instance(None, "elasticsearch://localhost", 9200, "Default ElasticSearch Instance", ComponentType.ElasticSearch))
19+
if(!instanceDao.allInstances().exists(instance => instance.name.equals("Default ElasticSearch Instance"))){
20+
//Add default ES instance
21+
registerNewInstance(Instance(None, "elasticsearch://localhost", 9200, "Default ElasticSearch Instance", ComponentType.ElasticSearch))
22+
}
2023
log.info("Done initializing request handler.")
2124
}
2225

@@ -25,10 +28,10 @@ class RequestHandler (configuration: Configuration) extends AppLogging {
2528
}
2629

2730
def registerNewInstance(instance : Instance) : Try[Long] = {
28-
val newID = if(instanceDao.getAllInstances().isEmpty){
31+
val newID = if(instanceDao.allInstances().isEmpty){
2932
0L
3033
} else {
31-
(instanceDao.getAllInstances().map(i => i.id.getOrElse(0L)) max) + 1L
34+
(instanceDao.allInstances().map(i => i.id.getOrElse(0L)) max) + 1L
3235
}
3336

3437
log.info(s"Assigned new id $newID to registering instance with name ${instance.name}.")
@@ -55,7 +58,7 @@ class RequestHandler (configuration: Configuration) extends AppLogging {
5558
}
5659

5760
def getNumberOfInstances(compType : ComponentType) : Int = {
58-
instanceDao.getAllInstances().count(i => i.componentType == compType)
61+
instanceDao.allInstances().count(i => i.componentType == compType)
5962
}
6063

6164
def getMatchingInstanceOfType(compType : ComponentType ) : Try[Instance] = {

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/Server.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ object Server extends HttpApp with JsonSupport with AppLogging {
3131
path("deregister") { deleteInstance() } ~
3232
path("instances" ) { fetchInstancesOfType() } ~
3333
path("numberOfInstances" ) { numberOfInstances() } ~
34-
path("matchingInstance" ) { getMatchingInstance()} ~
34+
path("matchingInstance" ) { matchingInstance()} ~
3535
path("matchingResult" ) {matchInstance()}
3636

3737

@@ -99,7 +99,7 @@ object Server extends HttpApp with JsonSupport with AppLogging {
9999
}
100100
}
101101

102-
def getMatchingInstance() : server.Route = parameters('ComponentType.as[String]){ compTypeString =>
102+
def matchingInstance() : server.Route = parameters('ComponentType.as[String]){ compTypeString =>
103103
get{
104104
log.debug(s"GET /matchingInstance?ComponentType=$compTypeString has been called")
105105

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import java.io.{File, IOException, PrintWriter}
44

55
import akka.actor.ActorSystem
66
import akka.stream.ActorMaterializer
7-
import de.upb.cs.swt.delphi.instanceregistry.{AppLogging, Configuration, Registry, Server}
7+
import de.upb.cs.swt.delphi.instanceregistry.{AppLogging, Configuration, Registry}
88
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.{Instance, JsonSupport}
99
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.InstanceEnums.ComponentType
1010

1111
import scala.collection.mutable
1212
import scala.concurrent.ExecutionContext
1313
import scala.util.{Failure, Success, Try}
1414
import spray.json._
15-
import spray.json.DefaultJsonProtocol._
1615

1716
import scala.io.Source
1817

@@ -85,7 +84,7 @@ class DynamicInstanceDAO (configuration : Configuration) extends InstanceDAO wit
8584
List() ++ instances filter {i => i.componentType == componentType}
8685
}
8786

88-
override def getAllInstances(): List[Instance] = {
87+
override def allInstances(): List[Instance] = {
8988
List() ++ instances
9089
}
9190

@@ -141,7 +140,7 @@ class DynamicInstanceDAO (configuration : Configuration) extends InstanceDAO wit
141140
private[daos] def dumpToRecoveryFile() : Unit = {
142141
log.debug(s"Dumping data to recovery file ${configuration.recoveryFileName} ...")
143142
val writer = new PrintWriter(new File(configuration.recoveryFileName))
144-
writer.write(getAllInstances().toJson(listFormat(instanceFormat)).toString())
143+
writer.write(allInstances().toJson(listFormat(instanceFormat)).toString())
145144
writer.flush()
146145
writer.close()
147146
log.debug(s"Successfully wrote to recovery file.")

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ trait InstanceDAO {
4949
* Retrieves all instances from the DAO
5050
* @return A list of all instances in the DAO
5151
*/
52-
def getAllInstances() : List[Instance]
52+
def allInstances() : List[Instance]
5353

5454
/**
5555
* Removes all instances from the DAO
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,93 @@
11
package de.upb.cs.swt.delphi.instanceregistry
22

3-
import org.scalatest.{BeforeAndAfterEach, FlatSpec, Matchers}
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, BeforeAndAfterEach, FlatSpec, Matchers}
46

5-
class RequestHandlerTest extends FlatSpec with Matchers with BeforeAndAfterEach{
7+
class RequestHandlerTest extends FlatSpec with Matchers with BeforeAndAfterEach with BeforeAndAfterAll{
68

79
val handler : RequestHandler = new RequestHandler(new Configuration())
810

11+
private def buildInstance(id : Long) : Instance = {
12+
Instance(Some(id), "https://localhost", 12345, "TestInstance", ComponentType.ElasticSearch)
13+
}
14+
15+
override protected def beforeAll():Unit = {
16+
//Delete recovery file for sure
17+
handler.shutdown()
18+
}
19+
920
override protected def beforeEach(): Unit = {
21+
handler.initialize()
22+
}
23+
24+
"The RequestHandler" must "assign new IDs to instances regardless of their actual id" in {
25+
val registerNewInstance = handler.registerNewInstance(buildInstance(Long.MaxValue))
26+
assert(registerNewInstance.isSuccess)
27+
assert(registerNewInstance.get != Long.MaxValue)
1028

29+
val registerNewInstance2 = handler.registerNewInstance(buildInstance(1L))
30+
assert(registerNewInstance2.isSuccess)
31+
assert(registerNewInstance2.get != 1L)
1132
}
1233

13-
override protected def afterEach(): Unit = {
34+
it must "be able to remove instances that have been added before" in {
35+
val registerNewInstance = handler.registerNewInstance(buildInstance(-1L))
36+
assert(registerNewInstance.isSuccess)
37+
assert(registerNewInstance.get == 1)
38+
assert(handler.removeInstance(1).isSuccess)
39+
}
40+
41+
it must "not add two default ES instances on initializing" in {
42+
assert(handler.getNumberOfInstances(ComponentType.ElasticSearch) == 1)
43+
handler.initialize()
44+
assert(handler.getNumberOfInstances(ComponentType.ElasticSearch) == 1)
45+
}
46+
47+
it must "match to instance with most consecutive positive matching results" in {
48+
val esInstance = handler.registerNewInstance(buildInstance(2))
49+
assert(esInstance.isSuccess)
50+
assert(esInstance.get == 1)
51+
52+
assert(handler.applyMatchingResult(0, result = false).isSuccess)
53+
assert(handler.applyMatchingResult(0, result = true).isSuccess)
54+
assert(handler.applyMatchingResult(0,result = true).isSuccess)
55+
56+
assert(handler.applyMatchingResult(1,result = true).isSuccess)
57+
assert(handler.applyMatchingResult(1,result = false).isSuccess)
1458

59+
val matchingInstance = handler.getMatchingInstanceOfType(ComponentType.ElasticSearch)
60+
assert(matchingInstance.isSuccess)
61+
assert(matchingInstance.get.id.get == 0)
62+
63+
assert(handler.removeInstance(1L).isSuccess)
64+
}
65+
66+
it must "match to instance with most positive matching results" in {
67+
val esInstance = handler.registerNewInstance(buildInstance(2))
68+
assert(esInstance.isSuccess)
69+
assert(esInstance.get == 1)
70+
71+
assert(handler.applyMatchingResult(0, result = true).isSuccess)
72+
assert(handler.applyMatchingResult(0,result = true).isSuccess)
73+
assert(handler.applyMatchingResult(0, result = false).isSuccess)
74+
75+
assert(handler.applyMatchingResult(1,result = false).isSuccess)
76+
assert(handler.applyMatchingResult(1,result = false).isSuccess)
77+
78+
val matchingInstance = handler.getMatchingInstanceOfType(ComponentType.ElasticSearch)
79+
assert(matchingInstance.isSuccess)
80+
assert(matchingInstance.get.id.get == 0)
81+
82+
assert(handler.removeInstance(1L).isSuccess)
83+
}
84+
85+
it must "fail to match if no instance of type is present" in {
86+
assert(handler.getMatchingInstanceOfType(ComponentType.Crawler).isFailure)
87+
}
88+
89+
override protected def afterEach(): Unit = {
90+
handler.shutdown()
1591
}
1692

1793
}

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class DynamicInstanceDAOTest extends FlatSpec with Matchers with BeforeAndAfterE
2222

2323
"The instance DAO" must "be able to add an get an instance with a new id" in {
2424
assert(dao.addInstance(buildInstance(4)).isSuccess)
25-
assert(dao.getAllInstances().size == 4)
25+
assert(dao.allInstances().size == 4)
2626
assert(dao.hasInstance(4))
2727
assert(dao.removeInstance(4).isSuccess)
2828
}
2929

3030
it must "not allow the addition of any id twice" in {
3131
assert(dao.addInstance(buildInstance(3)).isFailure)
32-
assert(dao.getAllInstances().size == 3)
32+
assert(dao.allInstances().size == 3)
3333
}
3434

3535
it must "return true on hasInstance for any present id" in {
@@ -67,7 +67,7 @@ class DynamicInstanceDAOTest extends FlatSpec with Matchers with BeforeAndAfterE
6767
assert(dao.removeInstance(i).isSuccess)
6868
assert(!dao.hasInstance(i))
6969
}
70-
assert(dao.getAllInstances().isEmpty)
70+
assert(dao.allInstances().isEmpty)
7171
}
7272

7373
it must "not change the data on removing invalid IDs" in {
@@ -78,7 +78,7 @@ class DynamicInstanceDAOTest extends FlatSpec with Matchers with BeforeAndAfterE
7878

7979
it must "remove all instance on removeAll" in {
8080
dao.removeAll()
81-
assert(dao.getAllInstances().isEmpty)
81+
assert(dao.allInstances().isEmpty)
8282
}
8383

8484
it must "have an empty list of matching results for newly added instances" in {
@@ -107,29 +107,29 @@ class DynamicInstanceDAOTest extends FlatSpec with Matchers with BeforeAndAfterE
107107
"The DAO" must "be able to read multiple instances from the recovery file" in {
108108
dao.dumpToRecoveryFile()
109109
dao.clearData()
110-
assert(dao.getAllInstances().isEmpty)
110+
assert(dao.allInstances().isEmpty)
111111
dao.tryInitFromRecoveryFile()
112-
assert(dao.getAllInstances().size == 3)
112+
assert(dao.allInstances().size == 3)
113113
}
114114

115115
it must "fail to load from recovery file if it is not present" in {
116116
dao.dumpToRecoveryFile()
117-
assert(dao.getAllInstances().size == 3)
117+
assert(dao.allInstances().size == 3)
118118
dao.deleteRecoveryFile()
119119
dao.clearData()
120-
assert(dao.getAllInstances().isEmpty)
120+
assert(dao.allInstances().isEmpty)
121121
dao.tryInitFromRecoveryFile()
122-
assert(dao.getAllInstances().isEmpty)
122+
assert(dao.allInstances().isEmpty)
123123
}
124124

125125
it must "contain the correct instance data after loading from recovery file" in {
126126
assert(dao.addInstance(buildInstance(4)).isSuccess)
127127
dao.dumpToRecoveryFile()
128-
assert(dao.getAllInstances().size == 4)
128+
assert(dao.allInstances().size == 4)
129129
dao.clearData()
130-
assert(dao.getAllInstances().isEmpty)
130+
assert(dao.allInstances().isEmpty)
131131
dao.tryInitFromRecoveryFile()
132-
assert(dao.getAllInstances().size == 4)
132+
assert(dao.allInstances().size == 4)
133133
val instance = dao.getInstance(4)
134134
assert(instance.isDefined)
135135
assert(instance.get.id.get == 4)

0 commit comments

Comments
 (0)