2 * Copyright (c) 2010-2022 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.mockito.Mockito;
41 import org.openhab.binding.wemo.internal.WemoBindingConstants;
42 import org.openhab.binding.wemo.internal.WemoHttpCallFactory;
43 import org.openhab.binding.wemo.internal.WemoUtil;
44 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
45 import org.openhab.core.config.core.Configuration;
46 import org.openhab.core.io.transport.upnp.UpnpIOService;
47 import org.openhab.core.library.CoreItemFactory;
48 import org.openhab.core.test.java.JavaOSGiTest;
49 import org.openhab.core.test.storage.VolatileStorageService;
50 import org.openhab.core.thing.Channel;
51 import org.openhab.core.thing.ChannelUID;
52 import org.openhab.core.thing.ManagedThingProvider;
53 import org.openhab.core.thing.Thing;
54 import org.openhab.core.thing.ThingRegistry;
55 import org.openhab.core.thing.ThingTypeUID;
56 import org.openhab.core.thing.ThingUID;
57 import org.openhab.core.thing.binding.builder.ChannelBuilder;
58 import org.openhab.core.thing.binding.builder.ThingBuilder;
59 import org.openhab.core.thing.type.ChannelKind;
60 import org.openhab.core.thing.type.ChannelTypeBuilder;
61 import org.openhab.core.thing.type.ChannelTypeProvider;
62 import org.openhab.core.thing.type.ChannelTypeUID;
65 * Generic test class for all Wemo related tests that contains methods and constants used across the different test
68 * @author Svilen Valkanov - Initial contribution
69 * @author Stefan Triller - Ported Tests from Groovy to Java
71 public abstract class GenericWemoOSGiTest extends JavaOSGiTest {
73 public static final String DEVICE_MANUFACTURER = "Belkin";
75 // This port is included in the run configuration
76 private static final int ORG_OSGI_SERVICE_HTTP_PORT = 9090;
79 protected static final String TEST_THING_ID = "TestThing";
81 // UPnP Device information
82 public static final String DEVICE_UDN = "Test-1_0-22124";
84 private static final String DEVICE_TYPE = "Test";
85 private static final int DEVICE_VERSION = 1;
86 private static final String DEVICE_URL = "http://127.0.0.1:" + ORG_OSGI_SERVICE_HTTP_PORT;
87 private static final String DEVICE_DESCRIPTION_PATH = "/setup.xml";
89 protected static final String DEVICE_FRIENDLY_NAME = "WeMo Test";
90 protected static final String DEVICE_CONTROL_PATH = "/upnp/control/";
91 protected static 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 WemoHttpCall mockCaller;
99 protected MockUpnpService mockUpnpService;
101 protected Thing thing;
103 protected void setUpServices() throws IOException {
104 WemoUtil.serviceAvailableFunction = (host, port) -> true;
106 // StorageService is required from the ManagedThingProvider
107 VolatileStorageService volatileStorageService = new VolatileStorageService();
108 registerService(volatileStorageService);
110 // Mock the UPnP Service, that is required from the UPnP IO Service
111 mockUpnpService = new MockUpnpService(false, true);
112 mockUpnpService.startup();
113 registerService(mockUpnpService, UpnpService.class.getName());
115 managedThingProvider = getService(ManagedThingProvider.class);
116 assertThat(managedThingProvider, is(notNullValue()));
118 thingRegistry = getService(ThingRegistry.class);
119 assertThat(thingRegistry, is(notNullValue()));
121 // UPnP IO Service is required from the WemoDiscoveryService and WemoHandlerFactory
122 upnpIOService = getService(UpnpIOService.class);
123 assertThat(upnpIOService, is(notNullValue()));
125 mockCaller = Mockito.spy(new WemoHttpCall());
126 WemoHttpCallFactory wemoHttpCallFactory = () -> mockCaller;
127 registerService(wemoHttpCallFactory, WemoHttpCallFactory.class.getName());
129 ChannelTypeProvider channelTypeProvider = mock(ChannelTypeProvider.class);
130 when(channelTypeProvider.getChannelType(any(ChannelTypeUID.class), any(Locale.class))).thenReturn(
131 ChannelTypeBuilder.state(DEFAULT_CHANNEL_TYPE_UID, "label", CoreItemFactory.SWITCH).build());
132 registerService(channelTypeProvider);
135 protected Thing createThing(ThingTypeUID thingTypeUID, String channelID, String itemAcceptedType) {
136 Configuration configuration = new Configuration();
137 configuration.put(WemoBindingConstants.UDN, DEVICE_UDN);
139 ThingUID thingUID = new ThingUID(thingTypeUID, TEST_THING_ID);
141 ChannelUID channelUID = new ChannelUID(thingUID, channelID);
142 Channel channel = ChannelBuilder.create(channelUID, itemAcceptedType).withType(DEFAULT_CHANNEL_TYPE_UID)
143 .withKind(ChannelKind.STATE).withLabel("label").build();
145 thing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(configuration).withChannel(channel)
148 managedThingProvider.add(thing);
152 protected void addUpnpDevice(String serviceTypeID, String serviceNumber, String modelName)
153 throws MalformedURLException, URISyntaxException, ValidationException {
154 UDN udn = new UDN(DEVICE_UDN);
155 URL deviceURL = new URL(DEVICE_URL + DEVICE_DESCRIPTION_PATH);
157 RemoteDeviceIdentity identity = new RemoteDeviceIdentity(udn,
158 WemoBindingConstants.SUBSCRIPTION_DURATION_SECONDS, deviceURL, new byte[1], null);
159 DeviceType type = new DeviceType(DEVICE_MANUFACTURER, DEVICE_TYPE, DEVICE_VERSION);
161 ManufacturerDetails manufacturerDetails = new ManufacturerDetails(DEVICE_MANUFACTURER);
162 ModelDetails modelDetails = new ModelDetails(modelName);
163 DeviceDetails details = new DeviceDetails(DEVICE_FRIENDLY_NAME, manufacturerDetails, modelDetails);
165 ServiceType serviceType = new ServiceType(DEVICE_MANUFACTURER, serviceTypeID);
166 ServiceId serviceId = new ServiceId(DEVICE_MANUFACTURER, serviceNumber);
168 // Use the same URI for control, event subscription and device description
169 URI mockURI = new URI(DEVICE_URL + DEVICE_DESCRIPTION_PATH);
170 URI descriptorURI = mockURI;
171 URI controlURI = mockURI;
172 URI eventSubscriptionURI = mockURI;
174 RemoteService service = new RemoteService(serviceType, serviceId, descriptorURI, controlURI,
175 eventSubscriptionURI);
177 RemoteDevice localDevice = new RemoteDevice(identity, type, details, service);
178 mockUpnpService.getRegistry().addDevice(localDevice);