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