]> git.basschouten.com Git - openhab-addons.git/blob
5d3a0f77598bd009274df67aab1897e9ee9e3fae
[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.test;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import org.openhab.binding.wemo.internal.WemoBindingConstants;
19 import org.openhab.core.config.core.Configuration;
20 import org.openhab.core.thing.Bridge;
21 import org.openhab.core.thing.Channel;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
27 import org.openhab.core.thing.binding.builder.BridgeBuilder;
28 import org.openhab.core.thing.binding.builder.ChannelBuilder;
29 import org.openhab.core.thing.binding.builder.ThingBuilder;
30 import org.openhab.core.thing.type.ChannelKind;
31
32 /**
33  * Generic test class for all WemoLight related tests that contains methods and constants used across the different test
34  * classes
35  *
36  * @author Svilen Valkanov - Initial contribution
37  * @author Stefan Triller - Ported Tests from Groovy to Java
38  */
39 public class GenericWemoLightOSGiTestParent extends GenericWemoOSGiTest {
40
41     // Thing information
42     protected static final ThingTypeUID THING_TYPE_UID = WemoBindingConstants.THING_TYPE_MZ100;
43     protected static final ThingTypeUID BRIDGE_TYPE_UID = WemoBindingConstants.THING_TYPE_BRIDGE;
44     protected static final String WEMO_BRIDGE_ID = BRIDGE_TYPE_UID.getId();
45     protected static final String DEFAULT_TEST_CHANNEL = WemoBindingConstants.CHANNEL_STATE;
46     protected static final String DEFAULT_TEST_CHANNEL_TYPE = "Switch";
47
48     private static final String WEMO_LIGHT_ID = THING_TYPE_UID.getId();
49
50     // UPnP service information
51     protected static final String DEVICE_MODEL_NAME = WEMO_LIGHT_ID;
52     protected static final String SERVICE_ID = "bridge";
53     protected static final String SERVICE_NUMBER = "1";
54     protected static final String SERVLET_URL = DEVICE_CONTROL_PATH + SERVICE_ID + SERVICE_NUMBER;
55
56     private Bridge bridge;
57
58     protected Bridge createBridge(ThingTypeUID bridgeTypeUID) {
59         Configuration configuration = new Configuration();
60         configuration.put(WemoBindingConstants.UDN, DEVICE_UDN);
61
62         ThingUID bridgeUID = new ThingUID(bridgeTypeUID, WEMO_BRIDGE_ID);
63
64         bridge = BridgeBuilder.create(bridgeTypeUID, bridgeUID).withConfiguration(configuration).build();
65
66         managedThingProvider.add(bridge);
67         return bridge;
68     }
69
70     @Override
71     protected Thing createThing(ThingTypeUID thingTypeUID, String channelID, String itemAcceptedType) {
72         Configuration configuration = new Configuration();
73         configuration.put(WemoBindingConstants.DEVICE_ID, WEMO_LIGHT_ID);
74
75         ThingUID thingUID = new ThingUID(thingTypeUID, TEST_THING_ID);
76
77         ChannelUID channelUID = new ChannelUID(thingUID, channelID);
78         Channel channel = ChannelBuilder.create(channelUID, itemAcceptedType).withType(DEFAULT_CHANNEL_TYPE_UID)
79                 .withKind(ChannelKind.STATE).withLabel("label").build();
80         ThingUID bridgeUID = new ThingUID(BRIDGE_TYPE_UID, WEMO_BRIDGE_ID);
81
82         thing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(configuration).withChannel(channel)
83                 .withBridge(bridgeUID).build();
84
85         managedThingProvider.add(thing);
86         return thing;
87     }
88
89     protected void removeThing() {
90         if (thing != null) {
91             Thing removedThing = thingRegistry.remove(thing.getUID());
92             assertThat(removedThing, is(notNullValue()));
93         }
94
95         waitForAssert(() -> {
96             assertThat(thing.getStatus(), is(ThingStatus.UNINITIALIZED));
97         });
98
99         if (bridge != null) {
100             Bridge bridgeThing = (Bridge) thingRegistry.remove(bridge.getUID());
101             assertThat(bridgeThing, is(notNullValue()));
102         }
103
104         waitForAssert(() -> {
105             assertThat(bridge.getStatus(), is(ThingStatus.UNINITIALIZED));
106         });
107     }
108 }