]> git.basschouten.com Git - openhab-addons.git/blob
28b3fe9e938bb5700156eb9edca7307a328efc9c
[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.tradfri.internal.discovery;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.junit.Assert.*;
17 import static org.mockito.Mockito.when;
18 import static org.mockito.MockitoAnnotations.initMocks;
19 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
20
21 import javax.jmdns.ServiceInfo;
22
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultFlag;
25 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.test.java.JavaOSGiTest;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32
33 /**
34  * Tests for {@link TradfriDiscoveryParticipant}.
35  *
36  * @author Kai Kreuzer - Initial contribution
37  */
38 public class TradfriDiscoveryParticipantOSGITest extends JavaOSGiTest {
39
40     private MDNSDiscoveryParticipant discoveryParticipant;
41
42     @Mock
43     private ServiceInfo tradfriGateway;
44
45     @Mock
46     private ServiceInfo otherDevice;
47
48     @Before
49     public void setUp() {
50         initMocks(this);
51         discoveryParticipant = getService(MDNSDiscoveryParticipant.class, TradfriDiscoveryParticipant.class);
52
53         when(tradfriGateway.getType()).thenReturn("_coap._udp.local.");
54         when(tradfriGateway.getName()).thenReturn("gw:12-34-56-78-90-ab");
55         when(tradfriGateway.getHostAddresses()).thenReturn(new String[] { "192.168.0.5" });
56         when(tradfriGateway.getPort()).thenReturn(1234);
57         when(tradfriGateway.getPropertyString("version")).thenReturn("1.1");
58
59         when(otherDevice.getType()).thenReturn("_coap._udp.local.");
60         when(otherDevice.getName()).thenReturn("something");
61         when(otherDevice.getHostAddresses()).thenReturn(new String[] { "192.168.0.5" });
62         when(otherDevice.getPort()).thenReturn(1234);
63         when(otherDevice.getPropertyString("version")).thenReturn("1.1");
64     }
65
66     @Test
67     public void correctSupportedTypes() {
68         assertThat(discoveryParticipant.getSupportedThingTypeUIDs().size(), is(1));
69         assertThat(discoveryParticipant.getSupportedThingTypeUIDs().iterator().next(), is(GATEWAY_TYPE_UID));
70     }
71
72     @Test
73     public void correctThingUID() {
74         when(tradfriGateway.getName()).thenReturn("gw:12-34-56-78-90-ab");
75         assertThat(discoveryParticipant.getThingUID(tradfriGateway),
76                 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
77
78         when(tradfriGateway.getName()).thenReturn("gw:1234567890ab");
79         assertThat(discoveryParticipant.getThingUID(tradfriGateway),
80                 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
81
82         when(tradfriGateway.getName()).thenReturn("gw-12-34-56-78-90-ab");
83         assertThat(discoveryParticipant.getThingUID(tradfriGateway),
84                 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
85
86         when(tradfriGateway.getName()).thenReturn("gw:1234567890ab");
87         assertThat(discoveryParticipant.getThingUID(tradfriGateway),
88                 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
89
90         when(tradfriGateway.getName()).thenReturn("gw:1234567890abServiceInfo");
91         assertThat(discoveryParticipant.getThingUID(tradfriGateway),
92                 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
93
94         when(tradfriGateway.getName()).thenReturn("gw:12-34-56-78-90-ab-service-info");
95         assertThat(discoveryParticipant.getThingUID(tradfriGateway),
96                 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
97
98         // restore original value
99         when(tradfriGateway.getName()).thenReturn("gw:12-34-56-78-90-ab");
100     }
101
102     @Test
103     public void validDiscoveryResult() {
104         DiscoveryResult result = discoveryParticipant.createResult(tradfriGateway);
105
106         assertNotNull(result);
107         assertThat(result.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is("1.1"));
108         assertThat(result.getFlag(), is(DiscoveryResultFlag.NEW));
109         assertThat(result.getThingUID(), is(new ThingUID("tradfri:gateway:gw1234567890ab")));
110         assertThat(result.getThingTypeUID(), is(GATEWAY_TYPE_UID));
111         assertThat(result.getBridgeUID(), is(nullValue()));
112         assertThat(result.getProperties().get(Thing.PROPERTY_VENDOR), is("IKEA of Sweden"));
113         assertThat(result.getProperties().get(GATEWAY_CONFIG_HOST), is("192.168.0.5"));
114         assertThat(result.getProperties().get(GATEWAY_CONFIG_PORT), is(1234));
115         assertThat(result.getRepresentationProperty(), is(GATEWAY_CONFIG_HOST));
116     }
117
118     @Test
119     public void noThingUIDForUnknownDevice() {
120         when(otherDevice.getName()).thenReturn("something");
121         assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
122
123         when(otherDevice.getName()).thenReturn("gw_1234567890ab");
124         assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
125
126         when(otherDevice.getName()).thenReturn("gw:12-3456--7890-ab");
127         assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
128
129         when(otherDevice.getName()).thenReturn("gw1234567890ab");
130         assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
131
132         // restore original value
133         when(otherDevice.getName()).thenReturn("something");
134     }
135
136     @Test
137     public void noDiscoveryResultForUnknownDevice() {
138         assertThat(discoveryParticipant.createResult(otherDevice), is(nullValue()));
139     }
140 }