-
Notifications
You must be signed in to change notification settings - Fork 5
/
EnergyManagementSourceTest.groovy
284 lines (257 loc) · 7.2 KB
/
EnergyManagementSourceTest.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* © 2024. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.io.source
import static edu.ie3.test.helper.EntityMap.map
import edu.ie3.datamodel.exceptions.FactoryException
import edu.ie3.datamodel.exceptions.SourceException
import edu.ie3.datamodel.io.factory.input.AssetInputEntityData
import edu.ie3.datamodel.models.input.EmInput
import edu.ie3.datamodel.utils.Try
import spock.lang.Specification
import java.util.stream.Stream
class EnergyManagementSourceTest extends Specification {
def "An EnergyManagementSource should construct hierarchical EmInputs with two branches as expected"() {
given:
def assetEntityDataStream = Stream.of(
new AssetInputEntityData(
["uuid": "0-0-0-0-0",
"id": "root",
"parentem" : "",
"controlstrategy" : ""],
EmInput
),
new AssetInputEntityData(
["uuid": "0-0-0-0-1",
"id": "child 1",
"parentem" : "0-0-0-0-0",
"controlstrategy" : ""],
EmInput
),
new AssetInputEntityData(
["uuid": "0-0-0-0-11",
"id": "child 1-1",
"parentem" : "0-0-0-0-1",
"controlstrategy" : ""],
EmInput
),
new AssetInputEntityData(
["uuid": "0-0-0-0-2",
"id": "child 2",
"parentem" : "0-0-0-0-0",
"controlstrategy" : ""],
EmInput
),
new AssetInputEntityData(
["uuid": "0-0-0-0-21",
"id": "child 2-1",
"parentem" : "0-0-0-0-2",
"controlstrategy" : ""],
EmInput
),
).map(data -> Try.of(() -> data, SourceException))
expect:
def emUnits = EnergyManagementSource.createEmInputs(assetEntityDataStream)
def expectedRootEm = new EmInput(
UUID.fromString("0-0-0-0-0"),
"root",
"",
null
)
def expectedEm1 = new EmInput(
UUID.fromString("0-0-0-0-1"),
"child 1",
"",
expectedRootEm
)
def expectedEm11 = new EmInput(
UUID.fromString("0-0-0-0-11"),
"child 1-1",
"",
expectedEm1
)
def expectedEm2 = new EmInput(
UUID.fromString("0-0-0-0-2"),
"child 2",
"",
expectedRootEm
)
def expectedEm21 = new EmInput(
UUID.fromString("0-0-0-0-21"),
"child 2-1",
"",
expectedEm2
)
emUnits == map([expectedRootEm, expectedEm1, expectedEm11, expectedEm2, expectedEm21])
}
def "An EnergyManagementSource should construct flat EmInputs without hierarchy as expected"() {
given:
def assetEntityDataStream = Stream.of(
new AssetInputEntityData(
["uuid": "0-0-0-0-1",
"id": "em 1",
"parentem" : "",
"controlstrategy" : ""],
EmInput
),
new AssetInputEntityData(
["uuid": "0-0-0-0-2",
"id": "em 2",
"parentem" : "",
"controlstrategy" : "strat_b"],
EmInput
),
new AssetInputEntityData(
["uuid": "0-0-0-0-3",
"id": "em 3",
"parentem" : "",
"controlstrategy" : "other"],
EmInput
),
).map(data -> Try.of(() -> data, SourceException))
expect:
def emUnits = EnergyManagementSource.createEmInputs(assetEntityDataStream)
def expectedEm1 = new EmInput(
UUID.fromString("0-0-0-0-1"),
"em 1",
"",
null
)
def expectedEm2 = new EmInput(
UUID.fromString("0-0-0-0-2"),
"em 2",
"strat_b",
null
)
def expectedEm3 = new EmInput(
UUID.fromString("0-0-0-0-3"),
"em 3",
"other",
null
)
emUnits == map([expectedEm1, expectedEm2, expectedEm3])
}
def "An EnergyManagementSource should fail if any entity data already failed before"() {
given:
def assetEntityDataStream = Stream.of(
new Try.Success<AssetInputEntityData, SourceException>(new AssetInputEntityData(
["uuid": "0-0-0-0-1",
"id": "em 1",
"parentem" : "",
"controlstrategy" : ""],
EmInput
)),
new Try.Success<AssetInputEntityData, SourceException>(new AssetInputEntityData(
["uuid": "0-0-0-0-2",
"id": "em 2",
"parentem" : "",
"controlstrategy" : ""],
EmInput
)),
new Try.Failure<AssetInputEntityData, SourceException>(new SourceException("test failure abc"))
)
when:
EnergyManagementSource.createEmInputs(assetEntityDataStream)
then:
def exc = thrown(SourceException)
exc.cause.message.contains("test failure abc")
}
def "An EnergyManagementSource should fail if a parent EM UUID is malformed"() {
given:
def assetEntityDataStream = Stream.of(
new AssetInputEntityData(
["uuid": "0-0-0-0-1",
"id": "em 1",
"parentem" : "",
"controlstrategy" : ""],
EmInput
),
new AssetInputEntityData(
["uuid": "0-0-0-0-2",
"id": "em 2",
"parentem" : "not-a-uuid",
"controlstrategy" : ""],
EmInput
),
).map(data -> Try.of(() -> data, SourceException))
when:
EnergyManagementSource.createEmInputs(assetEntityDataStream)
then:
def exc = thrown(SourceException)
exc.cause.message.contains("Exception while trying to parse UUID of field \"parentem\" with value \"not-a-uuid\"")
}
def "An EnergyManagementSource should fail if the factory fails for one EM"() {
given:
def assetEntityDataStream = Stream.of(
new AssetInputEntityData(
["uuid": "0-0-0-0-1",
"id": "em 1",
"parentem" : "",
"controlstrategy" : ""],
EmInput
),
new AssetInputEntityData(
["uuid": "0-0-0-0-2", // id is missing
"parentem" : "",
"controlstrategy" : ""],
EmInput
),
).map(data -> Try.of(() -> data, SourceException))
when:
EnergyManagementSource.createEmInputs(assetEntityDataStream)
then:
def exc = thrown(SourceException)
exc.cause.message.contains("An error occurred when creating instance of EmInput")
exc.cause.cause.class == FactoryException
}
def "An EnergyManagementSource should fail if a parent em is not provided"() {
given:
def assetEntityDataStream = Stream.of(
new AssetInputEntityData(
["uuid": "0-0-0-0-1",
"id": "em 1",
"parentem" : "",
"controlstrategy" : ""],
EmInput
),
new AssetInputEntityData(
["uuid": "0-0-0-0-2",
"id": "em 2",
"parentem" : "1-2-3-4-5", // does not exist
"controlstrategy" : ""],
EmInput
),
).map(data -> Try.of(() -> data, SourceException))
when:
EnergyManagementSource.createEmInputs(assetEntityDataStream)
then:
def exc = thrown(SourceException)
exc.message.contains("were assigned a parent EM that does not exist.")
}
def "An EnergyManagementSource should fail if no parent ems are provided"() {
given:
def assetEntityDataStream = Stream.of(
new AssetInputEntityData(
["uuid": "0-0-0-0-1",
"id": "em 1",
"parentem" : "1-2-3-4-5", // does not exist
"controlstrategy" : ""],
EmInput
),
new AssetInputEntityData(
["uuid": "0-0-0-0-2",
"id": "em 2",
"parentem" : "1-2-3-4-5", // does not exist
"controlstrategy" : ""],
EmInput
),
).map(data -> Try.of(() -> data, SourceException))
when:
EnergyManagementSource.createEmInputs(assetEntityDataStream)
then:
def exc = thrown(SourceException)
exc.message.contains("were assigned a parent EM that does not exist.")
}
}