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.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.*;
19 import java.util.concurrent.CompletableFuture;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
25 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
26 import org.openhab.core.io.transport.mqtt.internal.Subscription;
27 import org.openhab.core.io.transport.mqtt.internal.client.MqttAsyncClientWrapper;
29 import com.hivemq.client.mqtt.MqttClientState;
32 * We need an extended MqttBrokerConnection to overwrite the protected `connectionCallbacks` with
33 * an instance that takes the mocked version of `MqttBrokerConnection` and overwrite the connection state.
35 * We also replace the internal MqttAsyncClient with a spied one, that in respect to the success flags
36 * immediately succeed or fail with publish, subscribe, unsubscribe, connect, disconnect.
38 * @author David Graeff - Initial contribution
41 public class MqttBrokerConnectionEx extends MqttBrokerConnection {
42 public MqttConnectionState connectionStateOverwrite = MqttConnectionState.DISCONNECTED;
43 public boolean publishSuccess = true;
44 public boolean subscribeSuccess = true;
45 public boolean unsubscribeSuccess = true;
46 public boolean disconnectSuccess = true;
47 public boolean connectSuccess = true;
48 public boolean connectTimeout = false;
50 public MqttBrokerConnectionEx(String host, @Nullable Integer port, boolean secure, String clientId) {
51 super(host, port, secure, clientId);
54 public void setConnectionCallback(MqttBrokerConnectionEx o) {
55 connectionCallback = spy(new ConnectionCallback(o));
58 public Map<String, Subscription> getSubscribers() {
63 protected MqttAsyncClientWrapper createClient() {
64 MqttAsyncClientWrapper mockedClient = mock(MqttAsyncClientWrapper.class);
67 if (!connectTimeout) {
68 connectionCallback.onConnected(null);
69 connectionStateOverwrite = MqttConnectionState.CONNECTED;
70 return CompletableFuture.completedFuture(null);
72 return new CompletableFuture<>();
73 }).when(mockedClient).connect(any(), anyInt(), any(), any());
75 if (disconnectSuccess) {
76 connectionCallback.onDisconnected(new Throwable("disconnect called"));
77 connectionStateOverwrite = MqttConnectionState.DISCONNECTED;
78 return CompletableFuture.completedFuture(null);
80 return new CompletableFuture<>();
81 }).when(mockedClient).disconnect();
84 if (subscribeSuccess) {
85 return CompletableFuture.completedFuture(null);
87 CompletableFuture<Void> future = new CompletableFuture<>();
88 future.completeExceptionally(new Throwable("subscription failed"));
91 }).when(mockedClient).subscribe(any(), anyInt(), any());
94 if (unsubscribeSuccess) {
95 return CompletableFuture.completedFuture(null);
97 CompletableFuture<Void> future = new CompletableFuture<>();
98 future.completeExceptionally(new Throwable("unsubscription failed"));
101 }).when(mockedClient).unsubscribe(any());
104 return MqttClientState.CONNECTED;
105 }).when(mockedClient).getState();
110 public @NonNull MqttConnectionState connectionState() {
111 return connectionStateOverwrite;