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