2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.internal;
15 import static org.junit.Assert.assertTrue;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.*;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.ScheduledThreadPoolExecutor;
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;
41 * Test cases for the {@link MQTTTopicDiscoveryService} service.
43 * @author David Graeff - Initial contribution
45 public class MQTTTopicDiscoveryServiceTest {
46 private ScheduledExecutorService scheduler;
48 private MqttBrokerHandlerFactory subject;
51 private MqttService mqttService;
57 private ThingHandlerCallback callback;
60 MQTTTopicDiscoveryParticipant listener;
62 private MqttBrokerConnectionEx connection;
64 private BrokerHandler handler;
67 public void setUp() throws ConfigurationException, MqttException {
68 scheduler = new ScheduledThreadPoolExecutor(1);
69 MockitoAnnotations.initMocks(this);
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);
76 Configuration config = new Configuration();
77 config.put("host", "10.10.0.10");
78 config.put("port", 80);
79 when(thing.getConfiguration()).thenReturn(config);
81 handler = spy(new BrokerHandlerEx(thing, connection));
82 handler.setCallback(callback);
84 subject = new MqttBrokerHandlerFactory(mqttService);
88 public void tearDown() {
89 scheduler.shutdownNow();
93 public void firstSubscribeThenHandler() {
95 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
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));
107 public void firstHandlerThenSubscribe() {
108 handler.initialize();
109 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
111 subject.createdHandler(handler);
112 subject.subscribe(listener, "topic");
113 assertTrue(subject.discoveryTopics.get("topic").contains(listener));
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));
122 public void handlerInitializeAfterSubscribe() {
123 subject.createdHandler(handler);
124 subject.subscribe(listener, "topic");
125 assertTrue(subject.discoveryTopics.get("topic").contains(listener));
127 // Init handler -> create connection
128 handler.initialize();
129 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
131 // Simulate receiving
132 final byte[] bytes = "TEST".getBytes();
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));
140 public void topicVanished() {
141 handler.initialize();
142 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
144 subject.createdHandler(handler);
145 subject.subscribe(listener, "topic");
146 assertTrue(subject.discoveryTopics.get("topic").contains(listener));
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"));