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());
}
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)));
}
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));
}
Aggregations