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.boschshc.internal.discovery;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
18 import static org.mockito.Mockito.*;
20 import java.util.ArrayList;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Captor;
28 import org.mockito.Mock;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
31 import org.openhab.binding.boschshc.internal.devices.bridge.BridgeHandler;
32 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Device;
33 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Room;
34 import org.openhab.core.config.discovery.DiscoveryListener;
35 import org.openhab.core.config.discovery.DiscoveryResult;
36 import org.openhab.core.config.discovery.DiscoveryService;
37 import org.openhab.core.thing.Bridge;
38 import org.openhab.core.thing.ThingUID;
41 * ThingDiscoveryService Tester.
43 * @author Gerd Zanker - Initial contribution
45 @ExtendWith(MockitoExtension.class)
47 public class ThingDiscoveryServiceTest {
49 private @NonNullByDefault({}) ThingDiscoveryService fixture;
51 private @Mock @NonNullByDefault({}) BridgeHandler bridgeHandler;
52 private @Mock @NonNullByDefault({}) DiscoveryListener discoveryListener;
53 private @Captor @NonNullByDefault({}) ArgumentCaptor<DiscoveryService> discoveryServiceCaptor;
54 private @Captor @NonNullByDefault({}) ArgumentCaptor<DiscoveryResult> discoveryResultCaptor;
58 fixture = new ThingDiscoveryService();
59 fixture.addDiscoveryListener(discoveryListener);
60 fixture.setThingHandler(bridgeHandler);
63 private void mockBridgeCalls() {
64 // Set the Mock Bridge as the ThingHandler
65 ThingUID bridgeUID = new ThingUID(BoschSHCBindingConstants.THING_TYPE_SHC, "testSHC");
66 Bridge mockBridge = mock(Bridge.class);
67 when(mockBridge.getUID()).thenReturn(bridgeUID);
68 when(bridgeHandler.getThing()).thenReturn(mockBridge);
72 public void testStartScan() throws InterruptedException {
78 verify(bridgeHandler).getRooms();
79 verify(bridgeHandler).getDevices();
86 public void testStartScanWithoutBridgeHandler() {
89 // No fixture.setThingHandler(bridgeHandler);
93 // bridgeHandler not called, just no exception expected
99 public void testSetGetThingHandler() {
100 fixture.setThingHandler(bridgeHandler);
101 assertThat(fixture.getThingHandler(), is(bridgeHandler));
105 public void testAddDevices() {
108 ArrayList<Device> devices = new ArrayList<>();
109 ArrayList<Room> emptyRooms = new ArrayList<>();
111 Device device1 = new Device();
112 device1.deviceModel = "TWINGUARD";
113 device1.id = "testDevice:ID";
114 device1.name = "Test Name";
115 devices.add(device1);
116 Device device2 = new Device();
117 device2.deviceModel = "TWINGUARD";
118 device2.id = "testDevice:2";
119 device2.name = "Second device";
120 devices.add(device2);
122 verify(discoveryListener, never()).thingDiscovered(any(), any());
124 fixture.addDevices(devices, emptyRooms);
126 // two calls for the two devices expected
127 verify(discoveryListener, times(2)).thingDiscovered(any(), any());
131 public void testAddDevicesWithNoDevices() {
132 ArrayList<Device> emptyDevices = new ArrayList<>();
133 ArrayList<Room> emptyRooms = new ArrayList<>();
135 verify(discoveryListener, never()).thingDiscovered(any(), any());
137 fixture.addDevices(emptyDevices, emptyRooms);
139 // nothing shall be discovered, but also no exception shall be thrown
140 verify(discoveryListener, never()).thingDiscovered(any(), any());
144 public void testAddDevice() {
147 Device device = new Device();
148 device.deviceModel = "TWINGUARD";
149 device.id = "testDevice:ID";
150 device.name = "Test Name";
151 fixture.addDevice(device, "TestRoom");
153 verify(discoveryListener).thingDiscovered(discoveryServiceCaptor.capture(), discoveryResultCaptor.capture());
155 assertThat(discoveryServiceCaptor.getValue().getClass(), is(ThingDiscoveryService.class));
156 DiscoveryResult result = discoveryResultCaptor.getValue();
157 assertThat(result.getBindingId(), is(BoschSHCBindingConstants.BINDING_ID));
158 assertThat(result.getThingTypeUID(), is(BoschSHCBindingConstants.THING_TYPE_TWINGUARD));
159 assertThat(result.getThingUID().getId(), is("testDevice_ID"));
160 assertThat(result.getBridgeUID().getId(), is("testSHC"));
161 assertThat(result.getLabel(), is("Test Name"));
162 assertThat(result.getProperties().get("Location").toString(), is("TestRoom"));
166 public void testAddDeviceWithNiceNameAndAppendedRoomName() {
167 assertDeviceNiceName("-RoomClimateControl-", "TestRoom", "Room Climate Control TestRoom");
171 public void testAddDeviceWithNiceNameWithEmtpyRoomName() {
172 assertDeviceNiceName("-RoomClimateControl-", "", "Room Climate Control");
176 public void testAddDeviceWithNiceNameWithoutAppendingRoomName() {
177 assertDeviceNiceName("-SmokeDetectionSystem-", "TestRoom", "Smoke Detection System");
181 public void testAddDeviceWithNiceNameWithoutUsualName() {
182 assertDeviceNiceName("My other device", "TestRoom", "My other device");
185 private void assertDeviceNiceName(String deviceName, String roomName, String expectedNiceName) {
188 Device device = new Device();
189 device.deviceModel = "TWINGUARD";
190 device.id = "testDevice:ID";
191 device.name = deviceName;
192 fixture.addDevice(device, roomName);
193 verify(discoveryListener).thingDiscovered(discoveryServiceCaptor.capture(), discoveryResultCaptor.capture());
194 assertThat(discoveryServiceCaptor.getValue().getClass(), is(ThingDiscoveryService.class));
195 DiscoveryResult result = discoveryResultCaptor.getValue();
196 assertThat(result.getLabel(), is(expectedNiceName));
200 public void testGetRoomForDevice() {
201 Device device = new Device();
203 ArrayList<Room> rooms = new ArrayList<>();
204 Room room1 = new Room();
206 room1.name = "Room1";
208 Room room2 = new Room();
210 room2.name = "Room 2";
213 device.roomId = "r1";
214 assertThat(fixture.getRoomNameForDevice(device, rooms), is("Room1"));
216 device.roomId = "r2";
217 assertThat(fixture.getRoomNameForDevice(device, rooms), is("Room 2"));
219 device.roomId = "unknown";
220 assertTrue(fixture.getRoomNameForDevice(device, rooms).isEmpty());
224 public void testGetThingTypeUID() {
225 Device device = new Device();
227 device.deviceModel = "invalid";
228 assertNull(fixture.getThingTypeUID(device));
230 // just two spot checks
231 device.deviceModel = "BBL";
232 assertThat(fixture.getThingTypeUID(device), is(BoschSHCBindingConstants.THING_TYPE_SHUTTER_CONTROL));
233 device.deviceModel = "TWINGUARD";
234 assertThat(fixture.getThingTypeUID(device), is(BoschSHCBindingConstants.THING_TYPE_TWINGUARD));