2 * Copyright (c) 2010-2022 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.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.Mock;
30 import org.mockito.junit.jupiter.MockitoExtension;
31 import org.mockito.junit.jupiter.MockitoSettings;
32 import org.mockito.quality.Strictness;
33 import org.openhab.core.config.core.Configuration;
34 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
35 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
36 import org.openhab.core.io.transport.mqtt.MqttException;
37 import org.openhab.core.test.java.JavaTest;
38 import org.openhab.core.thing.Bridge;
39 import org.openhab.core.thing.ThingStatus;
40 import org.openhab.core.thing.ThingStatusInfo;
41 import org.openhab.core.thing.binding.ThingHandlerCallback;
42 import org.osgi.service.cm.ConfigurationException;
45 * Test cases for {@link BrokerHandler}.
47 * @author David Graeff - Initial contribution
49 @ExtendWith(MockitoExtension.class)
50 @MockitoSettings(strictness = Strictness.WARN)
51 public class BrokerHandlerTest extends JavaTest {
52 private ScheduledExecutorService scheduler;
54 private @Mock ThingHandlerCallback callback;
55 private @Mock Bridge thing;
57 private MqttBrokerConnectionEx connection;
59 private BrokerHandler handler;
63 scheduler = new ScheduledThreadPoolExecutor(1);
64 connection = spy(new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
65 connection.setTimeoutExecutor(scheduler, 10000);
66 connection.setConnectionCallback(connection);
68 Configuration config = new Configuration();
69 when(thing.getConfiguration()).thenReturn(config);
71 handler = spy(new BrokerHandlerEx(thing, connection));
72 handler.setCallback(callback);
76 public void tearDown() {
77 scheduler.shutdownNow();
81 public void handlerInitWithoutUrl() throws IllegalArgumentException {
82 // Assume it is a real handler and not a mock as defined above
83 handler = new BrokerHandler(thing);
84 assertThrows(IllegalArgumentException.class, this::initializeHandlerWaitForTimeout);
88 public void createBrokerConnection() {
89 Configuration config = new Configuration();
90 config.put("host", "10.10.0.10");
91 config.put("port", 80);
92 when(thing.getConfiguration()).thenReturn(config);
94 verify(handler).createBrokerConnection();
98 public void handlerInit() throws InterruptedException, IllegalArgumentException {
99 assertThat(initializeHandlerWaitForTimeout(), is(true));
101 ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
102 verify(callback, atLeast(3)).statusUpdated(eq(thing), statusInfoCaptor.capture());
103 assertThat(statusInfoCaptor.getValue().getStatus(), is(ThingStatus.ONLINE));
107 * Utility method for tests that need the handler to be initialized to go on.
109 * @return Return true if successful. You usually want to use:
110 * assertThat(initializeHandlerWaitForTimeout(), is(true));
111 * @throws InterruptedException
112 * @throws IllegalArgumentException
113 * @throws MqttException
114 * @throws ConfigurationException
116 boolean initializeHandlerWaitForTimeout() throws InterruptedException, IllegalArgumentException {
117 MqttBrokerConnection c = connection;
119 MqttConnectionObserverEx o = new MqttConnectionObserverEx();
120 c.addConnectionObserver(o);
122 assertThat(connection.connectionState(), is(MqttConnectionState.DISCONNECTED));
123 handler.initialize();
124 waitForAssert(() -> verify(connection, times(2)).addConnectionObserver(any()));
125 waitForAssert(() -> verify(connection, times(1)).start());
126 // First we expect a CONNECTING state and then a CONNECTED unique state change
127 waitForAssert(() -> assertThat(o.counter, is(2)));
128 // First we expect a CONNECTING state and then a CONNECTED state change
129 // (and other CONNECTED after the future completes)
130 waitForAssert(() -> verify(handler, times(3)).connectionStateChanged(any(), any()));