]> git.basschouten.com Git - openhab-addons.git/blob
b43f511a0279aca82361d7b4c72d8bb7a6fccc99
[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.assertNull;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.*;
20
21 import java.util.Collections;
22
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.mockito.junit.jupiter.MockitoSettings;
29 import org.mockito.quality.Strictness;
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.MqttException;
34 import org.openhab.core.io.transport.mqtt.MqttService;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.binding.ThingHandlerCallback;
37 import org.osgi.service.cm.ConfigurationException;
38
39 /**
40  * Tests cases for {@link org.openhab.binding.mqtt.handler.AbstractBrokerHandler}.
41  *
42  * @author David Graeff - Initial contribution
43  */
44 @ExtendWith(MockitoExtension.class)
45 @MockitoSettings(strictness = Strictness.WARN)
46 public class AbstractBrokerHandlerTest {
47     private final String HOST = "tcp://123.1.2.3";
48     private final int PORT = 80;
49     private SystemBrokerHandler handler;
50     int stateChangeCounter = 0;
51
52     private @Mock ThingHandlerCallback callback;
53     private @Mock Bridge thing;
54     private @Mock MqttService service;
55
56     @BeforeEach
57     public void setUp() {
58         doReturn(new Configuration(Collections.singletonMap("brokerid", MqttThingID.getThingUID(HOST, PORT).getId())))
59                 .when(thing).getConfiguration();
60         handler = new SystemBrokerHandler(thing, service);
61         handler.setCallback(callback);
62         assertThat(handler.getThing().getConfiguration().get("brokerid"), is(MqttThingID.getThingID(HOST, PORT)));
63         stateChangeCounter = 0;
64     }
65
66     @Test
67     public void brokerAddedWrongID() throws ConfigurationException, MqttException {
68         MqttBrokerConnection brokerConnection = mock(MqttBrokerConnection.class);
69         handler.brokerAdded("nonsense_id", brokerConnection);
70         assertNull(handler.connection);
71         // We do not expect a status change, because brokerAdded will do nothing with invalid connections.
72         verify(callback, times(0)).statusUpdated(any(), any());
73     }
74
75     @Test
76     public void brokerRemovedBroker() throws ConfigurationException, MqttException {
77         MqttBrokerConnectionEx connection = spy(
78                 new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
79         handler.brokerAdded(handler.brokerID, connection);
80         assertThat(handler.connection, is(connection));
81         handler.brokerRemoved("something", connection);
82         assertNull(handler.connection);
83     }
84
85     @Test
86     public void brokerAdded() throws ConfigurationException, MqttException {
87         MqttBrokerConnectionEx connection = spy(
88                 new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
89
90         verify(callback, times(0)).statusUpdated(any(), any());
91         handler.brokerAdded(handler.brokerID, connection);
92
93         assertThat(handler.connection, is(connection));
94
95         verify(connection).start();
96
97         // First connecting then connected and another connected after the future completes
98         verify(callback, times(3)).statusUpdated(any(), any());
99     }
100 }