2 * Copyright (c) 2010-2020 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.tradfri.internal.discovery;
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.*;
21 import javax.jmdns.ServiceInfo;
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;
34 * Tests for {@link TradfriDiscoveryParticipant}.
36 * @author Kai Kreuzer - Initial contribution
38 public class TradfriDiscoveryParticipantOSGITest extends JavaOSGiTest {
40 private MDNSDiscoveryParticipant discoveryParticipant;
43 private ServiceInfo tradfriGateway;
46 private ServiceInfo otherDevice;
51 discoveryParticipant = getService(MDNSDiscoveryParticipant.class, TradfriDiscoveryParticipant.class);
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");
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");
67 public void correctSupportedTypes() {
68 assertThat(discoveryParticipant.getSupportedThingTypeUIDs().size(), is(1));
69 assertThat(discoveryParticipant.getSupportedThingTypeUIDs().iterator().next(), is(GATEWAY_TYPE_UID));
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")));
78 when(tradfriGateway.getName()).thenReturn("gw:1234567890ab");
79 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
80 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
82 when(tradfriGateway.getName()).thenReturn("gw-12-34-56-78-90-ab");
83 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
84 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
86 when(tradfriGateway.getName()).thenReturn("gw:1234567890ab");
87 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
88 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
90 when(tradfriGateway.getName()).thenReturn("gw:1234567890abServiceInfo");
91 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
92 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
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")));
98 // restore original value
99 when(tradfriGateway.getName()).thenReturn("gw:12-34-56-78-90-ab");
103 public void validDiscoveryResult() {
104 DiscoveryResult result = discoveryParticipant.createResult(tradfriGateway);
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));
119 public void noThingUIDForUnknownDevice() {
120 when(otherDevice.getName()).thenReturn("something");
121 assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
123 when(otherDevice.getName()).thenReturn("gw_1234567890ab");
124 assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
126 when(otherDevice.getName()).thenReturn("gw:12-3456--7890-ab");
127 assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
129 when(otherDevice.getName()).thenReturn("gw1234567890ab");
130 assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
132 // restore original value
133 when(otherDevice.getName()).thenReturn("something");
137 public void noDiscoveryResultForUnknownDevice() {
138 assertThat(discoveryParticipant.createResult(otherDevice), is(nullValue()));