]> git.basschouten.com Git - openhab-addons.git/blob
f69bd03b363f010e249836a70f48af6678271bdf
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.mqtt.internal.discovery;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.ArgumentMatchers.eq;
18 import static org.mockito.Mockito.*;
19
20 import java.util.List;
21 import java.util.Map;
22 import java.util.TreeMap;
23
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.Mock;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.mockito.junit.jupiter.MockitoSettings;
31 import org.mockito.quality.Strictness;
32 import org.openhab.binding.mqtt.MqttBindingConstants;
33 import org.openhab.core.config.discovery.DiscoveryListener;
34 import org.openhab.core.config.discovery.DiscoveryResult;
35 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
36 import org.openhab.core.io.transport.mqtt.MqttService;
37
38 /**
39  * Tests cases for {@link org.openhab.binding.mqtt.internal.discovery.MqttServiceDiscoveryService}.
40  *
41  * @author David Graeff - Initial contribution
42  */
43 @ExtendWith(MockitoExtension.class)
44 @MockitoSettings(strictness = Strictness.WARN)
45 public class ServiceDiscoveryServiceTest {
46
47     private @Mock MqttService service;
48     private @Mock DiscoveryListener discoverListener;
49
50     @BeforeEach
51     public void initMocks() {
52         Map<String, MqttBrokerConnection> brokers = new TreeMap<>();
53         brokers.put("testname", new MqttBrokerConnection("tcp://123.123.123.123", null, false, null));
54         brokers.put("textual", new MqttBrokerConnection("tcp://123.123.123.123", null, true, null));
55         when(service.getAllBrokerConnections()).thenReturn(brokers);
56     }
57
58     @Test
59     public void testDiscovery() {
60         // Setting the MqttService will enable the background scanner
61         MqttServiceDiscoveryService d = new MqttServiceDiscoveryService();
62         d.addDiscoveryListener(discoverListener);
63         d.setMqttService(service);
64         d.startScan();
65
66         // We expect 3 discoveries. An embedded thing, a textual configured one, a non-textual one
67         ArgumentCaptor<DiscoveryResult> discoveryCapture = ArgumentCaptor.forClass(DiscoveryResult.class);
68         verify(discoverListener, times(2)).thingDiscovered(eq(d), discoveryCapture.capture());
69         List<DiscoveryResult> discoveryResults = discoveryCapture.getAllValues();
70         assertThat(discoveryResults.size(), is(2));
71         assertThat(discoveryResults.get(0).getThingTypeUID(), is(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER));
72         assertThat(discoveryResults.get(1).getThingTypeUID(), is(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER));
73
74         // Add another thing
75         d.brokerAdded("anotherone", new MqttBrokerConnection("tcp://123.123.123.123", null, false, null));
76         discoveryCapture = ArgumentCaptor.forClass(DiscoveryResult.class);
77         verify(discoverListener, times(3)).thingDiscovered(eq(d), discoveryCapture.capture());
78         discoveryResults = discoveryCapture.getAllValues();
79         assertThat(discoveryResults.size(), is(3));
80         assertThat(discoveryResults.get(2).getThingTypeUID(), is(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER));
81     }
82 }