-
Notifications
You must be signed in to change notification settings - Fork 5
/
AssetInputEntityFactoryTest.groovy
325 lines (285 loc) · 12.4 KB
/
AssetInputEntityFactoryTest.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.io.factory.input
import edu.ie3.datamodel.exceptions.FactoryException
import edu.ie3.datamodel.models.OperationTime
import edu.ie3.datamodel.models.input.AssetInput
import edu.ie3.datamodel.models.input.OperatorInput
import edu.ie3.datamodel.utils.Try
import edu.ie3.test.helper.FactoryTestHelper
import org.apache.commons.lang3.NotImplementedException
import spock.lang.Specification
import java.time.ZonedDateTime
/**
* Tests a minimal extension of {@link AssetInputEntityFactory}
*/
class AssetInputEntityFactoryTest extends Specification implements FactoryTestHelper {
def "An AssetInputFactory should contain exactly the expected class for parsing"() {
given:
def inputFactory = new TestAssetInputFactory()
def expectedClasses = [TestAssetInput]
expect:
inputFactory.supportedClasses == Arrays.asList(expectedClasses.toArray())
}
def "An AssetInputFactory should parse a valid operated AssetInput correctly (no operation time provided)"() {
given: "a system participant input type factory and model data"
def inputFactory = new TestAssetInputFactory()
Map<String, String> parameter = [
"uuid": "91ec3bcf-1777-4d38-af67-0bf7c9fa73c7",
"id" : "TestID"
]
def inputClass = TestAssetInput
def operatorInput = Mock(OperatorInput)
when:
Try<TestAssetInput, FactoryException> input = inputFactory.get(new AssetInputEntityData(parameter, inputClass, operatorInput))
then:
input.success
input.data.get().getClass() == inputClass
input.data.get().with {
assert uuid == UUID.fromString(parameter["uuid"])
assert operationTime == OperationTime.notLimited()
assert operator == operatorInput
assert id == parameter["id"]
}
}
def "An AssetInputFactory should parse a valid operated AssetInput correctly (with nulls and empty strings)"() {
given: "a system participant input type factory and model data"
def inputFactory = new TestAssetInputFactory()
Map<String, String> parameter = [
"uuid": "91ec3bcf-1777-4d38-af67-0bf7c9fa73c7",
"operatesfrom" : operatesfrom,
"operatesuntil": operatesuntil,
"id" : "TestID"
]
def inputClass = TestAssetInput
def operatorInput = Mock(OperatorInput)
when:
Try<TestAssetInput, FactoryException> input = inputFactory.get(new AssetInputEntityData(parameter, inputClass, operatorInput))
then:
input.success
input.data.get().getClass() == inputClass
input.data.get().with {
assert uuid == UUID.fromString(parameter["uuid"])
assert operationTime == OperationTime.notLimited()
assert operator == operatorInput
assert id == parameter["id"]
}
where:
operatesfrom | operatesuntil
null | null
"" | null
null | ""
"" | ""
}
def "An AssetInputFactory should parse a valid operated AssetInput correctly (operation start time provided)"() {
given: "a system participant input type factory and model data"
def inputFactory = new TestAssetInputFactory()
Map<String, String> parameter = [
"uuid" : "91ec3bcf-1777-4d38-af67-0bf7c9fa73c7",
"operatesfrom" : "2019-01-01T00:00:00+01:00[Europe/Berlin]",
"operatesuntil": "",
"id" : "TestID"
]
def inputClass = TestAssetInput
def operatorInput = Mock(OperatorInput)
when:
Try<TestAssetInput, FactoryException> input = inputFactory.get(new AssetInputEntityData(parameter, inputClass, operatorInput))
then:
input.success
input.data.get().getClass() == inputClass
input.data.get().with {
assert uuid == UUID.fromString(parameter["uuid"])
assert operationTime.startDate.present
assert operationTime.startDate.get() == ZonedDateTime.parse(parameter["operatesfrom"])
assert !operationTime.endDate.present
assert operator == operatorInput
assert id == parameter["id"]
}
}
def "An AssetInputFactory should parse a valid operated AssetInput correctly (operation end time provided)"() {
given: "a system participant input type factory and model data"
def inputFactory = new TestAssetInputFactory()
Map<String, String> parameter = [
"uuid" : "91ec3bcf-1777-4d38-af67-0bf7c9fa73c7",
"operatesuntil": "2019-01-01T00:00:00+01:00[Europe/Berlin]",
"id" : "TestID"
]
def inputClass = TestAssetInput
def operatorInput = Mock(OperatorInput)
when:
Try<TestAssetInput, FactoryException> input = inputFactory.get(new AssetInputEntityData(parameter, inputClass, operatorInput))
then:
input.success
input.data.get().getClass() == inputClass
input.data.get().with {
assert uuid == UUID.fromString(parameter["uuid"])
assert !operationTime.startDate.present
assert operationTime.endDate.present
assert operationTime.endDate.get() == ZonedDateTime.parse(parameter["operatesuntil"])
assert operator == operatorInput
assert id == parameter["id"]
}
}
def "An AssetInputFactory should parse a valid operated AssetInput correctly (operation start and end time provided)"() {
given: "a system participant input type factory and model data"
def inputFactory = new TestAssetInputFactory()
Map<String, String> parameter = [
"uuid" : "91ec3bcf-1777-4d38-af67-0bf7c9fa73c7",
"operatesfrom" : "2019-01-01T00:00:00+01:00[Europe/Berlin]",
"operatesuntil": "2019-12-31T00:00:00+01:00[Europe/Berlin]",
"id" : "TestID"
]
def inputClass = TestAssetInput
def operatorInput = Mock(OperatorInput)
when:
Try<TestAssetInput, FactoryException> input = inputFactory.get(new AssetInputEntityData(parameter, inputClass, operatorInput))
then:
input.success
input.data.get().getClass() == inputClass
input.data.get().with {
assert uuid == UUID.fromString(parameter["uuid"])
assert operationTime.startDate.present
assert operationTime.startDate.get() == ZonedDateTime.parse(parameter["operatesfrom"])
assert operationTime.endDate.present
assert operationTime.endDate.get() == ZonedDateTime.parse(parameter["operatesuntil"])
assert operator == operatorInput
assert id == parameter["id"]
}
}
def "An AssetInputFactory should parse a valid operated, always on AssetInput correctly (no operation time provided)"() {
given: "a system participant input type factory and model data"
def inputFactory = new TestAssetInputFactory()
Map<String, String> parameter = [
"uuid": "91ec3bcf-1777-4d38-af67-0bf7c9fa73c7",
"id" : "TestID"
]
def inputClass = TestAssetInput
when:
Try<TestAssetInput, FactoryException> input = inputFactory.get(new AssetInputEntityData(parameter, inputClass))
then:
input.success
input.data.get().getClass() == inputClass
input.data.get().with {
assert uuid == UUID.fromString(parameter["uuid"])
assert operationTime == OperationTime.notLimited()
assert operator == OperatorInput.NO_OPERATOR_ASSIGNED
assert id == parameter["id"]
}
}
def "An AssetInputFactory should parse a valid AssetInput correctly (operation start time provided)"() {
given: "a system participant input type factory and model data"
def inputFactory = new TestAssetInputFactory()
Map<String, String> parameter = [
"uuid" : "91ec3bcf-1777-4d38-af67-0bf7c9fa73c7",
"operatesfrom": "2019-01-01T00:00:00+01:00[Europe/Berlin]",
"id" : "TestID"
]
def inputClass = TestAssetInput
when:
Try<TestAssetInput, FactoryException> input = inputFactory.get(new AssetInputEntityData(parameter, inputClass))
then:
input.success
input.data.get().getClass() == inputClass
input.data.get().with {
assert uuid == UUID.fromString(parameter["uuid"])
assert operationTime.startDate.present
assert operationTime.startDate.get() == ZonedDateTime.parse(parameter["operatesfrom"])
assert !operationTime.endDate.present
assert operator == OperatorInput.NO_OPERATOR_ASSIGNED
assert id == parameter["id"]
}
}
def "An AssetInputFactory should parse a valid AssetInput correctly (operation end time provided)"() {
given: "a system participant input type factory and model data"
def inputFactory = new TestAssetInputFactory()
Map<String, String> parameter = [
"uuid" : "91ec3bcf-1777-4d38-af67-0bf7c9fa73c7",
"operatesuntil": "2019-01-01T00:00:00+01:00[Europe/Berlin]",
"id" : "TestID"
]
def inputClass = TestAssetInput
when:
Try<TestAssetInput, FactoryException> input = inputFactory.get(new AssetInputEntityData(parameter, inputClass))
then:
input.success
input.data.get().getClass() == inputClass
input.data.get().with {
assert uuid == UUID.fromString(parameter["uuid"])
assert !operationTime.startDate.present
assert operationTime.endDate.present
assert operationTime.endDate.get() == ZonedDateTime.parse(parameter["operatesuntil"])
assert operator == OperatorInput.NO_OPERATOR_ASSIGNED
assert id == parameter["id"]
}
}
def "An AssetInputFactory should parse a valid AssetInput correctly (operation start and end time provided"() {
given: "a system participant input type factory and model data"
def inputFactory = new TestAssetInputFactory()
Map<String, String> parameter = [
"uuid" : "91ec3bcf-1777-4d38-af67-0bf7c9fa73c7",
"operatesfrom" : "2019-01-01T00:00:00+01:00[Europe/Berlin]",
"operatesuntil": "2019-12-31T00:00:00+01:00[Europe/Berlin]",
"id" : "TestID"
]
def inputClass = TestAssetInput
when:
Try<TestAssetInput, FactoryException> input = inputFactory.get(new AssetInputEntityData(parameter, inputClass))
then:
input.success
input.data.get().getClass() == inputClass
input.data.get().with {
assert uuid == UUID.fromString(parameter["uuid"])
assert operationTime.startDate.present
assert operationTime.startDate.get() == ZonedDateTime.parse(parameter["operatesfrom"])
assert operationTime.endDate.present
assert operationTime.endDate.get() == ZonedDateTime.parse(parameter["operatesuntil"])
assert operator == OperatorInput.NO_OPERATOR_ASSIGNED
assert id == parameter["id"]
}
}
def "An AssetInputFactory should throw an exception on invalid or incomplete data "() {
given: "a system participant input type factory and model data"
def inputFactory = new TestAssetInputFactory()
Map<String, String> parameter = [
"uuid" : "91ec3bcf-1777-4d38-af67-0bf7c9fa73c7",
"operatesfrom" : "2019-01-01T00:00:00+01:00[Europe/Berlin]",
"operatesuntil": "2019-12-31T00:00:00+01:00[Europe/Berlin]"
]
def inputClass = TestAssetInput
when:
Try<AssetInput, FactoryException> input = inputFactory.get(new AssetInputEntityData(parameter, inputClass))
then:
input.failure
input.exception.get().cause.message ==
"The provided fields [operatesfrom, operatesuntil, uuid] with data \n" +
"{operatesfrom -> 2019-01-01T00:00:00+01:00[Europe/Berlin],\n" +
"operatesuntil -> 2019-12-31T00:00:00+01:00[Europe/Berlin],\n" +
"uuid -> 91ec3bcf-1777-4d38-af67-0bf7c9fa73c7} are invalid for instance of TestAssetInput. \n" +
"The following fields (without complex objects e.g. nodes, operators, ...) to be passed to a constructor of 'TestAssetInput' are possible (NOT case-sensitive!):\n" +
"0: [id, uuid]\n" +
"1: [id, operatesfrom, uuid]\n" +
"2: [id, operatesuntil, uuid]\n" +
"3: [id, operatesfrom, operatesuntil, uuid]\n"
}
private static class TestAssetInput extends AssetInput {
TestAssetInput(UUID uuid, String id, OperatorInput operator, OperationTime operationTime) {
super(uuid, id, operator, operationTime)
}
}
private static class TestAssetInputFactory extends AssetInputEntityFactory<TestAssetInput, AssetInputEntityData> {
TestAssetInputFactory() {
super(TestAssetInput)
}
@Override
protected String[] getAdditionalFields() {
return new String[0]
}
@Override
protected TestAssetInput buildModel(AssetInputEntityData data, UUID uuid, String id, OperatorInput operator, OperationTime operationTime) {
return new TestAssetInput(uuid, id, operator, operationTime)
}
}
}