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.fsinternetradio.test;
15 import static org.junit.jupiter.api.Assertions.*;
17 import java.net.MalformedURLException;
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23 import org.jupnp.model.ValidationException;
24 import org.jupnp.model.meta.DeviceDetails;
25 import org.jupnp.model.meta.ManufacturerDetails;
26 import org.jupnp.model.meta.ModelDetails;
27 import org.jupnp.model.meta.RemoteDevice;
28 import org.jupnp.model.meta.RemoteDeviceIdentity;
29 import org.jupnp.model.meta.RemoteService;
30 import org.jupnp.model.types.DeviceType;
31 import org.jupnp.model.types.UDN;
32 import org.openhab.binding.fsinternetradio.internal.FSInternetRadioBindingConstants;
33 import org.openhab.binding.fsinternetradio.internal.FSInternetRadioDiscoveryParticipant;
34 import org.openhab.core.config.discovery.DiscoveryResult;
35 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
36 import org.openhab.core.thing.ThingUID;
39 * OSGi tests for the {@link FSInternetRadioDiscoveryParticipant}.
41 * @author Mihaela Memova - Initial contribution
42 * @author Markus Rathgeb - Migrated from Groovy to pure Java test, made more robust
43 * @author Velin Yordanov - Migrated to mockito
46 public class FSInternetRadioDiscoveryParticipantJavaTest {
47 UpnpDiscoveryParticipant discoveryParticipant;
49 // default device variables used in the tests
50 DeviceType DEFAULT_TYPE = new DeviceType("namespace", "type");
51 String DEFAULT_UPC = "upc";
52 URI DEFAULT_URI = null;
54 // default radio variables used in most of the tests
55 private static final RemoteDeviceIdentity DEFAULT_RADIO_IDENTITY;
56 private static final URL DEFAULT_RADIO_BASE_URL;
57 String DEFAULT_RADIO_NAME = "HamaRadio";
60 DEFAULT_RADIO_IDENTITY = new RemoteDeviceIdentity(new UDN("radioUDN"), 60,
61 new URL("http://radioDescriptiveURL"), null, null);
62 DEFAULT_RADIO_BASE_URL = new URL("http://radioBaseURL");
63 } catch (final MalformedURLException ex) {
64 throw new Error("Initialization error", ex);
69 * The default radio is chosen from the {@link FrontierSiliconRadioDiscoveryParticipant}'s
70 * set of supported radios
72 String DEFAULT_RADIO_MANIFACTURER = "HAMA";
73 String DEFAULT_RADIO_MODEL_NAME = "IR";
74 String DEFAULT_RADIO_MODEL_DESCRIPTION = "IR Radio";
75 String DEFAULT_RADIO_MODEL_NUMBER = "IR100";
76 String DEFAULT_RADIO_SERIAL_NUMBER = "serialNumber123";
78 String RADIO_BINDING_ID = "fsinternetradio"; // taken from the binding.xml file
79 String RADIO_THING_TYPE_ID = "radio"; // taken from the thing-types.xml file
80 String DEFAULT_RADIO_THING_UID = String.format("%s:%s:%s", RADIO_BINDING_ID, RADIO_THING_TYPE_ID,
81 DEFAULT_RADIO_SERIAL_NUMBER);
85 discoveryParticipant = new FSInternetRadioDiscoveryParticipant();
89 * Verify correct supported types.
92 public void correctSupportedTypes() {
93 assertEquals(1, discoveryParticipant.getSupportedThingTypeUIDs().size());
94 assertEquals(FSInternetRadioBindingConstants.THING_TYPE_RADIO,
95 discoveryParticipant.getSupportedThingTypeUIDs().iterator().next());
99 * Verify valid DiscoveryResult with completeFSInterntRadioDevice.
101 * @throws ValidationException
104 public void validDiscoveryResultWithComplete() throws ValidationException {
105 RemoteDevice completeFSInternetRadioDevice = createDefaultFSInternetRadioDevice(DEFAULT_RADIO_BASE_URL);
106 final DiscoveryResult result = discoveryParticipant.createResult(completeFSInternetRadioDevice);
107 assertEquals(new ThingUID(DEFAULT_RADIO_THING_UID), result.getThingUID());
108 assertEquals(FSInternetRadioBindingConstants.THING_TYPE_RADIO, result.getThingTypeUID());
109 assertEquals(DEFAULT_RADIO_MANIFACTURER,
110 result.getProperties().get(FSInternetRadioBindingConstants.PROPERTY_MANUFACTURER));
111 assertEquals(DEFAULT_RADIO_MODEL_NUMBER,
112 result.getProperties().get(FSInternetRadioBindingConstants.PROPERTY_MODEL));
116 * Verify no discovery result for unknown device.
118 * @throws ValidationException
119 * @throws MalformedURLException
122 public void noDiscoveryResultIfUnknown() throws MalformedURLException, ValidationException {
123 RemoteDevice unknownRemoteDevice = createUnknownRemoteDevice();
124 assertNull(discoveryParticipant.createResult(unknownRemoteDevice));
128 * Verify valid DiscoveryResult with FSInterntRadio device without base URL.
130 * @throws ValidationException
133 public void validDiscoveryResultIfWithoutBaseUrl() throws ValidationException {
134 RemoteDevice fsInternetRadioDeviceWithoutUrl = createDefaultFSInternetRadioDevice(null);
135 final DiscoveryResult result = discoveryParticipant.createResult(fsInternetRadioDeviceWithoutUrl);
136 assertEquals(new ThingUID(DEFAULT_RADIO_THING_UID), result.getThingUID());
137 assertEquals(FSInternetRadioBindingConstants.THING_TYPE_RADIO, result.getThingTypeUID());
138 assertEquals(DEFAULT_RADIO_MANIFACTURER,
139 result.getProperties().get(FSInternetRadioBindingConstants.PROPERTY_MANUFACTURER));
140 assertEquals(DEFAULT_RADIO_MODEL_NUMBER,
141 result.getProperties().get(FSInternetRadioBindingConstants.PROPERTY_MODEL));
144 private RemoteDevice createDefaultFSInternetRadioDevice(URL baseURL) throws ValidationException {
145 ManufacturerDetails manifacturerDetails = new ManufacturerDetails(DEFAULT_RADIO_MANIFACTURER);
146 ModelDetails modelDetails = new ModelDetails(DEFAULT_RADIO_MODEL_NAME, DEFAULT_RADIO_MODEL_DESCRIPTION,
147 DEFAULT_RADIO_MODEL_NUMBER);
148 DeviceDetails deviceDetails = new DeviceDetails(baseURL, DEFAULT_RADIO_NAME, manifacturerDetails, modelDetails,
149 DEFAULT_RADIO_SERIAL_NUMBER, DEFAULT_UPC, DEFAULT_URI);
151 final RemoteService remoteService = null;
152 return new RemoteDevice(DEFAULT_RADIO_IDENTITY, DEFAULT_TYPE, deviceDetails, remoteService);
155 private RemoteDevice createUnknownRemoteDevice() throws ValidationException, MalformedURLException {
156 int deviceIdentityMaxAgeSeconds = 60;
157 RemoteDeviceIdentity identity = new RemoteDeviceIdentity(new UDN("unknownUDN"), deviceIdentityMaxAgeSeconds,
158 new URL("http://unknownDescriptorURL"), null, null);
159 URL anotherBaseURL = new URL("http://unknownBaseUrl");
160 String friendlyName = "Unknown remote device";
161 ManufacturerDetails manifacturerDetails = new ManufacturerDetails("UnknownManifacturer");
162 ModelDetails modelDetails = new ModelDetails("unknownModel");
163 String serialNumber = "unknownSerialNumber";
165 DeviceDetails deviceDetails = new DeviceDetails(anotherBaseURL, friendlyName, manifacturerDetails, modelDetails,
166 serialNumber, DEFAULT_UPC, DEFAULT_URI);
168 final RemoteService remoteService = null;
169 return new RemoteDevice(identity, DEFAULT_TYPE, deviceDetails, remoteService);