2 * Copyright (c) 2010-2020 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.mqtt.handler;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.junit.Assert.*;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
20 import java.util.Collections;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.openhab.binding.mqtt.internal.MqttThingID;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
29 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
30 import org.openhab.core.io.transport.mqtt.MqttException;
31 import org.openhab.core.io.transport.mqtt.MqttService;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.binding.ThingHandlerCallback;
34 import org.osgi.service.cm.ConfigurationException;
37 * Tests cases for {@link org.openhab.binding.mqtt.handler.AbstractBrokerHandler}.
39 * @author David Graeff - Initial contribution
41 public class AbstractBrokerHandlerTest {
42 private final String HOST = "tcp://123.1.2.3";
43 private final int PORT = 80;
44 private SystemBrokerHandler handler;
45 int stateChangeCounter = 0;
48 private ThingHandlerCallback callback;
54 private MqttService service;
58 MockitoAnnotations.initMocks(this);
59 doReturn(MqttThingID.getThingUID(HOST, PORT)).when(thing).getUID();
60 doReturn(new Configuration(Collections.singletonMap("brokerid", MqttThingID.getThingUID(HOST, PORT).getId())))
61 .when(thing).getConfiguration();
62 handler = new SystemBrokerHandler(thing, service);
63 handler.setCallback(callback);
64 assertThat(handler.getThing().getConfiguration().get("brokerid"), is(MqttThingID.getThingID(HOST, PORT)));
65 stateChangeCounter = 0;
69 public void brokerAddedWrongID() throws ConfigurationException, MqttException {
70 MqttBrokerConnection brokerConnection = mock(MqttBrokerConnection.class);
71 when(brokerConnection.connectionState()).thenReturn(MqttConnectionState.CONNECTED);
72 handler.brokerAdded("nonsense_id", brokerConnection);
73 assertNull(handler.connection);
74 // We do not expect a status change, because brokerAdded will do nothing with invalid connections.
75 verify(callback, times(0)).statusUpdated(any(), any());
79 public void brokerRemovedBroker() throws ConfigurationException, MqttException {
80 MqttBrokerConnectionEx connection = spy(
81 new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
82 handler.brokerAdded(handler.brokerID, connection);
83 assertThat(handler.connection, is(connection));
84 handler.brokerRemoved("something", connection);
85 assertNull(handler.connection);
89 public void brokerAdded() throws ConfigurationException, MqttException {
90 MqttBrokerConnectionEx connection = spy(
91 new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
92 doReturn(connection).when(service).getBrokerConnection(eq(handler.brokerID));
94 verify(callback, times(0)).statusUpdated(any(), any());
95 handler.brokerAdded(handler.brokerID, connection);
97 assertThat(handler.connection, is(connection));
99 verify(connection).start();
101 // First connecting then connected and another connected after the future completes
102 verify(callback, times(3)).statusUpdated(any(), any());