]> git.basschouten.com Git - openhab-addons.git/blob
4042a949626da3e36e3ed2178633c934aca4c845
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.fsinternetradio.test;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.net.MalformedURLException;
18 import java.net.URI;
19 import java.net.URL;
20
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;
37
38 /**
39  * OSGi tests for the {@link FSInternetRadioDiscoveryParticipant}.
40  *
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
44  *
45  */
46 public class FSInternetRadioDiscoveryParticipantJavaTest {
47     UpnpDiscoveryParticipant discoveryParticipant;
48
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;
53
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";
58     static {
59         try {
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);
65         }
66     }
67
68     /*
69      * The default radio is chosen from the {@link FrontierSiliconRadioDiscoveryParticipant}'s
70      * set of supported radios
71      */
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";
77
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);
82
83     @BeforeEach
84     public void setUp() {
85         discoveryParticipant = new FSInternetRadioDiscoveryParticipant();
86     }
87
88     /**
89      * Verify correct supported types.
90      */
91     @Test
92     public void correctSupportedTypes() {
93         assertEquals(1, discoveryParticipant.getSupportedThingTypeUIDs().size());
94         assertEquals(FSInternetRadioBindingConstants.THING_TYPE_RADIO,
95                 discoveryParticipant.getSupportedThingTypeUIDs().iterator().next());
96     }
97
98     /**
99      * Verify valid DiscoveryResult with completeFSInterntRadioDevice.
100      *
101      * @throws ValidationException
102      */
103     @Test
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));
113     }
114
115     /**
116      * Verify no discovery result for unknown device.
117      *
118      * @throws ValidationException
119      * @throws MalformedURLException
120      */
121     @Test
122     public void noDiscoveryResultIfUnknown() throws MalformedURLException, ValidationException {
123         RemoteDevice unknownRemoteDevice = createUnknownRemoteDevice();
124         assertNull(discoveryParticipant.createResult(unknownRemoteDevice));
125     }
126
127     /**
128      * Verify valid DiscoveryResult with FSInterntRadio device without base URL.
129      *
130      * @throws ValidationException
131      */
132     @Test
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));
142     }
143
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);
150
151         final RemoteService remoteService = null;
152         return new RemoteDevice(DEFAULT_RADIO_IDENTITY, DEFAULT_TYPE, deviceDetails, remoteService);
153     }
154
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";
164
165         DeviceDetails deviceDetails = new DeviceDetails(anotherBaseURL, friendlyName, manifacturerDetails, modelDetails,
166                 serialNumber, DEFAULT_UPC, DEFAULT_URI);
167
168         final RemoteService remoteService = null;
169         return new RemoteDevice(identity, DEFAULT_TYPE, deviceDetails, remoteService);
170     }
171 }