]> git.basschouten.com Git - openhab-addons.git/blob
7ef44d4af6de9d969c3d13b6b54d70de05424ffe
[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.junit.Assert.assertThat;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.io.IOException;
21 import java.net.MalformedURLException;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.net.URL;
25 import java.util.Locale;
26
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.library.CoreItemFactory;
29 import org.openhab.core.thing.Channel;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.ManagedThingProvider;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingRegistry;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.builder.ChannelBuilder;
38 import org.openhab.core.thing.binding.builder.ThingBuilder;
39 import org.openhab.core.thing.type.ChannelKind;
40 import org.openhab.core.thing.type.ChannelTypeBuilder;
41 import org.openhab.core.thing.type.ChannelTypeProvider;
42 import org.openhab.core.thing.type.ChannelTypeUID;
43 import org.openhab.core.io.transport.upnp.UpnpIOService;
44 import org.openhab.core.test.java.JavaOSGiTest;
45 import org.openhab.core.test.storage.VolatileStorageService;
46 import org.jupnp.UpnpService;
47 import org.jupnp.mock.MockUpnpService;
48 import org.jupnp.model.ValidationException;
49 import org.jupnp.model.meta.DeviceDetails;
50 import org.jupnp.model.meta.ManufacturerDetails;
51 import org.jupnp.model.meta.ModelDetails;
52 import org.jupnp.model.meta.RemoteDevice;
53 import org.jupnp.model.meta.RemoteDeviceIdentity;
54 import org.jupnp.model.meta.RemoteService;
55 import org.jupnp.model.types.DeviceType;
56 import org.jupnp.model.types.ServiceId;
57 import org.jupnp.model.types.ServiceType;
58 import org.jupnp.model.types.UDN;
59 import org.openhab.binding.wemo.internal.WemoBindingConstants;
60 import org.openhab.binding.wemo.internal.handler.AbstractWemoHandler;
61 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
62
63 /**
64  * Generic test class for all Wemo related tests that contains methods and constants used across the different test
65  * classes
66  *
67  * @author Svilen Valkanov - Initial contribution
68  * @author Stefan Triller - Ported Tests from Groovy to Java
69  */
70 public abstract class GenericWemoOSGiTest extends JavaOSGiTest {
71
72     public static MockUpnpService mockUpnpService;
73     public static final String DEVICE_MANUFACTURER = "Belkin";
74
75     // This port is included in the run configuration
76     private final int ORG_OSGI_SERVICE_HTTP_PORT = 9090;
77
78     // Thing information
79     protected String TEST_THING_ID = "TestThing";
80
81     // UPnP Device information
82     public static String DEVICE_UDN = "Test-1_0-22124";
83
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";
88
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");
93
94     protected ManagedThingProvider managedThingProvider;
95     protected UpnpIOService upnpIOService;
96     protected ThingRegistry thingRegistry;
97
98     protected Thing thing;
99
100     protected void setUpServices() throws IOException {
101         // StorageService is required from the ManagedThingProvider
102         VolatileStorageService volatileStorageService = new VolatileStorageService();
103         registerService(volatileStorageService);
104
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());
109
110         managedThingProvider = getService(ManagedThingProvider.class);
111         assertThat(managedThingProvider, is(notNullValue()));
112
113         thingRegistry = getService(ThingRegistry.class);
114         assertThat(thingRegistry, is(notNullValue()));
115
116         // UPnP IO Service is required from the WemoDiscoveryService and WemoHandlerFactory
117         upnpIOService = getService(UpnpIOService.class);
118         assertThat(upnpIOService, is(notNullValue()));
119
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);
124     }
125
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);
130
131         ThingUID thingUID = new ThingUID(thingTypeUID, TEST_THING_ID);
132
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();
136
137         thing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(configuration).withChannel(channel)
138                 .build();
139
140         managedThingProvider.add(thing);
141
142         ThingHandler handler = thing.getHandler();
143         if (handler != null) {
144             AbstractWemoHandler h = (AbstractWemoHandler) handler;
145             h.setWemoHttpCaller(wemoHttpCaller);
146         }
147
148         return thing;
149     }
150
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);
155
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);
159
160         ManufacturerDetails manufacturerDetails = new ManufacturerDetails(DEVICE_MANUFACTURER);
161         ModelDetails modelDetails = new ModelDetails(modelName);
162         DeviceDetails details = new DeviceDetails(DEVICE_FRIENDLY_NAME, manufacturerDetails, modelDetails);
163
164         ServiceType serviceType = new ServiceType(DEVICE_MANUFACTURER, serviceTypeID);
165         ServiceId serviceId = new ServiceId(DEVICE_MANUFACTURER, serviceNumber);
166
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;
172
173         RemoteService service = new RemoteService(serviceType, serviceId, descriptorURI, controlURI,
174                 eventSubscriptionURI);
175
176         RemoteDevice localDevice = new RemoteDevice(identity, type, details, service);
177         mockUpnpService.getRegistry().addDevice(localDevice);
178     }
179 }