Search in sources :

Example 1 with NetworkBindingConfiguration

use of org.openhab.binding.network.internal.NetworkBindingConfiguration 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 2 with NetworkBindingConfiguration

use of org.openhab.binding.network.internal.NetworkBindingConfiguration 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)

Example 3 with NetworkBindingConfiguration

use of org.openhab.binding.network.internal.NetworkBindingConfiguration in project openhab-addons by openhab.

the class NetworkHandlerTest method checkAllConfigurations.

@Test
public void checkAllConfigurations() {
    NetworkBindingConfiguration config = new NetworkBindingConfiguration();
    NetworkHandler handler = spy(new NetworkHandler(thing, true, config));
    handler.setCallback(callback);
    // Provide all possible configuration
    when(thing.getConfiguration()).thenAnswer(a -> {
        Configuration conf = new Configuration();
        conf.put(NetworkBindingConstants.PARAMETER_RETRY, 10);
        conf.put(NetworkBindingConstants.PARAMETER_HOSTNAME, "127.0.0.1");
        conf.put(NetworkBindingConstants.PARAMETER_PORT, 8080);
        conf.put(NetworkBindingConstants.PARAMETER_REFRESH_INTERVAL, 101010);
        conf.put(NetworkBindingConstants.PARAMETER_TIMEOUT, 1234);
        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);
    assertThat(handler.retries, is(10));
    assertThat(presenceDetection.getHostname(), is("127.0.0.1"));
    assertThat(presenceDetection.getServicePorts().iterator().next(), is(8080));
    assertThat(presenceDetection.getRefreshInterval(), is(101010L));
    assertThat(presenceDetection.getTimeout(), is(1234));
}
Also used : Configuration(org.openhab.core.config.core.Configuration) NetworkBindingConfiguration(org.openhab.binding.network.internal.NetworkBindingConfiguration) NetworkBindingConfiguration(org.openhab.binding.network.internal.NetworkBindingConfiguration) PresenceDetection(org.openhab.binding.network.internal.PresenceDetection) Test(org.junit.jupiter.api.Test) JavaTest(org.openhab.core.test.java.JavaTest)

Aggregations

Test (org.junit.jupiter.api.Test)3 NetworkBindingConfiguration (org.openhab.binding.network.internal.NetworkBindingConfiguration)3 PresenceDetection (org.openhab.binding.network.internal.PresenceDetection)3 Configuration (org.openhab.core.config.core.Configuration)3 JavaTest (org.openhab.core.test.java.JavaTest)3 ThingStatusInfo (org.openhab.core.thing.ThingStatusInfo)2 PresenceDetectionValue (org.openhab.binding.network.internal.PresenceDetectionValue)1 QuantityType (org.openhab.core.library.types.QuantityType)1 ChannelUID (org.openhab.core.thing.ChannelUID)1