2 * Copyright (c) 2010-2021 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.any;
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.MqttException;
34 import org.openhab.core.io.transport.mqtt.MqttService;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.binding.ThingHandlerCallback;
37 import org.osgi.service.cm.ConfigurationException;
40 * Tests cases for {@link org.openhab.binding.mqtt.handler.AbstractBrokerHandler}.
42 * @author David Graeff - Initial contribution
44 @ExtendWith(MockitoExtension.class)
45 @MockitoSettings(strictness = Strictness.WARN)
46 public class AbstractBrokerHandlerTest {
47 private final String HOST = "tcp://123.1.2.3";
48 private final int PORT = 80;
49 private SystemBrokerHandler handler;
50 int stateChangeCounter = 0;
52 private @Mock ThingHandlerCallback callback;
53 private @Mock Bridge thing;
54 private @Mock MqttService service;
58 doReturn(new Configuration(Collections.singletonMap("brokerid", MqttThingID.getThingUID(HOST, PORT).getId())))
59 .when(thing).getConfiguration();
60 handler = new SystemBrokerHandler(thing, service);
61 handler.setCallback(callback);
62 assertThat(handler.getThing().getConfiguration().get("brokerid"), is(MqttThingID.getThingID(HOST, PORT)));
63 stateChangeCounter = 0;
67 public void brokerAddedWrongID() throws ConfigurationException, MqttException {
68 MqttBrokerConnection brokerConnection = mock(MqttBrokerConnection.class);
69 handler.brokerAdded("nonsense_id", brokerConnection);
70 assertNull(handler.connection);
71 // We do not expect a status change, because brokerAdded will do nothing with invalid connections.
72 verify(callback, times(0)).statusUpdated(any(), any());
76 public void brokerRemovedBroker() throws ConfigurationException, MqttException {
77 MqttBrokerConnectionEx connection = spy(
78 new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
79 handler.brokerAdded(handler.brokerID, connection);
80 assertThat(handler.connection, is(connection));
81 handler.brokerRemoved("something", connection);
82 assertNull(handler.connection);
86 public void brokerAdded() throws ConfigurationException, MqttException {
87 MqttBrokerConnectionEx connection = spy(
88 new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
90 verify(callback, times(0)).statusUpdated(any(), any());
91 handler.brokerAdded(handler.brokerID, connection);
93 assertThat(handler.connection, is(connection));
95 verify(connection).start();
97 // First connecting then connected and another connected after the future completes
98 verify(callback, times(3)).statusUpdated(any(), any());