]> git.basschouten.com Git - openhab-addons.git/blob
9f5c8e4b1a1d6b7e5b2acd13ef9bcf5fea578bbe
[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.junit.Assert.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.Before;
27 import org.junit.Test;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.openhab.binding.mqtt.MqttBindingConstants;
32 import org.openhab.core.config.discovery.DiscoveryListener;
33 import org.openhab.core.config.discovery.DiscoveryResult;
34 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
35 import org.openhab.core.io.transport.mqtt.MqttService;
36
37 /**
38  * Tests cases for {@link org.openhab.binding.mqtt.internal.discovery.MqttServiceDiscoveryService}.
39  *
40  * @author David Graeff - Initial contribution
41  */
42 public class ServiceDiscoveryServiceTest {
43     @Mock
44     private MqttService service;
45
46     @Mock
47     private DiscoveryListener discoverListener;
48
49     @Before
50     public void initMocks() throws ConfigurationException {
51         MockitoAnnotations.initMocks(this);
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() throws ConfigurationException {
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 }