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.assertThat;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.ScheduledThreadPoolExecutor;
23 import org.junit.After;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
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.ThingStatus;
38 import org.openhab.core.thing.ThingStatusInfo;
39 import org.openhab.core.thing.binding.ThingHandlerCallback;
40 import org.osgi.service.cm.ConfigurationException;
43 * Test cases for {@link BrokerHandler}.
45 * @author David Graeff - Initial contribution
47 public class BrokerHandlerTest {
48 private ScheduledExecutorService scheduler;
51 private ThingHandlerCallback callback;
57 private MqttService service;
59 private MqttBrokerConnectionEx connection;
61 private BrokerHandler handler;
64 public void setUp() throws ConfigurationException, MqttException {
65 scheduler = new ScheduledThreadPoolExecutor(1);
66 MockitoAnnotations.initMocks(this);
67 when(thing.getUID()).thenReturn(MqttThingID.getThingUID("10.10.0.10", 80));
68 connection = spy(new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
69 connection.setTimeoutExecutor(scheduler, 10);
70 connection.setConnectionCallback(connection);
72 Configuration config = new Configuration();
73 when(thing.getConfiguration()).thenReturn(config);
75 handler = spy(new BrokerHandlerEx(thing, connection));
76 handler.setCallback(callback);
80 public void tearDown() {
81 scheduler.shutdownNow();
84 @Test(expected = IllegalArgumentException.class)
85 public void handlerInitWithoutUrl()
86 throws InterruptedException, IllegalArgumentException, MqttException, ConfigurationException {
87 // Assume it is a real handler and not a mock as defined above
88 handler = new BrokerHandler(thing);
89 assertThat(initializeHandlerWaitForTimeout(), is(true));
93 public void createBrokerConnection() {
94 Configuration config = new Configuration();
95 config.put("host", "10.10.0.10");
96 config.put("port", 80);
97 when(thing.getConfiguration()).thenReturn(config);
99 verify(handler).createBrokerConnection();
103 public void handlerInit()
104 throws InterruptedException, IllegalArgumentException, MqttException, ConfigurationException {
105 assertThat(initializeHandlerWaitForTimeout(), is(true));
107 ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
108 verify(callback, atLeast(3)).statusUpdated(eq(thing), statusInfoCaptor.capture());
109 Assert.assertThat(statusInfoCaptor.getValue().getStatus(), is(ThingStatus.ONLINE));
113 * Utility method for tests that need the handler to be initialized to go on.
115 * @return Return true if successful. You usually want to use:
116 * assertThat(initializeHandlerWaitForTimeout(), is(true));
117 * @throws InterruptedException
118 * @throws IllegalArgumentException
119 * @throws MqttException
120 * @throws ConfigurationException
122 boolean initializeHandlerWaitForTimeout()
123 throws InterruptedException, IllegalArgumentException, MqttException, ConfigurationException {
124 MqttBrokerConnection c = connection;
126 MqttConnectionObserverEx o = new MqttConnectionObserverEx();
127 c.addConnectionObserver(o);
129 assertThat(connection.connectionState(), is(MqttConnectionState.DISCONNECTED));
130 handler.initialize();
131 verify(connection, times(2)).addConnectionObserver(any());
132 verify(connection, times(1)).start();
133 // First we expect a CONNECTING state and then a CONNECTED unique state change
134 assertThat(o.counter, is(2));
135 // First we expect a CONNECTING state and then a CONNECTED state change
136 // (and other CONNECTED after the future completes)
137 verify(handler, times(3)).connectionStateChanged(any(), any());