2 * Copyright (c) 2010-2023 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.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.mockito.junit.jupiter.MockitoSettings;
31 import org.mockito.quality.Strictness;
32 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryParticipant;
33 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryService;
34 import org.openhab.binding.mqtt.handler.BrokerHandler;
35 import org.openhab.binding.mqtt.handler.BrokerHandlerEx;
36 import org.openhab.binding.mqtt.handler.MqttBrokerConnectionEx;
37 import org.openhab.core.config.core.Configuration;
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.LENIENT)
49 public class MQTTTopicDiscoveryServiceTest {
51 private @Mock @NonNullByDefault({}) Bridge thingMock;
52 private @Mock @NonNullByDefault({}) ThingHandlerCallback callbackMock;
53 private @Mock @NonNullByDefault({}) MQTTTopicDiscoveryParticipant listenerMock;
55 private @NonNullByDefault({}) MqttBrokerConnectionEx connection;
56 private @NonNullByDefault({}) BrokerHandler handler;
57 private @NonNullByDefault({}) ScheduledExecutorService scheduler;
58 private @NonNullByDefault({}) MqttBrokerHandlerFactory subject;
62 scheduler = new ScheduledThreadPoolExecutor(1);
64 when(thingMock.getUID()).thenReturn(MqttThingID.getThingUID("10.10.0.10", 80));
65 connection = spy(new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
66 connection.setTimeoutExecutor(scheduler, 10);
67 connection.setConnectionCallback(connection);
69 Configuration config = new Configuration();
70 config.put("host", "10.10.0.10");
71 config.put("port", 80);
72 when(thingMock.getConfiguration()).thenReturn(config);
74 handler = spy(new BrokerHandlerEx(thingMock, connection));
75 handler.setCallback(callbackMock);
77 subject = new MqttBrokerHandlerFactory();
81 public void tearDown() {
82 scheduler.shutdownNow();
86 public void firstSubscribeThenHandler() {
88 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
90 subject.subscribe(listenerMock, "topic");
91 subject.createdHandler(handler);
92 assertThat(subject.discoveryTopics.get("topic"), hasItem(listenerMock));
94 final byte[] bytes = "TEST".getBytes();
95 connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
96 verify(listenerMock).receivedMessage(eq(thingMock.getUID()), eq(connection), eq("topic"), eq(bytes));
100 public void firstHandlerThenSubscribe() {
101 handler.initialize();
102 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
104 subject.createdHandler(handler);
105 subject.subscribe(listenerMock, "topic");
106 assertThat(subject.discoveryTopics.get("topic"), hasItem(listenerMock));
108 // Simulate receiving
109 final byte[] bytes = "TEST".getBytes();
110 connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
111 verify(listenerMock).receivedMessage(eq(thingMock.getUID()), eq(connection), eq("topic"), eq(bytes));
115 public void handlerInitializeAfterSubscribe() {
116 subject.createdHandler(handler);
117 subject.subscribe(listenerMock, "topic");
118 assertThat(subject.discoveryTopics.get("topic"), hasItem(listenerMock));
120 // Init handler -> create connection
121 handler.initialize();
122 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
124 // Simulate receiving
125 final byte[] bytes = "TEST".getBytes();
126 connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
127 verify(listenerMock).receivedMessage(eq(thingMock.getUID()), eq(connection), eq("topic"), eq(bytes));
131 public void topicVanished() {
132 handler.initialize();
133 BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
135 subject.createdHandler(handler);
136 subject.subscribe(listenerMock, "topic");
137 assertThat(subject.discoveryTopics.get("topic"), hasItem(listenerMock));
139 // Simulate receiving
140 final byte[] bytes = "".getBytes();
141 connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
142 verify(listenerMock).topicVanished(eq(thingMock.getUID()), eq(connection), eq("topic"));