2 * Copyright (c) 2010-2022 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.thing.Bridge;
38 import org.openhab.core.thing.binding.ThingHandlerCallback;
41 * Test cases for the {@link MQTTTopicDiscoveryService} service.
43 * @author David Graeff - Initial contribution
45 @ExtendWith(MockitoExtension.class)
46 @MockitoSettings(strictness = Strictness.WARN)
47 public class MQTTTopicDiscoveryServiceTest {
48 private ScheduledExecutorService scheduler;
50 private MqttBrokerHandlerFactory subject;
56 private ThingHandlerCallback callback;
59 MQTTTopicDiscoveryParticipant listener;
61 private MqttBrokerConnectionEx connection;
63 private BrokerHandler handler;
67 scheduler = new ScheduledThreadPoolExecutor(1);
69 when(thing.getUID()).thenReturn(MqttThingID.getThingUID("10.10.0.10", 80));
70 connection = spy(new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
71 connection.setTimeoutExecutor(scheduler, 10);
72 connection.setConnectionCallback(connection);
74 Configuration config = new Configuration();
75 config.put("host", "10.10.0.10");
76 config.put("port", 80);
77 when(thing.getConfiguration()).thenReturn(config);
79 handler = spy(new BrokerHandlerEx(thing, connection));
80 handler.setCallback(callback);
82 subject = new MqttBrokerHandlerFactory();
86 public void tearDown() {
87 scheduler.shutdownNow();
91 public void firstSubscribeThenHandler() {
93 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
95 subject.subscribe(listener, "topic");
96 subject.createdHandler(handler);
97 assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
99 final byte[] bytes = "TEST".getBytes();
100 connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
101 verify(listener).receivedMessage(eq(thing.getUID()), eq(connection), eq("topic"), eq(bytes));
105 public void firstHandlerThenSubscribe() {
106 handler.initialize();
107 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
109 subject.createdHandler(handler);
110 subject.subscribe(listener, "topic");
111 assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
113 // Simulate receiving
114 final byte[] bytes = "TEST".getBytes();
115 connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
116 verify(listener).receivedMessage(eq(thing.getUID()), eq(connection), eq("topic"), eq(bytes));
120 public void handlerInitializeAfterSubscribe() {
121 subject.createdHandler(handler);
122 subject.subscribe(listener, "topic");
123 assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
125 // Init handler -> create connection
126 handler.initialize();
127 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
129 // Simulate receiving
130 final byte[] bytes = "TEST".getBytes();
131 connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
132 verify(listener).receivedMessage(eq(thing.getUID()), eq(connection), eq("topic"), eq(bytes));
136 public void topicVanished() {
137 handler.initialize();
138 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
140 subject.createdHandler(handler);
141 subject.subscribe(listener, "topic");
142 assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
144 // Simulate receiving
145 final byte[] bytes = "".getBytes();
146 connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
147 verify(listener).topicVanished(eq(thing.getUID()), eq(connection), eq("topic"));