2 * Copyright (c) 2010-2023 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.assertThrows;
18 import static org.mockito.ArgumentMatchers.*;
19 import static org.mockito.Mockito.*;
21 import java.util.concurrent.ScheduledExecutorService;
22 import java.util.concurrent.ScheduledThreadPoolExecutor;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Disabled;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Mock;
32 import org.mockito.junit.jupiter.MockitoExtension;
33 import org.mockito.junit.jupiter.MockitoSettings;
34 import org.mockito.quality.Strictness;
35 import org.openhab.core.config.core.Configuration;
36 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
37 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
38 import org.openhab.core.io.transport.mqtt.MqttException;
39 import org.openhab.core.test.java.JavaTest;
40 import org.openhab.core.thing.Bridge;
41 import org.openhab.core.thing.ThingStatus;
42 import org.openhab.core.thing.ThingStatusInfo;
43 import org.openhab.core.thing.binding.ThingHandlerCallback;
44 import org.osgi.service.cm.ConfigurationException;
47 * Test cases for {@link BrokerHandler}.
49 * @author David Graeff - Initial contribution
51 @ExtendWith(MockitoExtension.class)
52 @MockitoSettings(strictness = Strictness.LENIENT)
54 public class BrokerHandlerTest extends JavaTest {
56 private @Mock @NonNullByDefault({}) ThingHandlerCallback callbackMock;
57 private @Mock @NonNullByDefault({}) Bridge thingMock;
59 private @NonNullByDefault({}) MqttBrokerConnectionEx connection;
60 private @NonNullByDefault({}) BrokerHandler handler;
61 private @NonNullByDefault({}) ScheduledExecutorService scheduler;
65 scheduler = new ScheduledThreadPoolExecutor(1);
66 connection = spy(new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
67 connection.setTimeoutExecutor(scheduler, 10000);
68 connection.setConnectionCallback(connection);
70 Configuration config = new Configuration();
71 when(thingMock.getConfiguration()).thenReturn(config);
73 handler = spy(new BrokerHandlerEx(thingMock, connection));
74 handler.setCallback(callbackMock);
78 public void tearDown() {
79 scheduler.shutdownNow();
83 public void handlerInitWithoutUrl() throws IllegalArgumentException {
84 // Assume it is a real handler and not a mock as defined above
85 handler = new BrokerHandler(thingMock);
86 assertThrows(IllegalArgumentException.class, this::initializeHandlerWaitForTimeout);
90 public void createBrokerConnection() {
91 Configuration config = new Configuration();
92 config.put("host", "10.10.0.10");
93 config.put("port", 80);
94 when(thingMock.getConfiguration()).thenReturn(config);
96 verify(handler).createBrokerConnection();
99 @Disabled("Temporarily disabled as broken since May 2022")
101 public void handlerInit() throws InterruptedException, IllegalArgumentException {
102 assertThat(initializeHandlerWaitForTimeout(), is(true));
104 ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
105 verify(callbackMock, atLeast(3)).statusUpdated(eq(thingMock), statusInfoCaptor.capture());
106 assertThat(statusInfoCaptor.getValue().getStatus(), is(ThingStatus.ONLINE));
110 * Utility method for tests that need the handler to be initialized to go on.
112 * @return Return true if successful. You usually want to use:
113 * assertThat(initializeHandlerWaitForTimeout(), is(true));
114 * @throws InterruptedException
115 * @throws IllegalArgumentException
116 * @throws MqttException
117 * @throws ConfigurationException
119 boolean initializeHandlerWaitForTimeout() throws InterruptedException, IllegalArgumentException {
120 MqttBrokerConnection c = connection;
122 MqttConnectionObserverEx o = new MqttConnectionObserverEx();
123 c.addConnectionObserver(o);
125 assertThat(connection.connectionState(), is(MqttConnectionState.DISCONNECTED));
126 handler.initialize();
127 waitForAssert(() -> verify(connection, times(2)).addConnectionObserver(any()));
128 waitForAssert(() -> verify(connection, times(1)).start());
129 // First we expect a CONNECTING state and then a CONNECTED unique state change
130 waitForAssert(() -> assertThat(o.counter, is(2)));
131 // First we expect a CONNECTING state and then a CONNECTED state change
132 // (and other CONNECTED after the future completes)
133 waitForAssert(() -> verify(handler, times(3)).connectionStateChanged(any(), any()));