2 * Copyright (c) 2010-2024 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.salus.internal.discovery;
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;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Random;
25 import java.util.TreeSet;
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;
37 * @author Martin GrzeĊlowski - Initial contribution
39 public class CloudDiscoveryTest {
42 @DisplayName("Method filters out disconnected devices and adds connected devices as things using addThing method")
43 void testFiltersOutDisconnectedDevicesAndAddsConnectedDevicesAsThings() throws SalusApiException {
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));
57 given(cloudApi.findDevices()).willReturn(devices);
60 discoveryService.startScan();
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())));
75 @DisplayName("Cloud API throws an exception during device retrieval, method logs the error")
76 void testLogsErrorWhenCloudApiThrowsException() throws SalusApiException {
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);
83 given(cloudApi.findDevices()).willThrow(new SalusApiException("API error"));
86 discoveryService.startScan();
89 // no error is thrown, OK
92 private Device randomDevice(boolean connected) {
93 var random = new Random();
94 var map = new HashMap<String, Object>();
96 map.put("connection_status", "online");
98 return new Device("dsn-" + random.nextInt(), "name-" + random.nextInt(), map);