]> git.basschouten.com Git - openhab-addons.git/blob
a05cd5ebe05e763c246e60027c8021f8989c013d
[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.internal;
14
15 import static org.junit.Assert.assertTrue;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.*;
18
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.ScheduledThreadPoolExecutor;
21
22 import org.openhab.core.config.core.Configuration;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.binding.ThingHandlerCallback;
25 import org.openhab.core.io.transport.mqtt.MqttException;
26 import org.openhab.core.io.transport.mqtt.MqttService;
27 import org.openhab.core.io.transport.mqtt.internal.Subscription;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryParticipant;
34 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryService;
35 import org.openhab.binding.mqtt.handler.BrokerHandler;
36 import org.openhab.binding.mqtt.handler.BrokerHandlerEx;
37 import org.openhab.binding.mqtt.handler.MqttBrokerConnectionEx;
38 import org.osgi.service.cm.ConfigurationException;
39
40 /**
41  * Test cases for the {@link MQTTTopicDiscoveryService} service.
42  *
43  * @author David Graeff - Initial contribution
44  */
45 public class MQTTTopicDiscoveryServiceTest {
46     private ScheduledExecutorService scheduler;
47
48     private MqttBrokerHandlerFactory subject;
49
50     @Mock
51     private MqttService mqttService;
52
53     @Mock
54     private Bridge thing;
55
56     @Mock
57     private ThingHandlerCallback callback;
58
59     @Mock
60     MQTTTopicDiscoveryParticipant listener;
61
62     private MqttBrokerConnectionEx connection;
63
64     private BrokerHandler handler;
65
66     @Before
67     public void setUp() throws ConfigurationException, MqttException {
68         scheduler = new ScheduledThreadPoolExecutor(1);
69         MockitoAnnotations.initMocks(this);
70
71         when(thing.getUID()).thenReturn(MqttThingID.getThingUID("10.10.0.10", 80));
72         connection = spy(new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
73         connection.setTimeoutExecutor(scheduler, 10);
74         connection.setConnectionCallback(connection);
75
76         Configuration config = new Configuration();
77         config.put("host", "10.10.0.10");
78         config.put("port", 80);
79         when(thing.getConfiguration()).thenReturn(config);
80
81         handler = spy(new BrokerHandlerEx(thing, connection));
82         handler.setCallback(callback);
83
84         subject = new MqttBrokerHandlerFactory(mqttService);
85     }
86
87     @After
88     public void tearDown() {
89         scheduler.shutdownNow();
90     }
91
92     @Test
93     public void firstSubscribeThenHandler() {
94         handler.initialize();
95         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
96
97         subject.subscribe(listener, "topic");
98         subject.createdHandler(handler);
99         assertTrue(subject.discoveryTopics.get("topic").contains(listener));
100         // Simulate receiving
101         final byte[] bytes = "TEST".getBytes();
102         connection.getSubscribers().get("topic").forEach(s -> s.processMessage("topic", bytes));
103         verify(listener).receivedMessage(eq(thing.getUID()), eq(connection), eq("topic"), eq(bytes));
104     }
105
106     @Test
107     public void firstHandlerThenSubscribe() {
108         handler.initialize();
109         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
110
111         subject.createdHandler(handler);
112         subject.subscribe(listener, "topic");
113         assertTrue(subject.discoveryTopics.get("topic").contains(listener));
114
115         // Simulate receiving
116         final byte[] bytes = "TEST".getBytes();
117         connection.getSubscribers().get("topic").forEach(s -> s.processMessage("topic", bytes));
118         verify(listener).receivedMessage(eq(thing.getUID()), eq(connection), eq("topic"), eq(bytes));
119     }
120
121     @Test
122     public void handlerInitializeAfterSubscribe() {
123         subject.createdHandler(handler);
124         subject.subscribe(listener, "topic");
125         assertTrue(subject.discoveryTopics.get("topic").contains(listener));
126
127         // Init handler -> create connection
128         handler.initialize();
129         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
130
131         // Simulate receiving
132         final byte[] bytes = "TEST".getBytes();
133
134         connection.getSubscribers().getOrDefault("topic", new Subscription("topic"))
135                 .forEach(s -> s.processMessage("topic", bytes));
136         verify(listener).receivedMessage(eq(thing.getUID()), eq(connection), eq("topic"), eq(bytes));
137     }
138
139     @Test
140     public void topicVanished() {
141         handler.initialize();
142         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
143
144         subject.createdHandler(handler);
145         subject.subscribe(listener, "topic");
146         assertTrue(subject.discoveryTopics.get("topic").contains(listener));
147
148         // Simulate receiving
149         final byte[] bytes = "".getBytes();
150         connection.getSubscribers().getOrDefault("topic", new Subscription("topic"))
151                 .forEach(s -> s.processMessage("topic", bytes));
152         verify(listener).topicVanished(eq(thing.getUID()), eq(connection), eq("topic"));
153     }
154 }