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.enigma2.internal.discovery;
15 import static org.eclipse.jdt.annotation.Checks.requireNonNull;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.Matchers.*;
18 import static org.mockito.Mockito.*;
20 import java.net.Inet4Address;
21 import java.net.InetAddress;
23 import javax.jmdns.ServiceInfo;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.openhab.binding.enigma2.internal.Enigma2BindingConstants;
30 import org.openhab.binding.enigma2.internal.Enigma2HttpClient;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.thing.ThingUID;
35 * The {@link Enigma2DiscoveryParticipantTest} class is responsible for testing {@link Enigma2DiscoveryParticipant}.
37 * @author Guido Dolfen - Initial contribution
39 @SuppressWarnings({ "null" })
41 public class Enigma2DiscoveryParticipantTest {
43 private ServiceInfo serviceInfo;
45 private Enigma2HttpClient enigma2HttpClient;
47 private Enigma2DiscoveryParticipant enigma2DiscoveryParticipant;
51 enigma2HttpClient = mock(Enigma2HttpClient.class);
52 serviceInfo = mock(ServiceInfo.class);
53 enigma2DiscoveryParticipant = spy(new Enigma2DiscoveryParticipant());
54 when(enigma2DiscoveryParticipant.getEnigma2HttpClient()).thenReturn(requireNonNull(enigma2HttpClient));
58 public void testGetSupportedThingTypeUIDs() {
59 assertThat(enigma2DiscoveryParticipant.getSupportedThingTypeUIDs(),
60 contains(Enigma2BindingConstants.THING_TYPE_DEVICE));
64 public void testGetServiceType() {
65 assertThat(enigma2DiscoveryParticipant.getServiceType(), is("_http._tcp.local."));
69 public void testCreateResult() throws Exception {
70 when(serviceInfo.getName()).thenReturn("enigma2");
71 when(enigma2HttpClient.get("http://192.168.10.3/web/about"))
72 .thenReturn("<e2abouts><e2about><e2enigmaversion>2020-01-11</e2enigmaversion></e2about></e2abouts>");
73 when(serviceInfo.getInet4Addresses())
74 .thenReturn(new Inet4Address[] { (Inet4Address) InetAddress.getAllByName("192.168.10.3")[0] });
75 DiscoveryResult discoveryResult = enigma2DiscoveryParticipant.createResult(requireNonNull(serviceInfo));
76 assertThat(discoveryResult, is(notNullValue()));
77 assertThat(discoveryResult.getLabel(), is("enigma2"));
78 assertThat(discoveryResult.getThingUID(),
79 is(new ThingUID(Enigma2BindingConstants.THING_TYPE_DEVICE, "192_168_10_3")));
80 assertThat(discoveryResult.getProperties(), is(notNullValue()));
81 assertThat(discoveryResult.getProperties(), hasEntry(Enigma2BindingConstants.CONFIG_HOST, "192.168.10.3"));
82 assertThat(discoveryResult.getProperties(), hasEntry(Enigma2BindingConstants.CONFIG_REFRESH, 5));
83 assertThat(discoveryResult.getProperties(), hasEntry(Enigma2BindingConstants.CONFIG_TIMEOUT, 5));
87 public void testCreateResultNotFound() throws Exception {
88 when(enigma2HttpClient.get("http://192.168.10.3/web/about")).thenReturn("any");
89 when(serviceInfo.getInet4Addresses())
90 .thenReturn(new Inet4Address[] { (Inet4Address) InetAddress.getAllByName("192.168.10.3")[0] });
91 assertThat(enigma2DiscoveryParticipant.createResult(requireNonNull(serviceInfo)), is(nullValue()));
95 public void testGetThingUID() throws Exception {
96 when(serviceInfo.getInet4Addresses())
97 .thenReturn(new Inet4Address[] { (Inet4Address) InetAddress.getAllByName("192.168.10.3")[0] });
98 assertThat(enigma2DiscoveryParticipant.getThingUID(requireNonNull(serviceInfo)),
99 is(new ThingUID(Enigma2BindingConstants.THING_TYPE_DEVICE, "192_168_10_3")));
103 public void testGetThingUIDTwoAddresses() throws Exception {
104 when(serviceInfo.getName()).thenReturn("enigma2");
105 Inet4Address[] addresses = { (Inet4Address) InetAddress.getAllByName("192.168.10.3")[0],
106 (Inet4Address) InetAddress.getAllByName("192.168.10.4")[0] };
107 when(serviceInfo.getInet4Addresses()).thenReturn(addresses);
108 assertThat(enigma2DiscoveryParticipant.getThingUID(requireNonNull(serviceInfo)),
109 is(new ThingUID(Enigma2BindingConstants.THING_TYPE_DEVICE, "192_168_10_3")));