2 * Copyright (c) 2010-2022 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.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
21 import javax.jmdns.ServiceInfo;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.mockito.junit.jupiter.MockitoSettings;
29 import org.mockito.quality.Strictness;
30 import org.openhab.core.config.discovery.DiscoveryResult;
31 import org.openhab.core.config.discovery.DiscoveryResultFlag;
32 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
33 import org.openhab.core.test.java.JavaOSGiTest;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingUID;
38 * Tests for {@link TradfriDiscoveryParticipant}.
40 * @author Kai Kreuzer - Initial contribution
42 @ExtendWith(MockitoExtension.class)
43 @MockitoSettings(strictness = Strictness.LENIENT)
44 public class TradfriDiscoveryParticipantOSGITest extends JavaOSGiTest {
46 private MDNSDiscoveryParticipant discoveryParticipant;
48 private @Mock ServiceInfo tradfriGateway;
49 private @Mock ServiceInfo otherDevice;
52 public void beforeEach() {
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 correctSupportedTypes() {
70 assertThat(discoveryParticipant.getSupportedThingTypeUIDs().size(), is(1));
71 assertThat(discoveryParticipant.getSupportedThingTypeUIDs().iterator().next(), is(GATEWAY_TYPE_UID));
75 public void correctThingUID() {
76 when(tradfriGateway.getName()).thenReturn("gw:12-34-56-78-90-ab");
77 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
78 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
80 when(tradfriGateway.getName()).thenReturn("gw:1234567890ab");
81 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
82 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
84 when(tradfriGateway.getName()).thenReturn("gw-12-34-56-78-90-ab");
85 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
86 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
88 when(tradfriGateway.getName()).thenReturn("gw:1234567890ab");
89 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
90 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
92 when(tradfriGateway.getName()).thenReturn("gw:1234567890abServiceInfo");
93 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
94 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
96 when(tradfriGateway.getName()).thenReturn("gw:12-34-56-78-90-ab-service-info");
97 assertThat(discoveryParticipant.getThingUID(tradfriGateway),
98 is(new ThingUID("tradfri:gateway:gw1234567890ab")));
100 // restore original value
101 when(tradfriGateway.getName()).thenReturn("gw:12-34-56-78-90-ab");
105 public void validDiscoveryResult() {
106 DiscoveryResult result = discoveryParticipant.createResult(tradfriGateway);
108 assertNotNull(result);
109 assertThat(result.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is("1.1"));
110 assertThat(result.getFlag(), is(DiscoveryResultFlag.NEW));
111 assertThat(result.getThingUID(), is(new ThingUID("tradfri:gateway:gw1234567890ab")));
112 assertThat(result.getThingTypeUID(), is(GATEWAY_TYPE_UID));
113 assertThat(result.getBridgeUID(), is(nullValue()));
114 assertThat(result.getProperties().get(Thing.PROPERTY_VENDOR), is("IKEA of Sweden"));
115 assertThat(result.getProperties().get(GATEWAY_CONFIG_HOST), is("192.168.0.5"));
116 assertThat(result.getProperties().get(GATEWAY_CONFIG_PORT), is(1234));
117 assertThat(result.getRepresentationProperty(), is(Thing.PROPERTY_SERIAL_NUMBER));
121 public void noThingUIDForUnknownDevice() {
122 when(otherDevice.getName()).thenReturn("something");
123 assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
125 when(otherDevice.getName()).thenReturn("gw_1234567890ab");
126 assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
128 when(otherDevice.getName()).thenReturn("gw:12-3456--7890-ab");
129 assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
131 when(otherDevice.getName()).thenReturn("gw1234567890ab");
132 assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
134 // restore original value
135 when(otherDevice.getName()).thenReturn("something");
139 public void noDiscoveryResultForUnknownDevice() {
140 assertThat(discoveryParticipant.createResult(otherDevice), is(nullValue()));