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