]> git.basschouten.com Git - openhab-addons.git/blob
0e6c941e735108b26a60f78d70c344725720cb6c
[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.enigma2.internal.discovery;
14
15 import static org.eclipse.jdt.annotation.Checks.requireNonNull;
16 import static org.hamcrest.Matchers.*;
17 import static org.junit.Assert.*;
18 import static org.mockito.Mockito.*;
19 import static org.mockito.Mockito.when;
20
21 import java.net.Inet4Address;
22 import java.net.InetAddress;
23
24 import javax.jmdns.ServiceInfo;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.openhab.binding.enigma2.internal.Enigma2BindingConstants;
31 import org.openhab.binding.enigma2.internal.Enigma2HttpClient;
32 import org.openhab.core.config.discovery.DiscoveryResult;
33 import org.openhab.core.thing.ThingUID;
34
35 /**
36  * The {@link Enigma2DiscoveryParticipantTest} class is responsible for testing {@link Enigma2DiscoveryParticipant}.
37  *
38  * @author Guido Dolfen - Initial contribution
39  */
40 @SuppressWarnings({ "null" })
41 @NonNullByDefault
42 public class Enigma2DiscoveryParticipantTest {
43     @Nullable
44     private ServiceInfo serviceInfo;
45     @Nullable
46     private Enigma2HttpClient enigma2HttpClient;
47     @Nullable
48     private Enigma2DiscoveryParticipant enigma2DiscoveryParticipant;
49
50     @Before
51     public void setUp() {
52         enigma2HttpClient = mock(Enigma2HttpClient.class);
53         serviceInfo = mock(ServiceInfo.class);
54         enigma2DiscoveryParticipant = spy(new Enigma2DiscoveryParticipant());
55         when(enigma2DiscoveryParticipant.getEnigma2HttpClient()).thenReturn(requireNonNull(enigma2HttpClient));
56     }
57
58     @Test
59     public void testGetSupportedThingTypeUIDs() {
60         assertThat(enigma2DiscoveryParticipant.getSupportedThingTypeUIDs(),
61                 contains(Enigma2BindingConstants.THING_TYPE_DEVICE));
62     }
63
64     @Test
65     public void testGetServiceType() {
66         assertThat(enigma2DiscoveryParticipant.getServiceType(), is("_http._tcp.local."));
67     }
68
69     @Test
70     public void testCreateResult() throws Exception {
71         when(serviceInfo.getName()).thenReturn("enigma2");
72         when(enigma2HttpClient.get("http://192.168.10.3/web/about"))
73                 .thenReturn("<e2abouts><e2about><e2enigmaversion>2020-01-11</e2enigmaversion></e2about></e2abouts>");
74         when(serviceInfo.getInet4Addresses())
75                 .thenReturn(new Inet4Address[] { (Inet4Address) InetAddress.getAllByName("192.168.10.3")[0] });
76         DiscoveryResult discoveryResult = enigma2DiscoveryParticipant.createResult(requireNonNull(serviceInfo));
77         assertThat(discoveryResult, is(notNullValue()));
78         assertThat(discoveryResult.getLabel(), is("enigma2"));
79         assertThat(discoveryResult.getThingUID(),
80                 is(new ThingUID(Enigma2BindingConstants.THING_TYPE_DEVICE, "192_168_10_3")));
81         assertThat(discoveryResult.getProperties(), is(notNullValue()));
82         assertThat(discoveryResult.getProperties(), hasEntry(Enigma2BindingConstants.CONFIG_HOST, "192.168.10.3"));
83         assertThat(discoveryResult.getProperties(), hasEntry(Enigma2BindingConstants.CONFIG_REFRESH, 5));
84         assertThat(discoveryResult.getProperties(), hasEntry(Enigma2BindingConstants.CONFIG_TIMEOUT, 5));
85     }
86
87     @Test
88     public void testCreateResultNotFound() throws Exception {
89         when(enigma2HttpClient.get("http://192.168.10.3/web/about")).thenReturn("any");
90         when(serviceInfo.getInet4Addresses())
91                 .thenReturn(new Inet4Address[] { (Inet4Address) InetAddress.getAllByName("192.168.10.3")[0] });
92         assertThat(enigma2DiscoveryParticipant.createResult(requireNonNull(serviceInfo)), is(nullValue()));
93     }
94
95     @Test
96     public void testGetThingUID() throws Exception {
97         when(serviceInfo.getInet4Addresses())
98                 .thenReturn(new Inet4Address[] { (Inet4Address) InetAddress.getAllByName("192.168.10.3")[0] });
99         assertThat(enigma2DiscoveryParticipant.getThingUID(requireNonNull(serviceInfo)),
100                 is(new ThingUID(Enigma2BindingConstants.THING_TYPE_DEVICE, "192_168_10_3")));
101     }
102
103     @Test
104     public void testGetThingUIDTwoAddresses() throws Exception {
105         when(serviceInfo.getName()).thenReturn("enigma2");
106         Inet4Address[] addresses = { (Inet4Address) InetAddress.getAllByName("192.168.10.3")[0],
107                 (Inet4Address) InetAddress.getAllByName("192.168.10.4")[0] };
108         when(serviceInfo.getInet4Addresses()).thenReturn(addresses);
109         assertThat(enigma2DiscoveryParticipant.getThingUID(requireNonNull(serviceInfo)),
110                 is(new ThingUID(Enigma2BindingConstants.THING_TYPE_DEVICE, "192_168_10_3")));
111     }
112 }