]> git.basschouten.com Git - openhab-addons.git/blob
326008b9762f333742170d871cc35e1a77421a84
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.handler;
14
15 import static org.mockito.Mockito.spy;
16
17 import java.util.Map;
18
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;
25
26 /**
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.
29  *
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.
32  *
33  * @author David Graeff - Initial contribution
34  */
35 @NonNullByDefault
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;
44
45     public MqttBrokerConnectionEx(String host, @Nullable Integer port, boolean secure, String clientId) {
46         super(host, port, secure, clientId);
47     }
48
49     public void setConnectionCallback(MqttBrokerConnectionEx o) {
50         connectionCallback = spy(new ConnectionCallback(o));
51     }
52
53     public Map<String, Subscription> getSubscribers() {
54         return subscribers;
55     }
56
57     public ConnectionCallback getCallback() {
58         return connectionCallback;
59     }
60
61     @Override
62     protected MqttAsyncClientWrapper createClient() {
63         return new MqttAsyncClientWrapperEx(this);
64     }
65
66     @Override
67     public MqttConnectionState connectionState() {
68         return connectionStateOverwrite;
69     }
70 }