1
1
package de .upb .cs .swt .delphi .instanceregistry .daos
2
2
3
+ import de .upb .cs .swt .delphi .instanceregistry .Configuration
3
4
import de .upb .cs .swt .delphi .instanceregistry .io .swagger .client .model .Instance
4
5
import de .upb .cs .swt .delphi .instanceregistry .io .swagger .client .model .InstanceEnums .ComponentType
5
- import org .scalatest .{BeforeAndAfterAll , FlatSpec , Matchers }
6
+ import org .scalatest .{BeforeAndAfterAll , BeforeAndAfterEach , FlatSpec , Matchers }
6
7
7
- class DynamicInstanceDAOTest extends FlatSpec with Matchers with BeforeAndAfterAll {
8
+ class DynamicInstanceDAOTest extends FlatSpec with Matchers with BeforeAndAfterEach {
8
9
9
- val dao : InstanceDAO = new DynamicInstanceDAO ()
10
+ val dao : DynamicInstanceDAO = new DynamicInstanceDAO (new Configuration () )
10
11
11
12
private def buildInstance (id : Int ) : Instance = {
12
13
Instance (Some (id), " https://localhost" , 12345 , " TestInstance" , ComponentType .Crawler )
13
14
}
14
15
15
- override protected def beforeAll () : Unit = {
16
- dao.clearAll ()
16
+ override protected def beforeEach () : Unit = {
17
+ dao.deleteRecoveryFile ()
17
18
for (i <- 1 to 3 ){
18
19
dao.addInstance(buildInstance(i))
19
20
}
@@ -26,4 +27,95 @@ class DynamicInstanceDAOTest extends FlatSpec with Matchers with BeforeAndAfterA
26
27
assert(dao.removeInstance(4 ).isSuccess)
27
28
}
28
29
30
+ it must " not allow the addition of any id twice" in {
31
+ assert(dao.addInstance(buildInstance(3 )).isFailure)
32
+ assert(dao.getAllInstances().size == 3 )
33
+ }
34
+
35
+ it must " return true on hasInstance for any present id" in {
36
+ for (i <- 1 to 3 ){
37
+ assert(dao.hasInstance(i))
38
+ }
39
+ }
40
+
41
+ it must " return false on hasInstance for any id not present" in {
42
+ assert(! dao.hasInstance(- 1 ))
43
+ assert(! dao.hasInstance(Long .MaxValue ))
44
+ assert(! dao.hasInstance(4 ))
45
+ }
46
+
47
+ it must " return instances with the correct id on getInstance" in {
48
+ for (i <- 1 to 3 ){
49
+ val instance = dao.getInstance(i)
50
+ assert(instance.isDefined)
51
+ assert(instance.get.id.isDefined)
52
+ assert(instance.get.id.get == i)
53
+ }
54
+ }
55
+
56
+ it must " return instance with the correct type on getInstanceOfType" in {
57
+ val compTypeInstances = dao.getInstancesOfType(ComponentType .Crawler )
58
+ assert(compTypeInstances.size == 3 )
59
+
60
+ for (instance <- compTypeInstances){
61
+ assert(instance.componentType == ComponentType .Crawler )
62
+ }
63
+ }
64
+
65
+ it must " remove instances that are present in the DAO" in {
66
+ for (i <- 1 to 3 ){
67
+ assert(dao.removeInstance(i).isSuccess)
68
+ assert(! dao.hasInstance(i))
69
+ }
70
+ assert(dao.getAllInstances().isEmpty)
71
+ }
72
+
73
+ it must " not change the data on removing invalid IDs" in {
74
+ assert(dao.removeInstance(- 1 ).isFailure)
75
+ assert(dao.removeInstance(Long .MaxValue ).isFailure)
76
+ assert(dao.removeInstance(4 ).isFailure)
77
+ }
78
+
79
+ it must " remove all instance on removeAll" in {
80
+ dao.removeAll()
81
+ assert(dao.getAllInstances().isEmpty)
82
+ }
83
+
84
+ " The DAO" must " be able to read multiple instances from the recovery file" in {
85
+ dao.dumpToRecoveryFile()
86
+ dao.clearData()
87
+ assert(dao.getAllInstances().isEmpty)
88
+ dao.tryInitFromRecoveryFile()
89
+ assert(dao.getAllInstances().size == 3 )
90
+ }
91
+
92
+ it must " fail to load from recovery file if it is not present" in {
93
+ dao.dumpToRecoveryFile()
94
+ assert(dao.getAllInstances().size == 3 )
95
+ dao.deleteRecoveryFile()
96
+ dao.clearData()
97
+ assert(dao.getAllInstances().isEmpty)
98
+ dao.tryInitFromRecoveryFile()
99
+ assert(dao.getAllInstances().isEmpty)
100
+ }
101
+
102
+ it must " contain the correct instance data after loading from recovery file" in {
103
+ assert(dao.addInstance(buildInstance(4 )).isSuccess)
104
+ dao.dumpToRecoveryFile()
105
+ assert(dao.getAllInstances().size == 4 )
106
+ dao.clearData()
107
+ assert(dao.getAllInstances().isEmpty)
108
+ dao.tryInitFromRecoveryFile()
109
+ assert(dao.getAllInstances().size == 4 )
110
+ val instance = dao.getInstance(4 )
111
+ assert(instance.isDefined)
112
+ assert(instance.get.id.get == 4 )
113
+ }
114
+
115
+
116
+ override protected def afterEach () : Unit = {
117
+ dao.removeAll()
118
+ dao.deleteRecoveryFile()
119
+ }
120
+
29
121
}
0 commit comments