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.mockito.Mockito.spy;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
22 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
23 import org.openhab.core.io.transport.mqtt.internal.Subscription;
24 import org.openhab.core.io.transport.mqtt.internal.client.MqttAsyncClientWrapper;
27 * We need an extended MqttBrokerConnection to overwrite the protected `connectionCallbacks` with
28 * an instance that takes the mocked version of `MqttBrokerConnection` and overwrite the connection state.
30 * We also replace the internal MqttAsyncClient with a spied one, that in respect to the success flags
31 * immediately succeed or fail with publish, subscribe, unsubscribe, connect, disconnect.
33 * @author David Graeff - Initial contribution
36 public class MqttBrokerConnectionEx extends MqttBrokerConnection {
37 public MqttConnectionState connectionStateOverwrite = MqttConnectionState.DISCONNECTED;
38 public boolean publishSuccess = true;
39 public boolean subscribeSuccess = true;
40 public boolean unsubscribeSuccess = true;
41 public boolean disconnectSuccess = true;
42 public boolean connectSuccess = true;
43 public boolean connectTimeout = false;
45 public MqttBrokerConnectionEx(String host, @Nullable Integer port, boolean secure, String clientId) {
46 super(host, port, secure, clientId);
49 public void setConnectionCallback(MqttBrokerConnectionEx o) {
50 connectionCallback = spy(new ConnectionCallback(o));
53 public Map<String, Subscription> getSubscribers() {
57 public ConnectionCallback getCallback() {
58 return connectionCallback;
62 protected MqttAsyncClientWrapper createClient() {
63 return new MqttAsyncClientWrapperEx(this);
67 public MqttConnectionState connectionState() {
68 return connectionStateOverwrite;