Search in sources :

Example 51 with ThingTypeUID

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());
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 52 with ThingTypeUID

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());
}
Also used : ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ExtendedDiscoveryServiceMock(org.eclipse.smarthome.config.setup.test.discovery.ExtendedDiscoveryServiceMock) DiscoveryServiceMock(org.eclipse.smarthome.config.setup.test.discovery.DiscoveryServiceMock) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 53 with ThingTypeUID

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();
}
Also used : ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 54 with ThingTypeUID

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));
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 55 with ThingTypeUID

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);
            }
        }
    }
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) CancellationException(java.util.concurrent.CancellationException)

Aggregations

ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)63 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)27 Test (org.junit.Test)27 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)25 Thing (org.eclipse.smarthome.core.thing.Thing)12 DiscoveryResult (org.eclipse.smarthome.config.discovery.DiscoveryResult)11 Locale (java.util.Locale)10 HashMap (java.util.HashMap)9 Collection (java.util.Collection)5 DiscoveryService (org.eclipse.smarthome.config.discovery.DiscoveryService)5 Before (org.junit.Before)5 Configuration (org.eclipse.smarthome.config.core.Configuration)4 DiscoveryListener (org.eclipse.smarthome.config.discovery.DiscoveryListener)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 Nullable (org.eclipse.jdt.annotation.Nullable)3 ThingRegistry (org.eclipse.smarthome.core.thing.ThingRegistry)3 BaseThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory)3 Firmware (org.eclipse.smarthome.core.thing.binding.firmware.Firmware)3 ArrayList (java.util.ArrayList)2