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