Search in sources :

Example 11 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-addons by openhab.

the class OwserverBridgeHandlerTest method testInitializationReportsNotRefreshableOnFailedConnection.

@Test
public void testInitializationReportsNotRefreshableOnFailedConnection() {
    final OwserverBridgeHandler bridgeHandler = this.bridgeHandler;
    if (bridgeHandler == null) {
        fail("bridgeHandler is null");
        return;
    }
    Mockito.doAnswer(answer -> {
        bridgeHandler.reportConnectionState(OwserverConnectionState.FAILED);
        return null;
    }).when(owserverConnection).start();
    bridgeHandler.initialize();
    ArgumentCaptor<ThingStatusInfo> statusCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
    waitForAssert(() -> {
        verify(thingHandlerCallback, times(2)).statusUpdated(eq(bridge), statusCaptor.capture());
    });
    assertThat(statusCaptor.getAllValues().get(0).getStatus(), is(ThingStatus.UNKNOWN));
    assertThat(statusCaptor.getAllValues().get(1).getStatus(), is(ThingStatus.OFFLINE));
    waitForAssert(() -> assertFalse(bridgeHandler.isRefreshable()));
}
Also used : OwserverBridgeHandler(org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) Test(org.junit.jupiter.api.Test) JavaTest(org.openhab.core.test.java.JavaTest)

Example 12 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-addons by openhab.

the class WWNAccountHandlerTest method initializeShouldCallTheCallback.

@SuppressWarnings("null")
@Test
public void initializeShouldCallTheCallback() {
    when(bridge.getConfiguration()).thenReturn(configuration);
    WWNAccountConfiguration bridgeConfig = new WWNAccountConfiguration();
    when(configuration.as(eq(WWNAccountConfiguration.class))).thenReturn(bridgeConfig);
    bridgeConfig.accessToken = "my token";
    // we expect the handler#initialize method to call the callback during execution and
    // pass it the thing and a ThingStatusInfo object containing the ThingStatus of the thing.
    handler.initialize();
    // the argument captor will capture the argument of type ThingStatusInfo given to the
    // callback#statusUpdated method.
    ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
    // verify the interaction with the callback and capture the ThingStatusInfo argument:
    verify(callback).statusUpdated(eq(bridge), statusInfoCaptor.capture());
    // assert that the ThingStatusInfo given to the callback was build with the UNKNOWN status:
    ThingStatusInfo thingStatusInfo = statusInfoCaptor.getValue();
    assertThat(thingStatusInfo.getStatus(), is(equalTo(ThingStatus.UNKNOWN)));
}
Also used : ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) WWNAccountConfiguration(org.openhab.binding.nest.internal.wwn.config.WWNAccountConfiguration) Test(org.junit.jupiter.api.Test)

Example 13 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-addons by openhab.

the class GenericThingHandlerTests method handleBridgeStatusChange.

@Test
public void handleBridgeStatusChange() {
    Configuration config = new Configuration();
    config.put("availabilityTopic", "test/LWT");
    when(thing.getConfiguration()).thenReturn(config);
    thingHandler.initialize();
    thingHandler.bridgeStatusChanged(new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, null));
    thingHandler.bridgeStatusChanged(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null));
    verify(connection, times(2)).subscribe(eq("test/LWT"), any());
}
Also used : Configuration(org.openhab.core.config.core.Configuration) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) Test(org.junit.jupiter.api.Test)

Example 14 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-addons by openhab.

the class NetworkHandlerTest method pingDeviceInitTests.

