2 * Copyright (c) 2010-2023 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.http.WemoHttpCall;
44 import org.openhab.core.config.core.Configuration;
45 import org.openhab.core.io.transport.upnp.UpnpIOService;
46 import org.openhab.core.library.CoreItemFactory;
47 import org.openhab.core.test.java.JavaOSGiTest;
48 import org.openhab.core.test.storage.VolatileStorageService;
49 import org.openhab.core.thing.Channel;
50 import org.openhab.core.thing.ChannelUID;
51 import org.openhab.core.thing.ManagedThingProvider;
52 import org.openhab.core.thing.Thing;
53 import org.openhab.core.thing.ThingRegistry;
54 import org.openhab.core.thing.ThingTypeUID;
55 import org.openhab.core.thing.ThingUID;
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 final String DEVICE_MANUFACTURER = "Belkin";
74 // This port is included in the run configuration
75 private static final int ORG_OSGI_SERVICE_HTTP_PORT = 9090;
78 protected static final String TEST_THING_ID = "TestThing";
80 // UPnP Device information
81 public static final String DEVICE_UDN = "Test-1_0-22124";
83 private static final String DEVICE_TYPE = "Test";
84 private static final int DEVICE_VERSION = 1;
85 private static final String DEVICE_URL = "http://127.0.0.1:" + ORG_OSGI_SERVICE_HTTP_PORT;
86 private static final String DEVICE_DESCRIPTION_PATH = "/setup.xml";
88 protected static final String DEVICE_FRIENDLY_NAME = "WeMo Test";
89 protected static final String DEVICE_CONTROL_PATH = "/upnp/control/";
90 protected static final ChannelTypeUID DEFAULT_CHANNEL_TYPE_UID = new ChannelTypeUID(
91 WemoBindingConstants.BINDING_ID + ":channelType");
93 protected ManagedThingProvider managedThingProvider;
94 protected UpnpIOService upnpIOService;
95 protected ThingRegistry thingRegistry;
97 protected WemoHttpCall mockCaller;
98 protected MockUpnpService mockUpnpService;
100 protected Thing thing;
102 protected void setUpServices() throws IOException {
103 // StorageService is required from the ManagedThingProvider
104 VolatileStorageService volatileStorageService = new VolatileStorageService();
105 registerService(volatileStorageService);
107 // Mock the UPnP Service, that is required from the UPnP IO Service
108 mockUpnpService = new MockUpnpService(false, true);
109 mockUpnpService.startup();
110 registerService(mockUpnpService, UpnpService.class.getName());
112 managedThingProvider = getService(ManagedThingProvider.class);
113 assertThat(managedThingProvider, is(notNullValue()));
115 thingRegistry = getService(ThingRegistry.class);
116 assertThat(thingRegistry, is(notNullValue()));
118 // UPnP IO Service is required from the WemoDiscoveryService and WemoHandlerFactory
119 upnpIOService = getService(UpnpIOService.class);
120 assertThat(upnpIOService, is(notNullValue()));
122 mockCaller = Mockito.spy(new WemoHttpCall());
123 doReturn(true).when(mockCaller).probeURL(any());
124 WemoHttpCallFactory wemoHttpCallFactory = () -> mockCaller;
125 registerService(wemoHttpCallFactory, WemoHttpCallFactory.class.getName());
127 ChannelTypeProvider channelTypeProvider = mock(ChannelTypeProvider.class);
128 when(channelTypeProvider.getChannelType(any(ChannelTypeUID.class), nullable(Locale.class))).thenReturn(
129 ChannelTypeBuilder.state(DEFAULT_CHANNEL_TYPE_UID, "label", CoreItemFactory.SWITCH).build());
130 registerService(channelTypeProvider);
133 protected Thing createThing(ThingTypeUID thingTypeUID, String channelID, String itemAcceptedType) {
134 Configuration configuration = new Configuration();
135 configuration.put(WemoBindingConstants.UDN, DEVICE_UDN);
137 ThingUID thingUID = new ThingUID(thingTypeUID, TEST_THING_ID);
139 ChannelUID channelUID = new ChannelUID(thingUID, channelID);
140 Channel channel = ChannelBuilder.create(channelUID, itemAcceptedType).withType(DEFAULT_CHANNEL_TYPE_UID)
141 .withKind(ChannelKind.STATE).withLabel("label").build();
143 thing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(configuration).withChannel(channel)
146 managedThingProvider.add(thing);
150 protected void addUpnpDevice(String serviceTypeID, String serviceNumber, String modelName)
151 throws MalformedURLException, URISyntaxException, ValidationException {
152 UDN udn = new UDN(DEVICE_UDN);
153 URL deviceURL = new URL(DEVICE_URL + DEVICE_DESCRIPTION_PATH);
155 RemoteDeviceIdentity identity = new RemoteDeviceIdentity(udn,
156 WemoBindingConstants.SUBSCRIPTION_DURATION_SECONDS, deviceURL, new byte[1], null);
157 DeviceType type = new DeviceType(DEVICE_MANUFACTURER, DEVICE_TYPE, DEVICE_VERSION);
159 ManufacturerDetails manufacturerDetails = new ManufacturerDetails(DEVICE_MANUFACTURER);
160 ModelDetails modelDetails = new ModelDetails(modelName);
161 DeviceDetails details = new DeviceDetails(DEVICE_FRIENDLY_NAME, manufacturerDetails, modelDetails);
163 ServiceType serviceType = new ServiceType(DEVICE_MANUFACTURER, serviceTypeID);
164 ServiceId serviceId = new ServiceId(DEVICE_MANUFACTURER, serviceNumber);
166 // Use the same URI for control, event subscription and device description
167 URI mockURI = new URI(DEVICE_URL + DEVICE_DESCRIPTION_PATH);
168 URI descriptorURI = mockURI;
169 URI controlURI = mockURI;
170 URI eventSubscriptionURI = mockURI;
172 RemoteService service = new RemoteService(serviceType, serviceId, descriptorURI, controlURI,
173 eventSubscriptionURI);
175 RemoteDevice localDevice = new RemoteDevice(identity, type, details, service);
176 mockUpnpService.getRegistry().addDevice(localDevice);