]> git.basschouten.com Git - openhab-addons.git/blob
6905437f76cb5c682ed21acc5d288819bfd03437
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.network.internal.handler;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.junit.Assert.*;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.ArgumentMatchers.eq;
19 import static org.mockito.Mockito.*;
20 import static org.mockito.MockitoAnnotations.initMocks;
21
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Mock;
27 import org.openhab.binding.network.internal.NetworkBindingConfiguration;
28 import org.openhab.binding.network.internal.NetworkBindingConstants;
29 import org.openhab.binding.network.internal.PresenceDetection;
30 import org.openhab.binding.network.internal.PresenceDetectionValue;
31 import org.openhab.core.config.core.Configuration;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.QuantityType;
34 import org.openhab.core.test.java.JavaTest;
35 import org.openhab.core.thing.ChannelUID;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingStatus;
38 import org.openhab.core.thing.ThingStatusDetail;
39 import org.openhab.core.thing.ThingStatusInfo;
40 import org.openhab.core.thing.ThingUID;
41 import org.openhab.core.thing.binding.ThingHandlerCallback;
42
43 /**
44  * Tests cases for {@link NetworkHandler}.
45  *
46  * @author David Graeff - Initial contribution
47  */
48 public class NetworkHandlerTest extends JavaTest {
49     private ThingUID thingUID = new ThingUID("network", "ttype", "ping");
50     @Mock
51     private ThingHandlerCallback callback;
52
53     @Mock
54     private Thing thing;
55
56     @Before
57     public void setUp() {
58         initMocks(this);
59         when(thing.getUID()).thenReturn(thingUID);
60     }
61
62     @Test
63     public void checkAllConfigurations() {
64         NetworkBindingConfiguration config = new NetworkBindingConfiguration();
65         NetworkHandler handler = spy(new NetworkHandler(thing, true, config));
66         handler.setCallback(callback);
67         // Provide all possible configuration
68         when(thing.getConfiguration()).thenAnswer(a -> {
69             Configuration conf = new Configuration();
70             conf.put(NetworkBindingConstants.PARAMETER_RETRY, 10);
71             conf.put(NetworkBindingConstants.PARAMETER_HOSTNAME, "127.0.0.1");
72             conf.put(NetworkBindingConstants.PARAMETER_PORT, 8080);
73             conf.put(NetworkBindingConstants.PARAMETER_REFRESH_INTERVAL, 101010);
74             conf.put(NetworkBindingConstants.PARAMETER_TIMEOUT, 1234);
75             return conf;
76         });
77         PresenceDetection presenceDetection = spy(new PresenceDetection(handler, 2000));
78         // Mock start/stop automatic refresh
79         doNothing().when(presenceDetection).startAutomaticRefresh(any());
80         doNothing().when(presenceDetection).stopAutomaticRefresh();
81
82         handler.initialize(presenceDetection);
83         assertThat(handler.retries, is(10));
84         assertThat(presenceDetection.getHostname(), is("127.0.0.1"));
85         assertThat(presenceDetection.getServicePorts().iterator().next(), is(8080));
86         assertThat(presenceDetection.getRefreshInterval(), is(101010L));
87         assertThat(presenceDetection.getTimeout(), is(1234));
88     }
89
90     @Test
91     public void tcpDeviceInitTests() {
92         NetworkBindingConfiguration config = new NetworkBindingConfiguration();
93         NetworkHandler handler = spy(new NetworkHandler(thing, true, config));
94         Assert.assertThat(handler.isTCPServiceDevice(), is(true));
95         handler.setCallback(callback);
96         // Port is missing, should make the device OFFLINE
97         when(thing.getConfiguration()).thenAnswer(a -> {
98             Configuration conf = new Configuration();
99             conf.put(NetworkBindingConstants.PARAMETER_HOSTNAME, "127.0.0.1");
100             return conf;
101         });
102         handler.initialize(new PresenceDetection(handler, 2000));
103         // Check that we are offline
104         ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
105         verify(callback).statusUpdated(eq(thing), statusInfoCaptor.capture());
106         Assert.assertThat(statusInfoCaptor.getValue().getStatus(), is(equalTo(ThingStatus.OFFLINE)));
107         Assert.assertThat(statusInfoCaptor.getValue().getStatusDetail(),
108                 is(equalTo(ThingStatusDetail.CONFIGURATION_ERROR)));
109     }
110
111     @Test
112     public void pingDeviceInitTests() {
113         NetworkBindingConfiguration config = new NetworkBindingConfiguration();
114         NetworkHandler handler = spy(new NetworkHandler(thing, false, config));
115         handler.setCallback(callback);
116         // Provide minimal configuration
117         when(thing.getConfiguration()).thenAnswer(a -> {
118             Configuration conf = new Configuration();
119             conf.put(NetworkBindingConstants.PARAMETER_HOSTNAME, "127.0.0.1");
120             return conf;
121         });
122         PresenceDetection presenceDetection = spy(new PresenceDetection(handler, 2000));
123         // Mock start/stop automatic refresh
124         doNothing().when(presenceDetection).startAutomaticRefresh(any());
125         doNothing().when(presenceDetection).stopAutomaticRefresh();
126
127         handler.initialize(presenceDetection);
128         // Check that we are online
129         ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
130         verify(callback).statusUpdated(eq(thing), statusInfoCaptor.capture());
131         assertEquals(ThingStatus.ONLINE, statusInfoCaptor.getValue().getStatus());
132
133         // Mock result value
134         PresenceDetectionValue value = mock(PresenceDetectionValue.class);
135         when(value.getLowestLatency()).thenReturn(10.0);
136         when(value.isReachable()).thenReturn(true);
137         when(value.getSuccessfulDetectionTypes()).thenReturn("TESTMETHOD");
138
139         // Partial result from the PresenceDetection object should affect the
140         // ONLINE and LATENCY channel
141         handler.partialDetectionResult(value);
142         verify(callback).stateUpdated(eq(new ChannelUID(thingUID, NetworkBindingConstants.CHANNEL_ONLINE)),
143                 eq(OnOffType.ON));
144         verify(callback).stateUpdated(eq(new ChannelUID(thingUID, NetworkBindingConstants.CHANNEL_LATENCY)),
145                 eq(new QuantityType<>("10.0 ms")));
146
147         // Final result affects the LASTSEEN channel
148         when(value.isFinished()).thenReturn(true);
149         handler.finalDetectionResult(value);
150         verify(callback).stateUpdated(eq(new ChannelUID(thingUID, NetworkBindingConstants.CHANNEL_LASTSEEN)), any());
151     }
152 }