@Test
public void pingDeviceInitTests() {
    NetworkBindingConfiguration config = new NetworkBindingConfiguration();
    NetworkHandler handler = spy(new NetworkHandler(thing, false, config));
    handler.setCallback(callback);
    // Provide minimal configuration
    when(thing.getConfiguration()).thenAnswer(a -> {
        Configuration conf = new Configuration();
        conf.put(NetworkBindingConstants.PARAMETER_HOSTNAME, "127.0.0.1");
        return conf;
    });
    PresenceDetection presenceDetection = spy(new PresenceDetection(handler, 2000));
    // Mock start/stop automatic refresh
    doNothing().when(presenceDetection).startAutomaticRefresh(any());
    doNothing().when(presenceDetection).stopAutomaticRefresh();
    handler.initialize(presenceDetection);
    // Check that we are online
    ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
    verify(callback).statusUpdated(eq(thing), statusInfoCaptor.capture());
    assertEquals(ThingStatus.ONLINE, statusInfoCaptor.getValue().getStatus());
    // Mock result value
    PresenceDetectionValue value = mock(PresenceDetectionValue.class);
    when(value.getLowestLatency()).thenReturn(10.0);
    when(value.isReachable()).thenReturn(true);
    when(value.getSuccessfulDetectionTypes()).thenReturn("TESTMETHOD");
    // Partial result from the PresenceDetection object should affect the
    // ONLINE and LATENCY channel
    handler.partialDetectionResult(value);
    verify(callback).stateUpdated(eq(new ChannelUID(thingUID, NetworkBindingConstants.CHANNEL_ONLINE)), eq(OnOffType.ON));
    verify(callback).stateUpdated(eq(new ChannelUID(thingUID, NetworkBindingConstants.CHANNEL_LATENCY)), eq(new QuantityType<>("10.0 ms")));
    // Final result affects the LASTSEEN channel
    when(value.isFinished()).thenReturn(true);
    handler.finalDetectionResult(value);
    verify(callback).stateUpdated(eq(new ChannelUID(thingUID, NetworkBindingConstants.CHANNEL_LASTSEEN)), any());
}
Also used : Configuration(org.openhab.core.config.core.Configuration) NetworkBindingConfiguration(org.openhab.binding.network.internal.NetworkBindingConfiguration) NetworkBindingConfiguration(org.openhab.binding.network.internal.NetworkBindingConfiguration) QuantityType(org.openhab.core.library.types.QuantityType) ChannelUID(org.openhab.core.thing.ChannelUID) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) PresenceDetectionValue(org.openhab.binding.network.internal.PresenceDetectionValue) PresenceDetection(org.openhab.binding.network.internal.PresenceDetection) Test(org.junit.jupiter.api.Test) JavaTest(org.openhab.core.test.java.JavaTest)

Example 15 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-addons by openhab.

the class NetworkHandlerTest method tcpDeviceInitTests.

@Test
public void tcpDeviceInitTests() {
    NetworkBindingConfiguration config = new NetworkBindingConfiguration();
    NetworkHandler handler = spy(new NetworkHandler(thing, true, config));
    assertThat(handler.isTCPServiceDevice(), is(true));
    handler.setCallback(callback);
    // Port is missing, should make the device OFFLINE
    when(thing.getConfiguration()).thenAnswer(a -> {
        Configuration conf = new Configuration();
        conf.put(NetworkBindingConstants.PARAMETER_HOSTNAME, "127.0.0.1");
        return conf;
    });
    handler.initialize(new PresenceDetection(handler, 2000));
    // Check that we are offline
    ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
    verify(callback).statusUpdated(eq(thing), statusInfoCaptor.capture());
    assertThat(statusInfoCaptor.getValue().getStatus(), is(equalTo(ThingStatus.OFFLINE)));
    assertThat(statusInfoCaptor.getValue().getStatusDetail(), is(equalTo(ThingStatusDetail.CONFIGURATION_ERROR)));
}
Also used : Configuration(org.openhab.core.config.core.Configuration) NetworkBindingConfiguration(org.openhab.binding.network.internal.NetworkBindingConfiguration) NetworkBindingConfiguration(org.openhab.binding.network.internal.NetworkBindingConfiguration) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) PresenceDetection(org.openhab.binding.network.internal.PresenceDetection) Test(org.junit.jupiter.api.Test) JavaTest(org.openhab.core.test.java.JavaTest)

Aggregations

ThingStatusInfo (org.openhab.core.thing.ThingStatusInfo)101 Test (org.junit.jupiter.api.Test)58 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)33 Thing (org.openhab.core.thing.Thing)22 ThingHandler (org.openhab.core.thing.binding.ThingHandler)22 ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)20 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)17 ThingHandlerFactory (org.openhab.core.thing.binding.ThingHandlerFactory)15 Nullable (org.eclipse.jdt.annotation.Nullable)14 Configuration (org.openhab.core.config.core.Configuration)13 ThingUID (org.openhab.core.thing.ThingUID)13 InvocationOnMock (org.mockito.invocation.InvocationOnMock)12 Bridge (org.openhab.core.thing.Bridge)12 JavaTest (org.openhab.core.test.java.JavaTest)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 DiscoveryResult (org.openhab.core.config.discovery.DiscoveryResult)5 BaseThingHandler (org.openhab.core.thing.binding.BaseThingHandler)5 ArrayList (java.util.ArrayList)4 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)4