]> git.basschouten.com Git - openhab-addons.git/blob
7414b3a076a26d55029c35484f9774f7629b6c09
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.test.java.JavaTest;
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 extends JavaTest {
52     private ScheduledExecutorService scheduler;
53
54     private @Mock ThingHandlerCallback callback;
55     private @Mock Bridge thing;
56
57     private MqttBrokerConnectionEx connection;
58
59     private BrokerHandler handler;
60
61     @BeforeEach
62     public void setUp() {
63         scheduler = new ScheduledThreadPoolExecutor(1);
64         connection = spy(new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
65         connection.setTimeoutExecutor(scheduler, 10000);
66         connection.setConnectionCallback(connection);
67
68         Configuration config = new Configuration();
69         when(thing.getConfiguration()).thenReturn(config);
70
71         handler = spy(new BrokerHandlerEx(thing, connection));
72         handler.setCallback(callback);
73     }
74
75     @AfterEach
76     public void tearDown() {
77         scheduler.shutdownNow();
78     }
79
80     @Test
81     public void handlerInitWithoutUrl() throws IllegalArgumentException {
82         // Assume it is a real handler and not a mock as defined above
83         handler = new BrokerHandler(thing);
84         assertThrows(IllegalArgumentException.class, this::initializeHandlerWaitForTimeout);
85     }
86
87     @Test
88     public void createBrokerConnection() {
89         Configuration config = new Configuration();
90         config.put("host", "10.10.0.10");
91         config.put("port", 80);
92         when(thing.getConfiguration()).thenReturn(config);
93         handler.initialize();
94         verify(handler).createBrokerConnection();
95     }
96
97     @Test
98     public void handlerInit() throws InterruptedException, IllegalArgumentException {
99         assertThat(initializeHandlerWaitForTimeout(), is(true));
100
101         ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
102         verify(callback, atLeast(3)).statusUpdated(eq(thing), statusInfoCaptor.capture());
103         assertThat(statusInfoCaptor.getValue().getStatus(), is(ThingStatus.ONLINE));
104     }
105
106     /**
107      * Utility method for tests that need the handler to be initialized to go on.
108      *
109      * @return Return true if successful. You usually want to use:
110      *         assertThat(initializeHandlerWaitForTimeout(), is(true));
111      * @throws InterruptedException
112      * @throws IllegalArgumentException
113      * @throws MqttException
114      * @throws ConfigurationException
115      */
116     boolean initializeHandlerWaitForTimeout() throws InterruptedException, IllegalArgumentException {
117         MqttBrokerConnection c = connection;
118
119         MqttConnectionObserverEx o = new MqttConnectionObserverEx();
120         c.addConnectionObserver(o);
121
122         assertThat(connection.connectionState(), is(MqttConnectionState.DISCONNECTED));
123         handler.initialize();
124         waitForAssert(() -> verify(connection, times(2)).addConnectionObserver(any()));
125         waitForAssert(() -> verify(connection, times(1)).start());
126         // First we expect a CONNECTING state and then a CONNECTED unique state change
127         waitForAssert(() -> assertThat(o.counter, is(2)));
128         // First we expect a CONNECTING state and then a CONNECTED state change
129         // (and other CONNECTED after the future completes)
130         waitForAssert(() -> verify(handler, times(3)).connectionStateChanged(any(), any()));
131         return true;
132     }
133 }