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