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