This repository has been archived by the owner on May 7, 2020. It is now read-only.
/
DiscoveryServiceRegistryOSGITest.groovy
503 lines (397 loc) · 20.8 KB
/
DiscoveryServiceRegistryOSGITest.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.config.setup.test.discovery
import static org.hamcrest.CoreMatchers.*
import static org.junit.Assert.*
import static org.junit.matchers.JUnitMatchers.*
import org.eclipse.smarthome.config.discovery.DiscoveryListener
import org.eclipse.smarthome.config.discovery.DiscoveryResult
import org.eclipse.smarthome.config.discovery.DiscoveryService
import org.eclipse.smarthome.config.discovery.DiscoveryServiceRegistry
import org.eclipse.smarthome.config.discovery.ScanListener
import org.eclipse.smarthome.config.discovery.inbox.Inbox
import org.eclipse.smarthome.config.discovery.internal.DiscoveryServiceRegistryImpl
import org.eclipse.smarthome.core.thing.ThingRegistry
import org.eclipse.smarthome.core.thing.ThingTypeUID
import org.eclipse.smarthome.core.thing.ThingUID
import org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder
import org.eclipse.smarthome.test.AsyncResultWrapper
import org.eclipse.smarthome.test.OSGiTest
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.osgi.framework.ServiceRegistration
/**
* The {@link DiscoveryServiceRegistryOSGITest} test checks if the concrete
* {@link DiscoveryServiceRegistry} implementation tracks all registered
* {@link DiscoveryService}s and if a registered listener is notified
* about the events fired by the according{@link DiscoveryService}.
* <p>
* This implementation creates two {@link DiscoveryService} mocks and registers
* them as service at the <i>OSGi</i> service registry. Since this test creates
* {@link DiscoveryResult}s which are added to the {@link Inbox},
* the {@link Inbox} is cleared again after this test returns.
*
* @author Michael Grammling - Initial Contribution
* @author Simon Kaufmann - added tests for ExtendedDiscoveryService
*/
class DiscoveryServiceRegistryOSGITest extends OSGiTest {
def ANY_BINDING_ID_1 = 'anyBindingId1'
def ANY_THING_TYPE_1 = 'anyThingType1'
def ANY_BINDING_ID_2 = 'anyBindingId2'
def ANY_THING_TYPE_2 = 'anyThingType2'
def FAULTY_BINDING_ID = 'faultyBindingId'
def FAULTY_THING_TYPE = 'faultyThingType'
def EXTENDED_BINDING_ID = 'extendedBindingId'
def EXTENDED_THING_TYPE = 'extendedThingType'
class AnotherDiscoveryService extends DiscoveryServiceMock {
public AnotherDiscoveryService(Object thingType, int timeout) {
super(thingType, timeout);
}
}
DiscoveryService discoveryServiceMockForBinding1
DiscoveryService discoveryServiceMockForBinding2
DiscoveryService discoveryServiceFaultyMock
ExtendedDiscoveryServiceMock extendedDiscoveryServiceMock
DiscoveryServiceRegistry discoveryServiceRegistry
List<ServiceRegistration<?>> serviceRegs = []
ThingRegistry thingRegistry
Inbox inbox
@Before
void setUp() {
registerVolatileStorageService()
thingRegistry = getService(ThingRegistry)
assertNotNull thingRegistry
inbox = getService(Inbox)
assertNotNull(inbox)
discoveryServiceMockForBinding1 = new DiscoveryServiceMock(
new ThingTypeUID(ANY_BINDING_ID_1,ANY_THING_TYPE_1), 1)
discoveryServiceMockForBinding2 = new ExtendedDiscoveryServiceMock(
new ThingTypeUID(ANY_BINDING_ID_2,ANY_THING_TYPE_2), 3)
discoveryServiceFaultyMock = new DiscoveryServiceMock(
new ThingTypeUID(FAULTY_BINDING_ID, FAULTY_THING_TYPE), 1, true)
extendedDiscoveryServiceMock = new ExtendedDiscoveryServiceMock(
new ThingTypeUID(EXTENDED_BINDING_ID, EXTENDED_THING_TYPE), 1, true)
serviceRegs.add(registerService(discoveryServiceMockForBinding1, DiscoveryService.class.name))
serviceRegs.add(registerService(discoveryServiceMockForBinding2, DiscoveryService.class.name))
serviceRegs.add(registerService(discoveryServiceFaultyMock, DiscoveryService.class.name))
serviceRegs.add(registerService(extendedDiscoveryServiceMock, DiscoveryService.class.name))
discoveryServiceRegistry = getService(DiscoveryServiceRegistry)
}
@After
void cleanUp() {
extendedDiscoveryServiceMock.abortScan()
discoveryServiceFaultyMock.abortScan()
discoveryServiceMockForBinding1.abortScan()
discoveryServiceMockForBinding2.abortScan()
serviceRegs.each {
try {
it.unregister()
} catch(Exception ex) {
}
}
((DiscoveryServiceRegistryImpl)discoveryServiceRegistry).listeners.each { discoveryServiceRegistry.removeDiscoveryListener(it) }
Inbox inbox = getService(Inbox)
DiscoveryResult[] discoveryResults = inbox.getAll()
discoveryResults.each { inbox.remove(it.getThingUID()) }
}
@Test
void 'assert that an not existing DiscoveryService can not be forced for a discovery' () {
boolean state
state = discoveryServiceRegistry.startScan(new ThingTypeUID('bindingId','thingType'), [] as ScanListener)
assertFalse(state)
}
@Test
void 'assert that a known DiscoveryService can be forced for a discovery' () {
boolean state
state = discoveryServiceRegistry.startScan(new ThingTypeUID(ANY_BINDING_ID_1,ANY_THING_TYPE_1), [onFinished: {}, onErrorOccurred: {}] as ScanListener)
assertTrue(state)
}
@Test
void 'assert that a faulty known DiscoveryService cannot be forced for a discovery' () {
boolean state
state = discoveryServiceRegistry.startScan(new ThingTypeUID(FAULTY_BINDING_ID,FAULTY_THING_TYPE), [] as ScanListener)
assertFalse(state)
}
@Test
void 'assert that a discovery cannot be aborted for a not existing DiscoveryService' () {
boolean state
state = discoveryServiceRegistry.abortScan(new ThingTypeUID('bindingId','thingType'))
assertFalse(state)
}
@Test
void 'assert that a discovery can be aborted for a known DiscoveryService' () {
def onErrorOccurredCalled = false
boolean state
state = discoveryServiceRegistry.startScan(new ThingTypeUID(ANY_BINDING_ID_1,ANY_THING_TYPE_1), [
onErrorOccurred: {onErrorOccurredCalled = true}
] as ScanListener)
assertTrue(state)
state = discoveryServiceRegistry.abortScan(new ThingTypeUID(ANY_BINDING_ID_1,ANY_THING_TYPE_1))
assertTrue(state)
waitForAssert { assertTrue onErrorOccurredCalled }
}
@Test
void 'assert that an added listener is notified about DiscoveryResults' () {
AsyncResultWrapper<Boolean> scanListenerResult = new AsyncResultWrapper<Boolean>()
AsyncResultWrapper<DiscoveryResult> discoveryListenerResult = new AsyncResultWrapper<Boolean>()
DiscoveryListener discoveryListenerMock = [
thingDiscovered: { DiscoveryService source, DiscoveryResult result ->
discoveryListenerResult.set result
}
] as DiscoveryListener
discoveryServiceRegistry.addDiscoveryListener(discoveryListenerMock);
discoveryServiceRegistry.startScan(new ThingTypeUID(ANY_BINDING_ID_1, ANY_THING_TYPE_1), [
onFinished: {scanListenerResult.set(Void.TYPE)},
onErrorOccurred: {
}
] as ScanListener)
waitForAssert({ assertTrue scanListenerResult.isSet }, 2000)
assertTrue discoveryListenerResult.isSet
}
@Test
void 'assert that an listener is notified about removeOlderResults' () {
boolean removeOlderResultsCalled = false;
DiscoveryListener discoveryListenerMock = [
removeOlderResults: { DiscoveryService src, long timestamp, Collection<ThingTypeUID> thingTypeIds ->
removeOlderResultsCalled = true
}
] as DiscoveryListener
discoveryServiceRegistry.addDiscoveryListener(discoveryListenerMock);
discoveryServiceMockForBinding1.removeOlderResults(discoveryServiceMockForBinding1.getTimestampOfLastScan());
assertTrue removeOlderResultsCalled
}
@Test
void 'assert that removeOlderResults works as expected' () {
AsyncResultWrapper<Boolean> scanListenerResult1 = new AsyncResultWrapper<Boolean>()
AsyncResultWrapper<Boolean> scanListenerResult2 = new AsyncResultWrapper<Boolean>()
AsyncResultWrapper<DiscoveryResult> discoveryListenerResult = new AsyncResultWrapper<Boolean>()
DiscoveryListener discoveryListenerMock = [
thingDiscovered: { DiscoveryService source, DiscoveryResult result ->
discoveryListenerResult.set result
},
removeOlderThings: { DiscoveryService source, long timestamp, Collection<ThingTypeUID> thingTypeUIDs ->
[]
}
] as DiscoveryListener
discoveryServiceRegistry.addDiscoveryListener(discoveryListenerMock);
discoveryServiceRegistry.startScan(new ThingTypeUID(ANY_BINDING_ID_1, ANY_THING_TYPE_1), [
onFinished: {
scanListenerResult1.set(Void.TYPE)
},
onErrorOccurred: {
}
] as ScanListener)
discoveryServiceRegistry.startScan(new ThingTypeUID(ANY_BINDING_ID_2, ANY_THING_TYPE_2), [
onFinished: {
scanListenerResult2.set(Void.TYPE)
},
onErrorOccurred: {
}
] as ScanListener)
waitForAssert({ assertTrue scanListenerResult1.isSet }, 2000)
waitForAssert({ assertTrue scanListenerResult2.isSet }, 2000)
assertTrue discoveryListenerResult.isSet
Inbox inbox = getService(Inbox)
assertThat inbox.getAll().size(), is(2)
// should not remove anything
discoveryServiceMockForBinding1.removeOlderResults(discoveryServiceMockForBinding1.getTimestampOfLastScan())
assertThat inbox.getAll().size(), is(2)
// start discovery again
discoveryServiceRegistry.startScan(new ThingTypeUID(ANY_BINDING_ID_1, ANY_THING_TYPE_1), [
onFinished: {scanListenerResult1.set(Void.TYPE)},
onErrorOccurred: {
}
] as ScanListener)
waitForAssert({ assertTrue scanListenerResult1.isSet }, 2000)
assertTrue discoveryListenerResult.isSet
assertThat inbox.getAll().size(), is(3)
// should remove one entry
discoveryServiceMockForBinding1.removeOlderResults(discoveryServiceMockForBinding1.getTimestampOfLastScan())
assertThat inbox.getAll().size(), is(2)
}
@Test
void 'assert that removeOlderResults removes only the entries of the same discovery service' () {
AsyncResultWrapper<Boolean> scanListenerResult1 = new AsyncResultWrapper<Boolean>()
AsyncResultWrapper<DiscoveryResult> discoveryListenerResult = new AsyncResultWrapper<Boolean>()
DiscoveryListener discoveryListenerMock = [
thingDiscovered: { DiscoveryService source, DiscoveryResult result ->
discoveryListenerResult.set result
},
removeOlderThings: { DiscoveryService source, long timestamp, Collection<ThingTypeUID> thingTypeUIDs ->
[]
}
] as DiscoveryListener
discoveryServiceRegistry.addDiscoveryListener(discoveryListenerMock);
discoveryServiceRegistry.startScan(new ThingTypeUID(ANY_BINDING_ID_1, ANY_THING_TYPE_1), [
onFinished: {
scanListenerResult1.set(Void.TYPE)
},
onErrorOccurred: {
}
] as ScanListener)
waitForAssert({ assertTrue scanListenerResult1.isSet }, 2000)
assertTrue discoveryListenerResult.isSet
Inbox inbox = getService(Inbox)
assertThat inbox.getAll().size(), is(1)
// register another discovery service for the same thing type
AnotherDiscoveryService anotherDiscoveryServiceMockForBinding1 = new AnotherDiscoveryService(
new ThingTypeUID(ANY_BINDING_ID_1,ANY_THING_TYPE_1), 1)
serviceRegs.add(registerService(anotherDiscoveryServiceMockForBinding1, DiscoveryService.class.name))
// start discovery again
discoveryServiceRegistry.startScan(new ThingTypeUID(ANY_BINDING_ID_1, ANY_THING_TYPE_1), [
onFinished: {scanListenerResult1.set(Void.TYPE)},
onErrorOccurred: {
}
] as ScanListener)
waitForAssert({ assertTrue scanListenerResult1.isSet }, 2000)
assertTrue discoveryListenerResult.isSet
assertThat inbox.getAll().size(), is(3)
// should remove no entry, as there is no older entry discovery of this specific discovery service
anotherDiscoveryServiceMockForBinding1.removeOlderResults(anotherDiscoveryServiceMockForBinding1.getTimestampOfLastScan())
assertThat inbox.getAll().size(), is(3)
// should remove only one entry
discoveryServiceMockForBinding1.removeOlderResults(discoveryServiceMockForBinding1.getTimestampOfLastScan())
assertThat inbox.getAll().size(), is(2)
anotherDiscoveryServiceMockForBinding1.abortScan()
}
@Test
void 'assert that a removed listener is not notified about DiscoveryResults anymore' () {
AsyncResultWrapper<DiscoveryResult> discoveryResult = new AsyncResultWrapper<Boolean>()
boolean discoveryFinished = false;
DiscoveryListener discoveryListenerMock = [
thingDiscovered: { DiscoveryService source, DiscoveryResult result ->
discoveryResult.set result
}
] as DiscoveryListener
discoveryServiceRegistry.addDiscoveryListener(discoveryListenerMock);
discoveryServiceRegistry.removeDiscoveryListener(discoveryListenerMock);
discoveryServiceRegistry.startScan(new ThingTypeUID(ANY_BINDING_ID_1, ANY_THING_TYPE_1), [
onFinished: { discoveryFinished = true },
onErrorOccurred: {
}
] as ScanListener)
waitForAssert ({ assertTrue(discoveryFinished) }, 2000 )
assertFalse discoveryResult.isSet
}
@Test
void 'assert that two discovery services are started' () {
def anotherDiscoveryServiceMock = new DiscoveryServiceMock(
new ThingTypeUID(ANY_BINDING_ID_1,ANY_THING_TYPE_1), 1)
serviceRegs.add(registerService(anotherDiscoveryServiceMock, DiscoveryService.class.name))
def numberOfDiscoveredThings = 0;
AsyncResultWrapper<Boolean> listenerResult = new AsyncResultWrapper<Boolean>()
DiscoveryListener discoveryListenerMock = [
thingDiscovered: { DiscoveryService source, DiscoveryResult result -> numberOfDiscoveredThings++}
] as DiscoveryListener
discoveryServiceRegistry.addDiscoveryListener(discoveryListenerMock);
discoveryServiceRegistry.startScan(new ThingTypeUID(ANY_BINDING_ID_1, ANY_THING_TYPE_1), [
onFinished: {listenerResult.set(true)},
onErrorOccurred: {
}
] as ScanListener)
waitForAssert ({ assertTrue(listenerResult.isSet) }, 2000 )
assertEquals 2, numberOfDiscoveredThings
}
@Test
void 'assert start discovery for binding id works' () {
AsyncResultWrapper<Boolean> listenerResult = new AsyncResultWrapper<Boolean>()
discoveryServiceRegistry.startScan(ANY_BINDING_ID_1, [
onFinished: {listenerResult.set(true)},
onErrorOccurred: {
}
] as ScanListener)
waitForAssert ({ assertTrue(listenerResult.isSet) }, 2000 )
}
@Test
void 'assert supportsDiscovery works' () {
assertTrue discoveryServiceRegistry.supportsDiscovery(new ThingTypeUID(ANY_BINDING_ID_1, ANY_THING_TYPE_1))
assertFalse discoveryServiceRegistry.supportsDiscovery(new ThingTypeUID(ANY_BINDING_ID_1, 'unknownType'))
assertTrue discoveryServiceRegistry.supportsDiscovery(ANY_BINDING_ID_1)
assertFalse discoveryServiceRegistry.supportsDiscovery('unknownBindingId')
}
@Test
void 'assert getMaxScanTimeout works' () {
assertEquals 1, discoveryServiceRegistry.getMaxScanTimeout(new ThingTypeUID(ANY_BINDING_ID_1, ANY_THING_TYPE_1))
assertEquals 0, discoveryServiceRegistry.getMaxScanTimeout(new ThingTypeUID(ANY_BINDING_ID_1, 'unknownType'))
assertEquals 3, discoveryServiceRegistry.getMaxScanTimeout(ANY_BINDING_ID_2)
assertEquals 0, discoveryServiceRegistry.getMaxScanTimeout('unknownBindingId')
}
@Test
void 'assert an existing Thing can be accessed by ExtendedDiscoveryService implementations'() {
ThingUID thingUID = new ThingUID(EXTENDED_BINDING_ID, "foo")
// verify that the callback has been set
assertNotNull extendedDiscoveryServiceMock.discoveryServiceCallback
// verify that the thing cannot be found if it's not there
assertNull extendedDiscoveryServiceMock.discoveryServiceCallback.getExistingThing(thingUID)
thingRegistry.add(ThingBuilder.create(new ThingTypeUID(EXTENDED_BINDING_ID, EXTENDED_THING_TYPE), thingUID).build())
// verify that the existing Thing can be accessed
assertNotNull extendedDiscoveryServiceMock.discoveryServiceCallback.getExistingThing(thingUID)
}
@Test
void 'assert that existing DiscoveryResults can be accessed by ExtendedDiscoveryService implementations'() {
ThingUID thingUID = new ThingUID(EXTENDED_BINDING_ID, EXTENDED_THING_TYPE, "foo")
// verify that the callback has been set
assertNotNull extendedDiscoveryServiceMock.discoveryServiceCallback
// verify that the DiscoveryResult cannot be found if it's not there
assertNull extendedDiscoveryServiceMock.discoveryServiceCallback.getExistingDiscoveryResult(thingUID)
boolean discoveryFinished = false;
discoveryServiceRegistry.startScan(new ThingTypeUID(EXTENDED_BINDING_ID, EXTENDED_THING_TYPE), [
onFinished: { discoveryFinished = true },
onErrorOccurred: {
}
] as ScanListener)
waitForAssert ({ assertTrue(discoveryFinished) }, 2000 )
// verify that the existing DiscoveryResult can be accessed
assertNotNull extendedDiscoveryServiceMock.discoveryServiceCallback.getExistingDiscoveryResult(thingUID)
// verify that a non-existing DiscoveryResult can not be accessed
thingUID = new ThingUID(EXTENDED_BINDING_ID, EXTENDED_THING_TYPE, "bar")
assertNull extendedDiscoveryServiceMock.discoveryServiceCallback.getExistingDiscoveryResult(thingUID)
}
@Test
void 'assert that updated discovery result is stored properly in cache'() {
ThingUID thingUID = new ThingUID(EXTENDED_BINDING_ID, EXTENDED_THING_TYPE, "foo")
ThingTypeUID thingTypeUID = new ThingTypeUID(EXTENDED_BINDING_ID, EXTENDED_THING_TYPE);
ScanListener scanListener = new ScanListener() {
public boolean discoveryFinished = false;
@Override
void onFinished() {
discoveryFinished = true;
}
@Override
void onErrorOccurred(Exception exception) {}
}
AsyncResultWrapper<DiscoveryResult> discoveryListenerResult = new AsyncResultWrapper<DiscoveryResult>()
DiscoveryListener discoveryListenerMock = [
thingDiscovered: { DiscoveryService source, DiscoveryResult result ->
discoveryListenerResult.set result
},
removeOlderThings: { DiscoveryService source, long timestamp, Collection<ThingTypeUID> thingTypeUIDs ->
[]
}
] as DiscoveryListener;
inbox.remove(thingUID);
boolean discoveryFinished = false;
discoveryServiceRegistry.startScan(thingTypeUID, scanListener);
waitForAssert ({ assertTrue(scanListener.discoveryFinished) });
discoveryServiceRegistry.addDiscoveryListener(discoveryListenerMock);
waitForAssert ({ assertNotNull(discoveryListenerResult.getWrappedObject()) });
assertEquals [:], discoveryListenerResult.getWrappedObject().getProperties();
discoveryServiceRegistry.removeDiscoveryListener(discoveryListenerMock);
discoveryListenerResult.set null;
Map<String, Object> props = ["ip":"0.0.0.0"];
extendedDiscoveryServiceMock.setDiscoveryProperties(props);
inbox.remove(thingUID);
scanListener.discoveryFinished = false;
discoveryServiceRegistry.startScan(thingTypeUID, scanListener);
waitForAssert ({ assertTrue(scanListener.discoveryFinished) });
discoveryServiceRegistry.addDiscoveryListener(discoveryListenerMock);
waitForAssert ({ assertNotNull(discoveryListenerResult.getWrappedObject()) });
assertEquals props, discoveryListenerResult.getWrappedObject().getProperties();
}
}