]> git.basschouten.com Git - openhab-addons.git/blob
868d6661fef2f95ebfbe165776399a1b1b0e6ca7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.enigma2.internal.discovery;
14
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.*;
19
20 import java.net.Inet4Address;
21 import java.net.InetAddress;
22
23 import javax.jmdns.ServiceInfo;
24
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;
33
34 /**
35  * The {@link Enigma2DiscoveryParticipantTest} class is responsible for testing {@link Enigma2DiscoveryParticipant}.
36  *
37  * @author Guido Dolfen - Initial contribution
38  */
39 @SuppressWarnings({ "null" })
40 @NonNullByDefault
41 public class Enigma2DiscoveryParticipantTest {
42     @Nullable
43     private ServiceInfo serviceInfo;
44     @Nullable
45     private Enigma2HttpClient enigma2HttpClient;
46     @Nullable
47     private Enigma2DiscoveryParticipant enigma2DiscoveryParticipant;
48
49     @BeforeEach
50     public void setUp() {
51         enigma2HttpClient = mock(Enigma2HttpClient.class);
52         serviceInfo = mock(ServiceInfo.class);
53         enigma2DiscoveryParticipant = spy(new Enigma2DiscoveryParticipant());
54         when(enigma2DiscoveryParticipant.getEnigma2HttpClient()).thenReturn(requireNonNull(enigma2HttpClient));
55     }
56
57     @Test
58     public void testGetSupportedThingTypeUIDs() {
59         assertThat(enigma2DiscoveryParticipant.getSupportedThingTypeUIDs(),
60                 contains(Enigma2BindingConstants.THING_TYPE_DEVICE));
61     }
62
63     @Test
64     public void testGetServiceType() {
65         assertThat(enigma2DiscoveryParticipant.getServiceType(), is("_http._tcp.local."));
66     }
67
68     @Test
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));
84     }
85
86     @Test
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()));
92     }
93
94     @Test
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")));
100     }
101
102     @Test
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")));
110     }
111 }