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.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.*;
21 import java.util.ArrayList;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Captor;
29 import org.mockito.Mock;
30 import org.mockito.junit.jupiter.MockitoExtension;
31 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
32 import org.openhab.binding.boschshc.internal.devices.bridge.BridgeHandler;
33 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Device;
34 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Room;
35 import org.openhab.core.config.discovery.DiscoveryListener;
36 import org.openhab.core.config.discovery.DiscoveryResult;
37 import org.openhab.core.config.discovery.DiscoveryService;
38 import org.openhab.core.thing.Bridge;
39 import org.openhab.core.thing.ThingUID;
42 * ThingDiscoveryService Tester.
44 * @author Gerd Zanker - Initial contribution
46 @ExtendWith(MockitoExtension.class)
48 class ThingDiscoveryServiceTest {
50 private @NonNullByDefault({}) ThingDiscoveryService fixture;
52 private @Mock @NonNullByDefault({}) BridgeHandler bridgeHandler;
53 private @Mock @NonNullByDefault({}) DiscoveryListener discoveryListener;
54 private @Captor @NonNullByDefault({}) ArgumentCaptor<DiscoveryService> discoveryServiceCaptor;
55 private @Captor @NonNullByDefault({}) ArgumentCaptor<DiscoveryResult> discoveryResultCaptor;
59 fixture = new ThingDiscoveryService();
60 fixture.addDiscoveryListener(discoveryListener);
61 fixture.setThingHandler(bridgeHandler);
64 private void mockBridgeCalls() {
65 // Set the Mock Bridge as the ThingHandler
66 ThingUID bridgeUID = new ThingUID(BoschSHCBindingConstants.THING_TYPE_SHC, "testSHC");
67 Bridge mockBridge = mock(Bridge.class);
68 when(mockBridge.getUID()).thenReturn(bridgeUID);
69 when(bridgeHandler.getThing()).thenReturn(mockBridge);
73 void testStartScan() throws InterruptedException {
79 verify(bridgeHandler).getRooms();
80 verify(bridgeHandler).getDevices();
87 void testStartScanWithoutBridgeHandler() {
90 // No fixture.setThingHandler(bridgeHandler);
94 // bridgeHandler not called, just no exception expected
100 void testSetGetThingHandler() {
101 fixture.setThingHandler(bridgeHandler);
102 assertThat(fixture.getThingHandler(), is(bridgeHandler));
106 void testAddDevices() {
109 ArrayList<Device> devices = new ArrayList<>();
110 ArrayList<Room> emptyRooms = new ArrayList<>();
112 Device device1 = new Device();
113 device1.deviceModel = "TWINGUARD";
114 device1.id = "testDevice:ID";
115 device1.name = "Test Name";
116 devices.add(device1);
117 Device device2 = new Device();
118 device2.deviceModel = "TWINGUARD";
119 device2.id = "testDevice:2";
120 device2.name = "Second device";
121 devices.add(device2);
123 verify(discoveryListener, never()).thingDiscovered(any(), any());
125 fixture.addDevices(devices, emptyRooms);
127 // two calls for the two devices expected
128 verify(discoveryListener, times(2)).thingDiscovered(any(), any());
132 void testAddDevicesWithNoDevices() {
133 ArrayList<Device> emptyDevices = new ArrayList<>();
134 ArrayList<Room> emptyRooms = new ArrayList<>();
136 verify(discoveryListener, never()).thingDiscovered(any(), any());
138 fixture.addDevices(emptyDevices, emptyRooms);
140 // nothing shall be discovered, but also no exception shall be thrown
141 verify(discoveryListener, never()).thingDiscovered(any(), any());
145 void testAddDevice() {
148 Device device = new Device();
149 device.deviceModel = "TWINGUARD";
150 device.id = "testDevice:ID";
151 device.name = "Test Name";
152 fixture.addDevice(device, "TestRoom");
154 verify(discoveryListener).thingDiscovered(discoveryServiceCaptor.capture(), discoveryResultCaptor.capture());
156 assertThat(discoveryServiceCaptor.getValue().getClass(), is(ThingDiscoveryService.class));
157 DiscoveryResult result = discoveryResultCaptor.getValue();
158 assertThat(result.getBindingId(), is(BoschSHCBindingConstants.BINDING_ID));
159 assertThat(result.getThingTypeUID(), is(BoschSHCBindingConstants.THING_TYPE_TWINGUARD));
160 assertThat(result.getThingUID().getId(), is("testDevice_ID"));
161 assertThat(result.getBridgeUID().getId(), is("testSHC"));
162 assertThat(result.getLabel(), is("Test Name"));
163 assertThat(String.valueOf(result.getProperties().get("Location")), is("TestRoom"));
167 void testAddDeviceWithNiceNameAndAppendedRoomName() {
168 assertDeviceNiceName("-RoomClimateControl-", "TestRoom", "Room Climate Control TestRoom");
172 void testAddDeviceWithNiceNameWithEmtpyRoomName() {
173 assertDeviceNiceName("-RoomClimateControl-", "", "Room Climate Control");
177 void testAddDeviceWithNiceNameWithoutAppendingRoomName() {
178 assertDeviceNiceName("-SmokeDetectionSystem-", "TestRoom", "Smoke Detection System");
182 void testAddDeviceWithNiceNameWithoutUsualName() {
183 assertDeviceNiceName("My other device", "TestRoom", "My other device");
186 private void assertDeviceNiceName(String deviceName, String roomName, String expectedNiceName) {
189 Device device = new Device();
190 device.deviceModel = "TWINGUARD";
191 device.id = "testDevice:ID";
192 device.name = deviceName;
193 fixture.addDevice(device, roomName);
194 verify(discoveryListener).thingDiscovered(discoveryServiceCaptor.capture(), discoveryResultCaptor.capture());
195 assertThat(discoveryServiceCaptor.getValue().getClass(), is(ThingDiscoveryService.class));
196 DiscoveryResult result = discoveryResultCaptor.getValue();
197 assertThat(result.getLabel(), is(expectedNiceName));
201 void testGetRoomForDevice() {
202 Device device = new Device();
204 ArrayList<Room> rooms = new ArrayList<>();
205 Room room1 = new Room();
207 room1.name = "Room1";
209 Room room2 = new Room();
211 room2.name = "Room 2";
214 device.roomId = "r1";
215 assertThat(fixture.getRoomNameForDevice(device, rooms), is("Room1"));
217 device.roomId = "r2";
218 assertThat(fixture.getRoomNameForDevice(device, rooms), is("Room 2"));
220 device.roomId = "unknown";
221 assertTrue(fixture.getRoomNameForDevice(device, rooms).isEmpty());
225 void testGetThingTypeUID() {
226 Device device = new Device();
228 device.deviceModel = "invalid";
229 assertNull(fixture.getThingTypeUID(device));
231 // just two spot checks
232 device.deviceModel = "BBL";
233 assertThat(fixture.getThingTypeUID(device), is(BoschSHCBindingConstants.THING_TYPE_SHUTTER_CONTROL));
234 device.deviceModel = "TWINGUARD";
235 assertThat(fixture.getThingTypeUID(device), is(BoschSHCBindingConstants.THING_TYPE_TWINGUARD));