]> git.basschouten.com Git - openhab-addons.git/blob
b801d8c9546c544c3c3a83da0f862191a636dad6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.wemo.internal.discovery.test;
14
15 import static org.openhab.core.config.discovery.inbox.InboxPredicates.forThingUID;
16 import static org.hamcrest.CoreMatchers.*;
17 import static org.junit.Assert.*;
18
19 import java.io.IOException;
20 import java.net.MalformedURLException;
21 import java.net.URISyntaxException;
22 import java.util.Collection;
23 import java.util.List;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.inbox.Inbox;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.jupnp.model.ValidationException;
35 import org.jupnp.model.meta.Device;
36 import org.openhab.binding.wemo.internal.WemoBindingConstants;
37 import org.openhab.binding.wemo.internal.discovery.WemoDiscoveryService;
38 import org.openhab.binding.wemo.internal.test.GenericWemoOSGiTest;
39
40 /**
41  * Tests for {@link WemoDiscoveryService}.
42  *
43  * @author Svilen Valkanov - Initial contribution
44  * @author Stefan Triller - Ported Tests from Groovy to Java
45  */
46 @NonNullByDefault
47 public class WemoDiscoveryOSGiTest extends GenericWemoOSGiTest {
48
49     // UpnP service information
50     private static final String SERVICE_ID = "basicevent";
51     private static final String SERVICE_NUMBER = "1";
52
53     private @NonNullByDefault({}) Inbox inbox;
54
55     @Before
56     public void setUp() throws IOException {
57         setUpServices();
58
59         inbox = getService(Inbox.class);
60         assertThat(inbox, is(notNullValue()));
61     }
62
63     @After
64     public void tearDown() {
65         List<DiscoveryResult> results = inbox.getAll();
66         assertThat(results.size(), is(0));
67     }
68
69     @Test
70     public void assertSupportedThingIsDiscovered()
71             throws MalformedURLException, URISyntaxException, ValidationException {
72         ThingTypeUID thingType = WemoBindingConstants.THING_TYPE_INSIGHT;
73         String model = WemoBindingConstants.THING_TYPE_INSIGHT.getId();
74
75         addUpnpDevice(SERVICE_ID, SERVICE_NUMBER, model);
76
77         waitForAssert(() -> {
78             Collection<Device> devices = mockUpnpService.getRegistry().getDevices();
79             assertThat(devices.size(), is(1));
80             Device device = devices.iterator().next();
81             assertThat(device.getDetails().getModelDetails().getModelName(), is(model));
82         });
83
84         ThingUID thingUID = new ThingUID(thingType, DEVICE_UDN);
85
86         waitForAssert(() -> {
87             assertTrue(inbox.stream().anyMatch(forThingUID(thingUID)));
88         });
89
90         inbox.approve(thingUID, DEVICE_FRIENDLY_NAME);
91
92         waitForAssert(() -> {
93             Thing thing = thingRegistry.get(thingUID);
94             assertThat(thing, is(notNullValue()));
95         });
96     }
97 }