]> git.basschouten.com Git - openhab-addons.git/blob
b56ebf508ed68b9eb90db44440571acce65458f8
[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.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertThrows;
18 import static org.mockito.ArgumentMatchers.*;
19 import static org.mockito.Mockito.*;
20
21 import java.util.concurrent.ScheduledExecutorService;
22 import java.util.concurrent.ScheduledThreadPoolExecutor;
23
24 import org.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.Mock;
30 import org.mockito.junit.jupiter.MockitoExtension;
31 import org.mockito.junit.jupiter.MockitoSettings;
32 import org.mockito.quality.Strictness;
33 import org.openhab.binding.mqtt.internal.MqttThingID;
34 import org.openhab.core.config.core.Configuration;
35 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
36 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
37 import org.openhab.core.io.transport.mqtt.MqttException;
38 import org.openhab.core.io.transport.mqtt.MqttService;
39 import org.openhab.core.thing.Bridge;
40 import org.openhab.core.thing.ThingStatus;
41 import org.openhab.core.thing.ThingStatusInfo;
42 import org.openhab.core.thing.binding.ThingHandlerCallback;
43 import org.osgi.service.cm.ConfigurationException;
44
45 /**
46  * Test cases for {@link BrokerHandler}.
47  *
48  * @author David Graeff - Initial contribution
49  */
50 @ExtendWith(MockitoExtension.class)
51 @MockitoSettings(strictness = Strictness.WARN)
52 public class BrokerHandlerTest {
53     private ScheduledExecutorService scheduler;
54
55     private @Mock ThingHandlerCallback callback;
56     private @Mock Bridge thing;
57     private @Mock MqttService service;
58
59     private MqttBrokerConnectionEx connection;
60
61     private BrokerHandler handler;
62
63     @BeforeEach
64     public void setUp() throws ConfigurationException, MqttException {
65         scheduler = new ScheduledThreadPoolExecutor(1);
66         when(thing.getUID()).thenReturn(MqttThingID.getThingUID("10.10.0.10", 80));
67         connection = spy(new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
68         connection.setTimeoutExecutor(scheduler, 10);
69         connection.setConnectionCallback(connection);
70
71         Configuration config = new Configuration();
72         when(thing.getConfiguration()).thenReturn(config);
73
74         handler = spy(new BrokerHandlerEx(thing, connection));
75         handler.setCallback(callback);
76     }
77
78     @AfterEach
79     public void tearDown() {
80         scheduler.shutdownNow();
81     }
82
83     @Test
84     public void handlerInitWithoutUrl()
85             throws InterruptedException, IllegalArgumentException, MqttException, ConfigurationException {
86         // Assume it is a real handler and not a mock as defined above
87         handler = new BrokerHandler(thing);
88         assertThrows(IllegalArgumentException.class, () -> initializeHandlerWaitForTimeout());
89     }
90
91     @Test
92     public void createBrokerConnection() {
93         Configuration config = new Configuration();
94         config.put("host", "10.10.0.10");
95         config.put("port", 80);
96         when(thing.getConfiguration()).thenReturn(config);
97         handler.initialize();
98         verify(handler).createBrokerConnection();
99     }
100
101     @Test
102     public void handlerInit()
103             throws InterruptedException, IllegalArgumentException, MqttException, ConfigurationException {
104         assertThat(initializeHandlerWaitForTimeout(), is(true));
105
106         ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
107         verify(callback, atLeast(3)).statusUpdated(eq(thing), statusInfoCaptor.capture());
108         assertThat(statusInfoCaptor.getValue().getStatus(), is(ThingStatus.ONLINE));
109     }
110
111     /**
112      * Utility method for tests that need the handler to be initialized to go on.
113      *
114      * @return Return true if successful. You usually want to use:
115      *         assertThat(initializeHandlerWaitForTimeout(), is(true));
116      * @throws InterruptedException
117      * @throws IllegalArgumentException
118      * @throws MqttException
119      * @throws ConfigurationException
120      */
121     boolean initializeHandlerWaitForTimeout()
122             throws InterruptedException, IllegalArgumentException, MqttException, ConfigurationException {
123         MqttBrokerConnection c = connection;
124
125         MqttConnectionObserverEx o = new MqttConnectionObserverEx();
126         c.addConnectionObserver(o);
127
128         assertThat(connection.connectionState(), is(MqttConnectionState.DISCONNECTED));
129         handler.initialize();
130         verify(connection, times(2)).addConnectionObserver(any());
131         verify(connection, times(1)).start();
132         // First we expect a CONNECTING state and then a CONNECTED unique state change
133         assertThat(o.counter, is(2));
134         // First we expect a CONNECTING state and then a CONNECTED state change
135         // (and other CONNECTED after the future completes)
136         verify(handler, times(3)).connectionStateChanged(any(), any());
137         return true;
138     }
139 }