]> git.basschouten.com Git - openhab-addons.git/blob
b0295b48176bf4d1f28a3cea42c6b327d3db7f52
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.salus.internal.discovery;
14
15 import static org.mockito.BDDMockito.given;
16 import static org.mockito.Mockito.argThat;
17 import static org.mockito.Mockito.eq;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.never;
20 import static org.mockito.Mockito.verify;
21
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Random;
25 import java.util.TreeSet;
26
27 import org.junit.jupiter.api.DisplayName;
28 import org.junit.jupiter.api.Test;
29 import org.openhab.binding.salus.internal.handler.CloudApi;
30 import org.openhab.binding.salus.internal.handler.CloudBridgeHandler;
31 import org.openhab.binding.salus.internal.rest.Device;
32 import org.openhab.binding.salus.internal.rest.SalusApiException;
33 import org.openhab.core.config.discovery.DiscoveryListener;
34 import org.openhab.core.thing.ThingUID;
35
36 /**
37  * @author Martin GrzeĊ›lowski - Initial contribution
38  */
39 public class CloudDiscoveryTest {
40
41     @Test
42     @DisplayName("Method filters out disconnected devices and adds connected devices as things using addThing method")
43     void testFiltersOutDisconnectedDevicesAndAddsConnectedDevicesAsThings() throws SalusApiException {
44         // Given
45         var cloudApi = mock(CloudApi.class);
46         var bridgeHandler = mock(CloudBridgeHandler.class);
47         var bridgeUid = new ThingUID("salus", "salus-device", "boo");
48         var discoveryService = new CloudDiscovery(bridgeHandler, cloudApi, bridgeUid);
49         var discoveryListener = mock(DiscoveryListener.class);
50         discoveryService.addDiscoveryListener(discoveryListener);
51         var device1 = randomDevice(true);
52         var device2 = randomDevice(true);
53         var device3 = randomDevice(false);
54         var device4 = randomDevice(false);
55         var devices = new TreeSet<>(List.of(device1, device2, device3, device4));
56
57         given(cloudApi.findDevices()).willReturn(devices);
58
59         // When
60         discoveryService.startScan();
61
62         // Then
63         verify(cloudApi).findDevices();
64         verify(discoveryListener).thingDiscovered(eq(discoveryService),
65                 argThat(discoveryResult -> discoveryResult.getLabel().equals(device1.name())));
66         verify(discoveryListener).thingDiscovered(eq(discoveryService),
67                 argThat(discoveryResult -> discoveryResult.getLabel().equals(device2.name())));
68         verify(discoveryListener, never()).thingDiscovered(eq(discoveryService),
69                 argThat(discoveryResult -> discoveryResult.getLabel().equals(device3.name())));
70         verify(discoveryListener, never()).thingDiscovered(eq(discoveryService),
71                 argThat(discoveryResult -> discoveryResult.getLabel().equals(device4.name())));
72     }
73
74     @Test
75     @DisplayName("Cloud API throws an exception during device retrieval, method logs the error")
76     void testLogsErrorWhenCloudApiThrowsException() throws SalusApiException {
77         // Given
78         var cloudApi = mock(CloudApi.class);
79         var bridgeHandler = mock(CloudBridgeHandler.class);
80         var bridgeUid = mock(ThingUID.class);
81         var discoveryService = new CloudDiscovery(bridgeHandler, cloudApi, bridgeUid);
82
83         given(cloudApi.findDevices()).willThrow(new SalusApiException("API error"));
84
85         // When
86         discoveryService.startScan();
87
88         // Then
89         // no error is thrown, OK
90     }
91
92     private Device randomDevice(boolean connected) {
93         var random = new Random();
94         var map = new HashMap<String, Object>();
95         if (connected) {
96             map.put("connection_status", "online");
97         }
98         return new Device("dsn-" + random.nextInt(), "name-" + random.nextInt(), map);
99     }
100 }