2 * Copyright (c) 2010-2021 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.hamcrest.CoreMatchers.hasItem;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.ArgumentMatchers.eq;
18 import static org.mockito.Mockito.*;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.ScheduledThreadPoolExecutor;
23 import org.junit.jupiter.api.AfterEach;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.mockito.junit.jupiter.MockitoSettings;
30 import org.mockito.quality.Strictness;
31 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryParticipant;
32 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryService;
33 import org.openhab.binding.mqtt.handler.BrokerHandler;
34 import org.openhab.binding.mqtt.handler.BrokerHandlerEx;
35 import org.openhab.binding.mqtt.handler.MqttBrokerConnectionEx;
36 import org.openhab.core.config.core.Configuration;
37 import org.openhab.core.io.transport.mqtt.MqttService;
38 import org.openhab.core.thing.Bridge;
39 import org.openhab.core.thing.binding.ThingHandlerCallback;
42 * Test cases for the {@link MQTTTopicDiscoveryService} service.
44 * @author David Graeff - Initial contribution
46 @ExtendWith(MockitoExtension.class)
47 @MockitoSettings(strictness = Strictness.WARN)
48 public class MQTTTopicDiscoveryServiceTest {
49 private ScheduledExecutorService scheduler;
51 private MqttBrokerHandlerFactory subject;
54 private MqttService mqttService;
60 private ThingHandlerCallback callback;
63 MQTTTopicDiscoveryParticipant listener;
65 private MqttBrokerConnectionEx connection;
67 private BrokerHandler handler;
71 scheduler = new ScheduledThreadPoolExecutor(1);
73 when(thing.getUID()).thenReturn(MqttThingID.getThingUID("10.10.0.10", 80));
74 connection = spy(new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
75 connection.setTimeoutExecutor(scheduler, 10);
76 connection.setConnectionCallback(connection);
78 Configuration config = new Configuration();
79 config.put("host", "10.10.0.10");
80 config.put("port", 80);
81 when(thing.getConfiguration()).thenReturn(config);
83 handler = spy(new BrokerHandlerEx(thing, connection));
84 handler.setCallback(callback);
86 subject = new MqttBrokerHandlerFactory(mqttService);
90 public void tearDown() {
91 scheduler.shutdownNow();
95 public void firstSubscribeThenHandler() {
97 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
99 subject.subscribe(listener, "topic");
100 subject.createdHandler(handler);
101 assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
102 // Simulate receiving
103 final byte[] bytes = "TEST".getBytes();
104 connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
105 verify(listener).receivedMessage(eq(thing.getUID()), eq(connection), eq("topic"), eq(bytes));
109 public void firstHandlerThenSubscribe() {
110 handler.initialize();
111 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
113 subject.createdHandler(handler);
114 subject.subscribe(listener, "topic");
115 assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
117 // Simulate receiving
118 final byte[] bytes = "TEST".getBytes();
119 connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
120 verify(listener).receivedMessage(eq(thing.getUID()), eq(connection), eq("topic"), eq(bytes));
124 public void handlerInitializeAfterSubscribe() {
125 subject.createdHandler(handler);
126 subject.subscribe(listener, "topic");
127 assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
129 // Init handler -> create connection
130 handler.initialize();
131 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
133 // Simulate receiving
134 final byte[] bytes = "TEST".getBytes();
135 connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
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 assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
148 // Simulate receiving
149 final byte[] bytes = "".getBytes();
150 connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
151 verify(listener).topicVanished(eq(thing.getUID()), eq(connection), eq("topic"));