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