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.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertNull;
18 import static org.mockito.ArgumentMatchers.*;
19 import static org.mockito.Mockito.*;
21 import java.util.Collections;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.mockito.junit.jupiter.MockitoSettings;
29 import org.mockito.quality.Strictness;
30 import org.openhab.binding.mqtt.internal.MqttThingID;
31 import org.openhab.core.config.core.Configuration;
32 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
33 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
34 import org.openhab.core.io.transport.mqtt.MqttException;
35 import org.openhab.core.io.transport.mqtt.MqttService;
36 import org.openhab.core.thing.Bridge;
37 import org.openhab.core.thing.binding.ThingHandlerCallback;
38 import org.osgi.service.cm.ConfigurationException;
41 * Tests cases for {@link org.openhab.binding.mqtt.handler.AbstractBrokerHandler}.
43 * @author David Graeff - Initial contribution
45 @ExtendWith(MockitoExtension.class)
46 @MockitoSettings(strictness = Strictness.WARN)
47 public class AbstractBrokerHandlerTest {
48 private final String HOST = "tcp://123.1.2.3";
49 private final int PORT = 80;
50 private SystemBrokerHandler handler;
51 int stateChangeCounter = 0;
53 private @Mock ThingHandlerCallback callback;
54 private @Mock Bridge thing;
55 private @Mock MqttService service;
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());