2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.wemo.internal.test;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
20 import java.io.IOException;
21 import java.net.MalformedURLException;
23 import java.net.URISyntaxException;
25 import java.util.Locale;
27 import org.jupnp.UpnpService;
28 import org.jupnp.mock.MockUpnpService;
29 import org.jupnp.model.ValidationException;
30 import org.jupnp.model.meta.DeviceDetails;
31 import org.jupnp.model.meta.ManufacturerDetails;
32 import org.jupnp.model.meta.ModelDetails;
33 import org.jupnp.model.meta.RemoteDevice;
34 import org.jupnp.model.meta.RemoteDeviceIdentity;
35 import org.jupnp.model.meta.RemoteService;
36 import org.jupnp.model.types.DeviceType;
37 import org.jupnp.model.types.ServiceId;
38 import org.jupnp.model.types.ServiceType;
39 import org.jupnp.model.types.UDN;
40 import org.openhab.binding.wemo.internal.WemoBindingConstants;
41 import org.openhab.binding.wemo.internal.handler.AbstractWemoHandler;
42 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
43 import org.openhab.core.config.core.Configuration;
44 import org.openhab.core.io.transport.upnp.UpnpIOService;
45 import org.openhab.core.library.CoreItemFactory;
46 import org.openhab.core.test.java.JavaOSGiTest;
47 import org.openhab.core.test.storage.VolatileStorageService;
48 import org.openhab.core.thing.Channel;
49 import org.openhab.core.thing.ChannelUID;
50 import org.openhab.core.thing.ManagedThingProvider;
51 import org.openhab.core.thing.Thing;
52 import org.openhab.core.thing.ThingRegistry;
53 import org.openhab.core.thing.ThingTypeUID;
54 import org.openhab.core.thing.ThingUID;
55 import org.openhab.core.thing.binding.ThingHandler;
56 import org.openhab.core.thing.binding.builder.ChannelBuilder;
57 import org.openhab.core.thing.binding.builder.ThingBuilder;
58 import org.openhab.core.thing.type.ChannelKind;
59 import org.openhab.core.thing.type.ChannelTypeBuilder;
60 import org.openhab.core.thing.type.ChannelTypeProvider;
61 import org.openhab.core.thing.type.ChannelTypeUID;
64 * Generic test class for all Wemo related tests that contains methods and constants used across the different test
67 * @author Svilen Valkanov - Initial contribution
68 * @author Stefan Triller - Ported Tests from Groovy to Java
70 public abstract class GenericWemoOSGiTest extends JavaOSGiTest {
72 public static MockUpnpService mockUpnpService;
73 public static final String DEVICE_MANUFACTURER = "Belkin";
75 // This port is included in the run configuration
76 private final int ORG_OSGI_SERVICE_HTTP_PORT = 9090;
79 protected String TEST_THING_ID = "TestThing";
81 // UPnP Device information
82 public static String DEVICE_UDN = "Test-1_0-22124";
84 private final String DEVICE_TYPE = "Test";
85 private final int DEVICE_VERSION = 1;
86 private final String DEVICE_URL = "http://127.0.0.1:" + ORG_OSGI_SERVICE_HTTP_PORT;
87 private final String DEVICE_DESCRIPTION_PATH = "/setup.xml";
89 protected final String DEVICE_FRIENDLY_NAME = "WeMo Test";
90 protected final String DEVICE_CONTROL_PATH = "/upnp/control/";
91 protected final ChannelTypeUID DEFAULT_CHANNEL_TYPE_UID = new ChannelTypeUID(
92 WemoBindingConstants.BINDING_ID + ":channelType");
94 protected ManagedThingProvider managedThingProvider;
95 protected UpnpIOService upnpIOService;
96 protected ThingRegistry thingRegistry;
98 protected Thing thing;
100 protected void setUpServices() throws IOException {
101 // StorageService is required from the ManagedThingProvider
102 VolatileStorageService volatileStorageService = new VolatileStorageService();
103 registerService(volatileStorageService);
105 // Mock the UPnP Service, that is required from the UPnP IO Service
106 mockUpnpService = new MockUpnpService(false, true);
107 mockUpnpService.startup();
108 registerService(mockUpnpService, UpnpService.class.getName());
110 managedThingProvider = getService(ManagedThingProvider.class);
111 assertThat(managedThingProvider, is(notNullValue()));
113 thingRegistry = getService(ThingRegistry.class);
114 assertThat(thingRegistry, is(notNullValue()));
116 // UPnP IO Service is required from the WemoDiscoveryService and WemoHandlerFactory
117 upnpIOService = getService(UpnpIOService.class);
118 assertThat(upnpIOService, is(notNullValue()));
120 ChannelTypeProvider channelTypeProvider = mock(ChannelTypeProvider.class);
121 when(channelTypeProvider.getChannelType(any(ChannelTypeUID.class), any(Locale.class))).thenReturn(
122 ChannelTypeBuilder.state(DEFAULT_CHANNEL_TYPE_UID, "label", CoreItemFactory.SWITCH).build());
123 registerService(channelTypeProvider);
126 protected Thing createThing(ThingTypeUID thingTypeUID, String channelID, String itemAcceptedType,
127 WemoHttpCall wemoHttpCaller) {
128 Configuration configuration = new Configuration();
129 configuration.put(WemoBindingConstants.UDN, DEVICE_UDN);
131 ThingUID thingUID = new ThingUID(thingTypeUID, TEST_THING_ID);
133 ChannelUID channelUID = new ChannelUID(thingUID, channelID);
134 Channel channel = ChannelBuilder.create(channelUID, itemAcceptedType).withType(DEFAULT_CHANNEL_TYPE_UID)
135 .withKind(ChannelKind.STATE).withLabel("label").build();
137 thing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(configuration).withChannel(channel)
140 managedThingProvider.add(thing);
142 ThingHandler handler = thing.getHandler();
143 if (handler != null) {
144 AbstractWemoHandler h = (AbstractWemoHandler) handler;
145 h.setWemoHttpCaller(wemoHttpCaller);
151 protected void addUpnpDevice(String serviceTypeID, String serviceNumber, String modelName)
152 throws MalformedURLException, URISyntaxException, ValidationException {
153 UDN udn = new UDN(DEVICE_UDN);
154 URL deviceURL = new URL(DEVICE_URL + DEVICE_DESCRIPTION_PATH);
156 RemoteDeviceIdentity identity = new RemoteDeviceIdentity(udn, WemoBindingConstants.SUBSCRIPTION_DURATION,
157 deviceURL, new byte[1], null);
158 DeviceType type = new DeviceType(DEVICE_MANUFACTURER, DEVICE_TYPE, DEVICE_VERSION);
160 ManufacturerDetails manufacturerDetails = new ManufacturerDetails(DEVICE_MANUFACTURER);
161 ModelDetails modelDetails = new ModelDetails(modelName);
162 DeviceDetails details = new DeviceDetails(DEVICE_FRIENDLY_NAME, manufacturerDetails, modelDetails);
164 ServiceType serviceType = new ServiceType(DEVICE_MANUFACTURER, serviceTypeID);
165 ServiceId serviceId = new ServiceId(DEVICE_MANUFACTURER, serviceNumber);
167 // Use the same URI for control, event subscription and device description
168 URI mockURI = new URI(DEVICE_URL + DEVICE_DESCRIPTION_PATH);
169 URI descriptorURI = mockURI;
170 URI controlURI = mockURI;
171 URI eventSubscriptionURI = mockURI;
173 RemoteService service = new RemoteService(serviceType, serviceId, descriptorURI, controlURI,
174 eventSubscriptionURI);
176 RemoteDevice localDevice = new RemoteDevice(identity, type, details, service);
177 mockUpnpService.getRegistry().addDevice(localDevice);