]> git.basschouten.com Git - openhab-addons.git/blob
204dbd89ec6b4489927b0598dfc885926ac4b7bc
[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.boschshc.internal.discovery;
14
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.*;
20
21 import java.util.ArrayList;
22
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;
40
41 /**
42  * ThingDiscoveryService Tester.
43  *
44  * @author Gerd Zanker - Initial contribution
45  */
46 @ExtendWith(MockitoExtension.class)
47 @NonNullByDefault
48 class ThingDiscoveryServiceTest {
49
50     private @NonNullByDefault({}) ThingDiscoveryService fixture;
51
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;
56
57     @BeforeEach
58     void beforeEach() {
59         fixture = new ThingDiscoveryService();
60         fixture.addDiscoveryListener(discoveryListener);
61         fixture.setThingHandler(bridgeHandler);
62     }
63
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);
70     }
71
72     @Test
73     void testStartScan() throws InterruptedException {
74         mockBridgeCalls();
75
76         fixture.activate();
77         fixture.startScan();
78
79         verify(bridgeHandler).getRooms();
80         verify(bridgeHandler).getDevices();
81
82         fixture.stopScan();
83         fixture.deactivate();
84     }
85
86     @Test
87     void testStartScanWithoutBridgeHandler() {
88         mockBridgeCalls();
89
90         // No fixture.setThingHandler(bridgeHandler);
91         fixture.activate();
92         fixture.startScan();
93
94         // bridgeHandler not called, just no exception expected
95         fixture.stopScan();
96         fixture.deactivate();
97     }
98
99     @Test
100     void testSetGetThingHandler() {
101         fixture.setThingHandler(bridgeHandler);
102         assertThat(fixture.getThingHandler(), is(bridgeHandler));
103     }
104
105     @Test
106     void testAddDevices() {
107         mockBridgeCalls();
108
109         ArrayList<Device> devices = new ArrayList<>();
110         ArrayList<Room> emptyRooms = new ArrayList<>();
111
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);
122
123         verify(discoveryListener, never()).thingDiscovered(any(), any());
124
125         fixture.addDevices(devices, emptyRooms);
126
127         // two calls for the two devices expected
128         verify(discoveryListener, times(2)).thingDiscovered(any(), any());
129     }
130
131     @Test
132     void testAddDevicesWithNoDevices() {
133         ArrayList<Device> emptyDevices = new ArrayList<>();
134         ArrayList<Room> emptyRooms = new ArrayList<>();
135
136         verify(discoveryListener, never()).thingDiscovered(any(), any());
137
138         fixture.addDevices(emptyDevices, emptyRooms);
139
140         // nothing shall be discovered, but also no exception shall be thrown
141         verify(discoveryListener, never()).thingDiscovered(any(), any());
142     }
143
144     @Test
145     void testAddDevice() {
146         mockBridgeCalls();
147
148         Device device = new Device();
149         device.deviceModel = "TWINGUARD";
150         device.id = "testDevice:ID";
151         device.name = "Test Name";
152         fixture.addDevice(device, "TestRoom");
153
154         verify(discoveryListener).thingDiscovered(discoveryServiceCaptor.capture(), discoveryResultCaptor.capture());
155
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"));
164     }
165
166     @Test
167     void testAddDeviceWithNiceNameAndAppendedRoomName() {
168         assertDeviceNiceName("-RoomClimateControl-", "TestRoom", "Room Climate Control TestRoom");
169     }
170
171     @Test
172     void testAddDeviceWithNiceNameWithEmtpyRoomName() {
173         assertDeviceNiceName("-RoomClimateControl-", "", "Room Climate Control");
174     }
175
176     @Test
177     void testAddDeviceWithNiceNameWithoutAppendingRoomName() {
178         assertDeviceNiceName("-SmokeDetectionSystem-", "TestRoom", "Smoke Detection System");
179     }
180
181     @Test
182     void testAddDeviceWithNiceNameWithoutUsualName() {
183         assertDeviceNiceName("My other device", "TestRoom", "My other device");
184     }
185
186     private void assertDeviceNiceName(String deviceName, String roomName, String expectedNiceName) {
187         mockBridgeCalls();
188
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));
198     }
199
200     @Test
201     void testGetRoomForDevice() {
202         Device device = new Device();
203
204         ArrayList<Room> rooms = new ArrayList<>();
205         Room room1 = new Room();
206         room1.id = "r1";
207         room1.name = "Room1";
208         rooms.add(room1);
209         Room room2 = new Room();
210         room2.id = "r2";
211         room2.name = "Room 2";
212         rooms.add(room2);
213
214         device.roomId = "r1";
215         assertThat(fixture.getRoomNameForDevice(device, rooms), is("Room1"));
216
217         device.roomId = "r2";
218         assertThat(fixture.getRoomNameForDevice(device, rooms), is("Room 2"));
219
220         device.roomId = "unknown";
221         assertTrue(fixture.getRoomNameForDevice(device, rooms).isEmpty());
222     }
223
224     @Test
225     void testGetThingTypeUID() {
226         Device device = new Device();
227
228         device.deviceModel = "invalid";
229         assertNull(fixture.getThingTypeUID(device));
230
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));
236     }
237 }