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