use of org.eclipse.smarthome.core.thing.ThingTypeUID in project smarthome by eclipse.
the class DiscoveryServiceRegistryOSGiTest method testCache.
@Test
public void testCache() {
ScanListener mockScanListener1 = mock(ScanListener.class);
ArgumentCaptor<DiscoveryResult> discoveryResultCaptor = ArgumentCaptor.forClass(DiscoveryResult.class);
ThingUID thingUID = new ThingUID(EXTENDED_BINDING_ID, EXTENDED_THING_TYPE, "foo");
ThingTypeUID thingTypeUID = new ThingTypeUID(EXTENDED_BINDING_ID, EXTENDED_THING_TYPE);
inbox.remove(thingUID);
discoveryServiceRegistry.startScan(thingTypeUID, mockScanListener1);
waitForAssert(() -> verify(mockScanListener1, times(1)).onFinished());
discoveryServiceRegistry.addDiscoveryListener(mockDiscoveryListener);
waitForAssert(() -> {
verify(mockDiscoveryListener, times(1)).thingDiscovered(any(), discoveryResultCaptor.capture());
});
assertTrue(discoveryResultCaptor.getValue().getProperties().isEmpty());
discoveryServiceRegistry.removeDiscoveryListener(mockDiscoveryListener);
extendedDiscoveryServiceMock.setDiscoveryProperties(Collections.singletonMap("ip", "0.0.0.0"));
inbox.remove(thingUID);
discoveryServiceRegistry.startScan(thingTypeUID, mockScanListener1);
waitForAssert(() -> verify(mockScanListener1, times(2)).onFinished());
discoveryServiceRegistry.addDiscoveryListener(mockDiscoveryListener);
waitForAssert(() -> {
verify(mockDiscoveryListener, times(2)).thingDiscovered(any(), discoveryResultCaptor.capture());
});
assertEquals(Collections.singletonMap("ip", "0.0.0.0"), discoveryResultCaptor.getValue().getProperties());
}
use of org.eclipse.smarthome.core.thing.ThingTypeUID in project smarthome by eclipse.
the class DiscoveryServiceRegistryOSGiTest method testStartScan_twoDiscoveryServices.
@Test
public void testStartScan_twoDiscoveryServices() {
ScanListener mockScanListener1 = mock(ScanListener.class);
DiscoveryService anotherDiscoveryServiceMock = new DiscoveryServiceMock(new ThingTypeUID(ANY_BINDING_ID_1, ANY_THING_TYPE_1), 1);
serviceRegs.add(bundleContext.registerService(DiscoveryService.class.getName(), anotherDiscoveryServiceMock, null));
discoveryServiceRegistry.addDiscoveryListener(mockDiscoveryListener);
discoveryServiceRegistry.startScan(new ThingTypeUID(ANY_BINDING_ID_1, ANY_THING_TYPE_1), mockScanListener1);
waitForAssert(() -> mockScanListener1.onFinished(), 2000, DFL_SLEEP_TIME);
verify(mockDiscoveryListener, times(2)).thingDiscovered(any(), any());
}
use of org.eclipse.smarthome.core.thing.ThingTypeUID in project smarthome by eclipse.
the class DiscoveryServiceRegistryOSGiTest method testRemoveOlderResults_onlySameService.
@Test
public void testRemoveOlderResults_onlySameService() {
mockDiscoveryListener = mock(DiscoveryListener.class);
ScanListener mockScanListener1 = mock(ScanListener.class);
discoveryServiceRegistry.addDiscoveryListener(mockDiscoveryListener);
discoveryServiceRegistry.startScan(new ThingTypeUID(ANY_BINDING_ID_1, ANY_THING_TYPE_1), mockScanListener1);
waitForAssert(() -> verify(mockScanListener1, times(1)).onFinished(), 2000, DFL_SLEEP_TIME);
verify(mockDiscoveryListener, times(1)).thingDiscovered(any(), any());
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(bundleContext.registerService(DiscoveryService.class.getName(), anotherDiscoveryServiceMockForBinding1, null));
// start discovery again
discoveryServiceRegistry.startScan(new ThingTypeUID(ANY_BINDING_ID_1, ANY_THING_TYPE_1), mockScanListener1);
waitForAssert(() -> verify(mockScanListener1, times(2)).onFinished(), 2000, DFL_SLEEP_TIME);
verify(mockDiscoveryListener, times(3)).thingDiscovered(any(), any());
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();
}
use of org.eclipse.smarthome.core.thing.ThingTypeUID in project smarthome by eclipse.
the class DiscoveryServiceRegistryOSGiTest method testGetExistingDiscoveryResult.
@Test
public void testGetExistingDiscoveryResult() {
ScanListener mockScanListener1 = mock(ScanListener.class);
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));
discoveryServiceRegistry.startScan(new ThingTypeUID(EXTENDED_BINDING_ID, EXTENDED_THING_TYPE), mockScanListener1);
waitForAssert(() -> verify(mockScanListener1, times(1)).onFinished(), 2000, DFL_SLEEP_TIME);
// 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));
}
use of org.eclipse.smarthome.core.thing.ThingTypeUID in project smarthome by eclipse.
the class AbstractDiscoveryService method removeOlderResults.
/**
* Call to remove all results of the given types that are older than the
* given timestamp. To remove all left over results after a full scan, this
* method could be called {@link #getTimestampOfLastScan()} as timestamp.
*
* @param timestamp timestamp, older results will be removed
* @param thingTypeUIDs collection of {@code ThingType}s, only results of these
* {@code ThingType}s will be removed; if {@code null} then
* {@link DiscoveryService#getSupportedThingTypes()} will be used
* instead
* @param bridgeUID if not {@code null} only results of that bridge are being removed
*/
protected void removeOlderResults(long timestamp, @Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable ThingUID bridgeUID) {
Collection<ThingUID> removedThings = null;
Collection<ThingTypeUID> toBeRemoved = thingTypeUIDs != null ? thingTypeUIDs : getSupportedThingTypes();
for (DiscoveryListener discoveryListener : discoveryListeners) {
try {
removedThings = discoveryListener.removeOlderResults(this, timestamp, toBeRemoved, bridgeUID);
} catch (Exception e) {
logger.error("An error occurred while calling the discovery listener {}.", discoveryListener.getClass().getName(), e);
}
}
if (removedThings != null) {
synchronized (cachedResults) {
for (ThingUID uid : removedThings) {
cachedResults.remove(uid);
}
}
}
}
Aggregations