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.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.*;
22 import javax.jmdns.ServiceInfo;
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;
36 * Tests for {@link TradfriDiscoveryParticipant}.
38 * @author Kai Kreuzer - Initial contribution
40 public class TradfriDiscoveryParticipantOSGITest extends JavaOSGiTest {
42 private MDNSDiscoveryParticipant discoveryParticipant;
44 private AutoCloseable mocksCloseable;
46 private @Mock ServiceInfo tradfriGateway;
47 private @Mock ServiceInfo otherDevice;
50 public void beforeEach() {
51 mocksCloseable = openMocks(this);
53 discoveryParticipant = getService(MDNSDiscoveryParticipant.class, TradfriDiscoveryParticipant.class);
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");
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");
69 public void afterEach() throws Exception {
70 mocksCloseable.close();
74 public void correctSupportedTypes() {
75 assertThat(discoveryParticipant.getSupportedThingTypeUIDs().size(), is(1));
76 assertThat(discoveryParticipant.getSupportedThingTypeUIDs().iterator().next(), is(GATEWAY_TYPE_UID));
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")));
85 when(tradfriGateway.getName()).thenReturn("gw:1234567890ab");
86 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
87 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
89 when(tradfriGateway.getName()).thenReturn("gw-12-34-56-78-90-ab");
90 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
91 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
93 when(tradfriGateway.getName()).thenReturn("gw:1234567890ab");
94 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
95 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
97 when(tradfriGateway.getName()).thenReturn("gw:1234567890abServiceInfo");
98 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
99 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
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")));
105 // restore original value
106 when(tradfriGateway.getName()).thenReturn("gw:12-34-56-78-90-ab");
110 public void validDiscoveryResult() {
111 DiscoveryResult result = discoveryParticipant.createResult(tradfriGateway);
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));
126 public void noThingUIDForUnknownDevice() {
127 when(otherDevice.getName()).thenReturn("something");
128 assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
130 when(otherDevice.getName()).thenReturn("gw_1234567890ab");
131 assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
133 when(otherDevice.getName()).thenReturn("gw:12-3456--7890-ab");
134 assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
136 when(otherDevice.getName()).thenReturn("gw1234567890ab");
137 assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
139 // restore original value
140 when(otherDevice.getName()).thenReturn("something");
144 public void noDiscoveryResultForUnknownDevice() {
145 assertThat(discoveryParticipant.createResult(otherDevice), is(nullValue()));