]> git.basschouten.com Git - openhab-addons.git/blob
1abef4f09a57ec0d47b58ea891ad444293e6de8f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.discovery;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.util.List;
21
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.NetworkBindingConstants;
31 import org.openhab.binding.network.internal.PresenceDetectionValue;
32 import org.openhab.core.config.discovery.DiscoveryListener;
33 import org.openhab.core.config.discovery.DiscoveryResult;
34
35 /**
36  * Tests cases for {@see PresenceDetectionValue}
37  *
38  * @author David Graeff - Initial contribution
39  */
40 @ExtendWith(MockitoExtension.class)
41 @MockitoSettings(strictness = Strictness.LENIENT)
42 public class DiscoveryTest {
43     private final String ip = "127.0.0.1";
44
45     private @Mock PresenceDetectionValue value;
46     private @Mock DiscoveryListener listener;
47
48     @BeforeEach
49     public void setUp() {
50         when(value.getHostAddress()).thenReturn(ip);
51         when(value.getLowestLatency()).thenReturn(10.0);
52         when(value.isReachable()).thenReturn(true);
53         when(value.getSuccessfulDetectionTypes()).thenReturn("TESTMETHOD");
54     }
55
56     @Test
57     public void pingDeviceDetected() {
58         NetworkDiscoveryService d = new NetworkDiscoveryService();
59         d.addDiscoveryListener(listener);
60
61         ArgumentCaptor<DiscoveryResult> result = ArgumentCaptor.forClass(DiscoveryResult.class);
62
63         // Ping device
64         when(value.isPingReachable()).thenReturn(true);
65         when(value.isTCPServiceReachable()).thenReturn(false);
66         d.partialDetectionResult(value);
67         verify(listener).thingDiscovered(any(), result.capture());
68         DiscoveryResult dresult = result.getValue();
69         assertThat(dresult.getThingUID(), is(NetworkDiscoveryService.createPingUID(ip)));
70         assertThat(dresult.getProperties().get(NetworkBindingConstants.PARAMETER_HOSTNAME), is(ip));
71     }
72
73     @Test
74     public void tcpDeviceDetected() {
75         NetworkDiscoveryService d = new NetworkDiscoveryService();
76         d.addDiscoveryListener(listener);
77
78         ArgumentCaptor<DiscoveryResult> result = ArgumentCaptor.forClass(DiscoveryResult.class);
79
80         // TCP device
81         when(value.isPingReachable()).thenReturn(false);
82         when(value.isTCPServiceReachable()).thenReturn(true);
83         when(value.getReachableTCPports()).thenReturn(List.of(1010));
84         d.partialDetectionResult(value);
85         verify(listener).thingDiscovered(any(), result.capture());
86         DiscoveryResult dresult = result.getValue();
87         assertThat(dresult.getThingUID(), is(NetworkDiscoveryService.createServiceUID(ip, 1010)));
88         assertThat(dresult.getProperties().get(NetworkBindingConstants.PARAMETER_HOSTNAME), is(ip));
89         assertThat(dresult.getProperties().get(NetworkBindingConstants.PARAMETER_PORT), is(1010));
90     }
91 }