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