2 * Copyright (c) 2010-2023 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.discovery;
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.*;
20 import java.util.Collections;
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;
36 * Tests cases for {@see PresenceDetectionValue}
38 * @author David Graeff - Initial contribution
40 @ExtendWith(MockitoExtension.class)
41 @MockitoSettings(strictness = Strictness.LENIENT)
42 public class DiscoveryTest {
43 private final String ip = "127.0.0.1";
45 private @Mock PresenceDetectionValue value;
46 private @Mock DiscoveryListener listener;
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");
57 public void pingDeviceDetected() {
58 NetworkDiscoveryService d = new NetworkDiscoveryService();
59 d.addDiscoveryListener(listener);
61 ArgumentCaptor<DiscoveryResult> result = ArgumentCaptor.forClass(DiscoveryResult.class);
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));
74 public void tcpDeviceDetected() {
75 NetworkDiscoveryService d = new NetworkDiscoveryService();
76 d.addDiscoveryListener(listener);
78 ArgumentCaptor<DiscoveryResult> result = ArgumentCaptor.forClass(DiscoveryResult.class);
81 when(value.isPingReachable()).thenReturn(false);
82 when(value.isTCPServiceReachable()).thenReturn(true);
83 when(value.getReachableTCPports()).thenReturn(Collections.singletonList(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));