2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.network.internal.handler;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertEquals;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.ArgumentMatchers.eq;
20 import static org.mockito.Mockito.*;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.mockito.junit.jupiter.MockitoSettings;
29 import org.mockito.quality.Strictness;
30 import org.openhab.binding.network.internal.NetworkBindingConfiguration;
31 import org.openhab.binding.network.internal.NetworkBindingConstants;
32 import org.openhab.binding.network.internal.PresenceDetection;
33 import org.openhab.binding.network.internal.PresenceDetectionValue;
34 import org.openhab.core.config.core.Configuration;
35 import org.openhab.core.library.types.OnOffType;
36 import org.openhab.core.library.types.QuantityType;
37 import org.openhab.core.test.java.JavaTest;
38 import org.openhab.core.thing.ChannelUID;
39 import org.openhab.core.thing.Thing;
40 import org.openhab.core.thing.ThingStatus;
41 import org.openhab.core.thing.ThingStatusDetail;
42 import org.openhab.core.thing.ThingStatusInfo;
43 import org.openhab.core.thing.ThingUID;
44 import org.openhab.core.thing.binding.ThingHandlerCallback;
47 * Tests cases for {@link NetworkHandler}.
49 * @author David Graeff - Initial contribution
51 @ExtendWith(MockitoExtension.class)
52 @MockitoSettings(strictness = Strictness.LENIENT)
53 public class NetworkHandlerTest extends JavaTest {
54 private ThingUID thingUID = new ThingUID("network", "ttype", "ping");
56 private @Mock ThingHandlerCallback callback;
57 private @Mock Thing thing;
61 when(thing.getUID()).thenReturn(thingUID);
65 public void checkAllConfigurations() {
66 NetworkBindingConfiguration config = new NetworkBindingConfiguration();
67 NetworkHandler handler = spy(new NetworkHandler(thing, true, config));
68 handler.setCallback(callback);
69 // Provide all possible configuration
70 when(thing.getConfiguration()).thenAnswer(a -> {
71 Configuration conf = new Configuration();
72 conf.put(NetworkBindingConstants.PARAMETER_RETRY, 10);
73 conf.put(NetworkBindingConstants.PARAMETER_HOSTNAME, "127.0.0.1");
74 conf.put(NetworkBindingConstants.PARAMETER_PORT, 8080);
75 conf.put(NetworkBindingConstants.PARAMETER_REFRESH_INTERVAL, 101010);
76 conf.put(NetworkBindingConstants.PARAMETER_TIMEOUT, 1234);
79 PresenceDetection presenceDetection = spy(new PresenceDetection(handler, 2000));
80 // Mock start/stop automatic refresh
81 doNothing().when(presenceDetection).startAutomaticRefresh(any());
82 doNothing().when(presenceDetection).stopAutomaticRefresh();
84 handler.initialize(presenceDetection);
85 assertThat(handler.retries, is(10));
86 assertThat(presenceDetection.getHostname(), is("127.0.0.1"));
87 assertThat(presenceDetection.getServicePorts().iterator().next(), is(8080));
88 assertThat(presenceDetection.getRefreshInterval(), is(101010L));
89 assertThat(presenceDetection.getTimeout(), is(1234));
93 public void tcpDeviceInitTests() {
94 NetworkBindingConfiguration config = new NetworkBindingConfiguration();
95 NetworkHandler handler = spy(new NetworkHandler(thing, true, config));
96 assertThat(handler.isTCPServiceDevice(), is(true));
97 handler.setCallback(callback);
98 // Port is missing, should make the device OFFLINE
99 when(thing.getConfiguration()).thenAnswer(a -> {
100 Configuration conf = new Configuration();
101 conf.put(NetworkBindingConstants.PARAMETER_HOSTNAME, "127.0.0.1");
104 handler.initialize(new PresenceDetection(handler, 2000));
105 // Check that we are offline
106 ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
107 verify(callback).statusUpdated(eq(thing), statusInfoCaptor.capture());
108 assertThat(statusInfoCaptor.getValue().getStatus(), is(equalTo(ThingStatus.OFFLINE)));
109 assertThat(statusInfoCaptor.getValue().getStatusDetail(), is(equalTo(ThingStatusDetail.CONFIGURATION_ERROR)));
113 public void pingDeviceInitTests() {
114 NetworkBindingConfiguration config = new NetworkBindingConfiguration();
115 NetworkHandler handler = spy(new NetworkHandler(thing, false, config));
116 handler.setCallback(callback);
117 // Provide minimal configuration
118 when(thing.getConfiguration()).thenAnswer(a -> {
119 Configuration conf = new Configuration();
120 conf.put(NetworkBindingConstants.PARAMETER_HOSTNAME, "127.0.0.1");
123 PresenceDetection presenceDetection = spy(new PresenceDetection(handler, 2000));
124 // Mock start/stop automatic refresh
125 doNothing().when(presenceDetection).startAutomaticRefresh(any());
126 doNothing().when(presenceDetection).stopAutomaticRefresh();
128 handler.initialize(presenceDetection);
129 // Check that we are online
130 ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
131 verify(callback).statusUpdated(eq(thing), statusInfoCaptor.capture());
132 assertEquals(ThingStatus.ONLINE, statusInfoCaptor.getValue().getStatus());
135 PresenceDetectionValue value = mock(PresenceDetectionValue.class);
136 when(value.getLowestLatency()).thenReturn(10.0);
137 when(value.isReachable()).thenReturn(true);
138 when(value.getSuccessfulDetectionTypes()).thenReturn("TESTMETHOD");
140 // Partial result from the PresenceDetection object should affect the
141 // ONLINE and LATENCY channel
142 handler.partialDetectionResult(value);
143 verify(callback).stateUpdated(eq(new ChannelUID(thingUID, NetworkBindingConstants.CHANNEL_ONLINE)),
145 verify(callback).stateUpdated(eq(new ChannelUID(thingUID, NetworkBindingConstants.CHANNEL_LATENCY)),
146 eq(new QuantityType<>("10.0 ms")));
148 // Final result affects the LASTSEEN channel
149 when(value.isFinished()).thenReturn(true);
150 handler.finalDetectionResult(value);
151 verify(callback).stateUpdated(eq(new ChannelUID(thingUID, NetworkBindingConstants.CHANNEL_LASTSEEN)), any());