]> git.basschouten.com Git - openhab-addons.git/blob
873c2af6edf1a2bb56755a8b5552a4eb56963e08
[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.hue.internal.discovery;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.fail;
18 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
19 import static org.openhab.core.thing.Thing.PROPERTY_SERIAL_NUMBER;
20
21 import java.net.MalformedURLException;
22 import java.net.URL;
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.jupnp.model.ValidationException;
28 import org.jupnp.model.meta.DeviceDetails;
29 import org.jupnp.model.meta.ManufacturerDetails;
30 import org.jupnp.model.meta.ModelDetails;
31 import org.jupnp.model.meta.RemoteDevice;
32 import org.jupnp.model.meta.RemoteDeviceIdentity;
33 import org.jupnp.model.meta.RemoteService;
34 import org.jupnp.model.types.DeviceType;
35 import org.jupnp.model.types.UDN;
36 import org.openhab.core.config.discovery.DiscoveryResult;
37 import org.openhab.core.config.discovery.DiscoveryResultFlag;
38 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
39 import org.openhab.core.test.java.JavaOSGiTest;
40 import org.openhab.core.thing.ThingUID;
41
42 /**
43  * Tests for {@link org.openhab.binding.hue.internal.discovery.HueBridgeDiscoveryParticipant}.
44  *
45  * @author Kai Kreuzer - Initial contribution
46  * @author Thomas Höfer - Added representation
47  * @author Markus Rathgeb - migrated to plain Java test
48  */
49 public class HueBridgeDiscoveryParticipantOSGITest extends JavaOSGiTest {
50
51     UpnpDiscoveryParticipant discoveryParticipant;
52
53     RemoteDevice hueDevice;
54     RemoteDevice otherDevice;
55
56     @BeforeEach
57     public void setUp() {
58         discoveryParticipant = getService(UpnpDiscoveryParticipant.class, HueBridgeDiscoveryParticipant.class);
59         assertThat(discoveryParticipant, is(notNullValue()));
60
61         try {
62             final RemoteService remoteService = null;
63
64             hueDevice = new RemoteDevice(
65                     new RemoteDeviceIdentity(new UDN("123"), 60, new URL("http://hue"), null, null),
66                     new DeviceType("namespace", "type"),
67                     new DeviceDetails(new URL("http://1.2.3.4/"), "Hue Bridge", new ManufacturerDetails("Philips"),
68                             new ModelDetails("Philips hue bridge"), "serial123", "upc", null),
69                     remoteService);
70
71             otherDevice = new RemoteDevice(
72                     new RemoteDeviceIdentity(new UDN("567"), 60, new URL("http://acme"), null, null),
73                     new DeviceType("namespace", "type"), new DeviceDetails("Some Device",
74                             new ManufacturerDetails("Taiwan"), new ModelDetails("$%&/"), "serial567", "upc"),
75                     remoteService);
76         } catch (final ValidationException | MalformedURLException ex) {
77             fail("Internal test error.");
78         }
79     }
80
81     @AfterEach
82     public void cleanUp() {
83     }
84
85     @Test
86     public void correctSupportedTypes() {
87         assertThat(discoveryParticipant.getSupportedThingTypeUIDs().size(), is(1));
88         assertThat(discoveryParticipant.getSupportedThingTypeUIDs().iterator().next(), is(THING_TYPE_BRIDGE));
89     }
90
91     @Test
92     public void correctThingUID() {
93         assertThat(discoveryParticipant.getThingUID(hueDevice), is(new ThingUID("hue:bridge:serial123")));
94     }
95
96     @Test
97     public void validDiscoveryResult() {
98         final DiscoveryResult result = discoveryParticipant.createResult(hueDevice);
99         assertThat(result.getFlag(), is(DiscoveryResultFlag.NEW));
100         assertThat(result.getThingUID(), is(new ThingUID("hue:bridge:serial123")));
101         assertThat(result.getThingTypeUID(), is(THING_TYPE_BRIDGE));
102         assertThat(result.getBridgeUID(), is(nullValue()));
103         assertThat(result.getProperties().get(HOST), is("1.2.3.4"));
104         assertThat(result.getProperties().get(PROPERTY_SERIAL_NUMBER), is("serial123"));
105         assertThat(result.getRepresentationProperty(), is(PROPERTY_SERIAL_NUMBER));
106     }
107
108     @Test
109     public void noThingUIDForUnknownDevice() {
110         assertThat(discoveryParticipant.getThingUID(otherDevice), is(nullValue()));
111     }
112
113     @Test
114     public void noDiscoveryResultForUnknownDevice() {
115         assertThat(discoveryParticipant.createResult(otherDevice), is(nullValue()));
116     }
117 }