]> git.basschouten.com Git - openhab-addons.git/blob
da616efa59a45a4623f133da35692b734a2ca239
[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.homematic.internal.discovery;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
19 import static org.openhab.binding.homematic.test.util.BridgeHelper.createHomematicBridge;
20 import static org.openhab.binding.homematic.test.util.DimmerHelper.createDimmerHmDevice;
21
22 import java.io.IOException;
23
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.homematic.internal.communicator.HomematicGateway;
27 import org.openhab.binding.homematic.internal.handler.HomematicBridgeHandler;
28 import org.openhab.binding.homematic.internal.model.HmDevice;
29 import org.openhab.binding.homematic.internal.type.HomematicTypeGenerator;
30 import org.openhab.binding.homematic.test.util.SimpleDiscoveryListener;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.test.java.JavaTest;
33 import org.openhab.core.thing.Bridge;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.ThingStatusInfo;
37
38 /**
39  * Tests for {@link HomematicDeviceDiscoveryServiceTest}.
40  *
41  * @author Florian Stolte - Initial Contribution
42  *
43  */
44 public class HomematicDeviceDiscoveryServiceTest extends JavaTest {
45
46     private HomematicDeviceDiscoveryService homematicDeviceDiscoveryService;
47     private HomematicBridgeHandler homematicBridgeHandler;
48
49     @BeforeEach
50     public void setup() throws IOException {
51         this.homematicBridgeHandler = mockHomematicBridgeHandler();
52         this.homematicDeviceDiscoveryService = new HomematicDeviceDiscoveryService();
53         this.homematicDeviceDiscoveryService.setThingHandler(homematicBridgeHandler);
54     }
55
56     private HomematicBridgeHandler mockHomematicBridgeHandler() throws IOException {
57         HomematicBridgeHandler homematicBridgeHandler = mock(HomematicBridgeHandler.class);
58         Bridge bridge = createHomematicBridge();
59         HomematicGateway homematicGateway = mockHomematicGateway();
60         HomematicTypeGenerator homematicTypeGenerator = mockHomematicTypeGenerator();
61
62         when(homematicBridgeHandler.getThing()).thenReturn(bridge);
63         when(homematicBridgeHandler.getGateway()).thenReturn(homematicGateway);
64         when(homematicBridgeHandler.getTypeGenerator()).thenReturn(homematicTypeGenerator);
65
66         return homematicBridgeHandler;
67     }
68
69     private HomematicGateway mockHomematicGateway() throws IOException {
70         HomematicGateway homematicGateway = mock(HomematicGateway.class);
71
72         when(homematicGateway.getInstallMode()).thenReturn(60);
73
74         return homematicGateway;
75     }
76
77     private HomematicTypeGenerator mockHomematicTypeGenerator() {
78         return mock(HomematicTypeGenerator.class);
79     }
80
81     @Test
82     public void testDiscoveryResultIsReportedForNewDevice() {
83         SimpleDiscoveryListener discoveryListener = new SimpleDiscoveryListener();
84         homematicDeviceDiscoveryService.addDiscoveryListener(discoveryListener);
85
86         HmDevice hmDevice = createDimmerHmDevice();
87         homematicDeviceDiscoveryService.deviceDiscovered(hmDevice);
88
89         assertThat(discoveryListener.discoveredResults.size(), is(1));
90         discoveryResultMatchesHmDevice(discoveryListener.discoveredResults.element(), hmDevice);
91     }
92
93     @Test
94     public void testDevicesAreLoadedFromBridgeDuringDiscovery() throws IOException {
95         startScanAndWaitForLoadedDevices();
96
97         verify(homematicBridgeHandler.getGateway()).loadAllDeviceMetadata();
98     }
99
100     @Test
101     public void testInstallModeIsNotActiveDuringInitialDiscovery() throws IOException {
102         startScanAndWaitForLoadedDevices();
103
104         verify(homematicBridgeHandler.getGateway(), never()).setInstallMode(eq(true), anyInt());
105     }
106
107     @Test
108     public void testInstallModeIsActiveDuringSubsequentDiscovery() throws IOException {
109         homematicBridgeHandler.getThing()
110                 .setStatusInfo(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, ""));
111
112         startScanAndWaitForLoadedDevices();
113
114         verify(homematicBridgeHandler.getGateway()).setInstallMode(true, 60);
115     }
116
117     @Test
118     public void testStoppingDiscoveryDisablesInstallMode() throws IOException {
119         homematicBridgeHandler.getThing()
120                 .setStatusInfo(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, ""));
121         homematicDeviceDiscoveryService.startScan();
122
123         homematicDeviceDiscoveryService.stopScan();
124
125         verify(homematicBridgeHandler.getGateway()).setInstallMode(false, 0);
126     }
127
128     private void startScanAndWaitForLoadedDevices() {
129         homematicDeviceDiscoveryService.startScan();
130         waitForAssert(() -> verify(homematicBridgeHandler).setOfflineStatus(), 1000, 50);
131     }
132
133     private void discoveryResultMatchesHmDevice(DiscoveryResult result, HmDevice device) {
134         assertThat(result.getThingTypeUID().getId(), is(device.getType()));
135         assertThat(result.getThingUID().getId(), is(device.getAddress()));
136         assertThat(result.getLabel(), is(device.getName()));
137     }
138 }