]> git.basschouten.com Git - openhab-addons.git/blob
f09a110327fd7e2b950b82bfd7a20d09ce8e1248
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.ArgumentMatchers.*;
16 import static org.mockito.Mockito.*;
17
18 import java.util.Map;
19 import java.util.concurrent.CompletableFuture;
20
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;
28
29 import com.hivemq.client.mqtt.MqttClientState;
30
31 /**
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.
34  *
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.
37  *
38  * @author David Graeff - Initial contribution
39  */
40 @NonNullByDefault
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;
49
50     public MqttBrokerConnectionEx(String host, @Nullable Integer port, boolean secure, String clientId) {
51         super(host, port, secure, clientId);
52     }
53
54     public void setConnectionCallback(MqttBrokerConnectionEx o) {
55         connectionCallback = spy(new ConnectionCallback(o));
56     }
57
58     public Map<String, Subscription> getSubscribers() {
59         return subscribers;
60     }
61
62     @Override
63     protected MqttAsyncClientWrapper createClient() {
64         MqttAsyncClientWrapper mockedClient = mock(MqttAsyncClientWrapper.class);
65         // connect
66         doAnswer(i -> {
67             if (!connectTimeout) {
68                 connectionCallback.onConnected(null);
69                 connectionStateOverwrite = MqttConnectionState.CONNECTED;
70                 return CompletableFuture.completedFuture(null);
71             }
72             return new CompletableFuture<>();
73         }).when(mockedClient).connect(any(), anyInt(), any(), any());
74         doAnswer(i -> {
75             if (disconnectSuccess) {
76                 connectionCallback.onDisconnected(new Throwable("disconnect called"));
77                 connectionStateOverwrite = MqttConnectionState.DISCONNECTED;
78                 return CompletableFuture.completedFuture(null);
79             }
80             return new CompletableFuture<>();
81         }).when(mockedClient).disconnect();
82         // subscribe
83         doAnswer(i -> {
84             if (subscribeSuccess) {
85                 return CompletableFuture.completedFuture(null);
86             } else {
87                 CompletableFuture<Void> future = new CompletableFuture<>();
88                 future.completeExceptionally(new Throwable("subscription failed"));
89                 return future;
90             }
91         }).when(mockedClient).subscribe(any(), anyInt(), any());
92         // unsubscribe
93         doAnswer(i -> {
94             if (unsubscribeSuccess) {
95                 return CompletableFuture.completedFuture(null);
96             } else {
97                 CompletableFuture<Void> future = new CompletableFuture<>();
98                 future.completeExceptionally(new Throwable("unsubscription failed"));
99                 return future;
100             }
101         }).when(mockedClient).unsubscribe(any());
102         // state
103         doAnswer(i -> {
104             return MqttClientState.CONNECTED;
105         }).when(mockedClient).getState();
106         return mockedClient;
107     }
108
109     @Override
110     public @NonNull MqttConnectionState connectionState() {
111         return connectionStateOverwrite;
112     }
113 }