]> git.basschouten.com Git - openhab-addons.git/blob
74afba0fb2d87b17930b2f774fafdb424daeedde
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.hamcrest.CoreMatchers.hasItem;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.ArgumentMatchers.eq;
18 import static org.mockito.Mockito.*;
19
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.ScheduledThreadPoolExecutor;
22
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;
40
41 /**
42  * Test cases for the {@link MQTTTopicDiscoveryService} service.
43  *
44  * @author David Graeff - Initial contribution
45  */
46 @ExtendWith(MockitoExtension.class)
47 @MockitoSettings(strictness = Strictness.LENIENT)
48 @NonNullByDefault
49 public class MQTTTopicDiscoveryServiceTest {
50
51     private @Mock @NonNullByDefault({}) Bridge thingMock;
52     private @Mock @NonNullByDefault({}) ThingHandlerCallback callbackMock;
53     private @Mock @NonNullByDefault({}) MQTTTopicDiscoveryParticipant listenerMock;
54
55     private @NonNullByDefault({}) MqttBrokerConnectionEx connection;
56     private @NonNullByDefault({}) BrokerHandler handler;
57     private @NonNullByDefault({}) ScheduledExecutorService scheduler;
58     private @NonNullByDefault({}) MqttBrokerHandlerFactory subject;
59
60     @BeforeEach
61     public void setUp() {
62         scheduler = new ScheduledThreadPoolExecutor(1);
63
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);
68
69         Configuration config = new Configuration();
70         config.put("host", "10.10.0.10");
71         config.put("port", 80);
72         when(thingMock.getConfiguration()).thenReturn(config);
73
74         handler = spy(new BrokerHandlerEx(thingMock, connection));
75         handler.setCallback(callbackMock);
76
77         subject = new MqttBrokerHandlerFactory();
78     }
79
80     @AfterEach
81     public void tearDown() {
82         scheduler.shutdownNow();
83     }
84
85     @Test
86     public void firstSubscribeThenHandler() {
87         handler.initialize();
88         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
89
90         subject.subscribe(listenerMock, "topic");
91         subject.createdHandler(handler);
92         assertThat(subject.discoveryTopics.get("topic"), hasItem(listenerMock));
93         // Simulate receiving
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));
97     }
98
99     @Test
100     public void firstHandlerThenSubscribe() {
101         handler.initialize();
102         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
103
104         subject.createdHandler(handler);
105         subject.subscribe(listenerMock, "topic");
106         assertThat(subject.discoveryTopics.get("topic"), hasItem(listenerMock));
107
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));
112     }
113
114     @Test
115     public void handlerInitializeAfterSubscribe() {
116         subject.createdHandler(handler);
117         subject.subscribe(listenerMock, "topic");
118         assertThat(subject.discoveryTopics.get("topic"), hasItem(listenerMock));
119
120         // Init handler -> create connection
121         handler.initialize();
122         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
123
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));
128     }
129
130     @Test
131     public void topicVanished() {
132         handler.initialize();
133         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
134
135         subject.createdHandler(handler);
136         subject.subscribe(listenerMock, "topic");
137         assertThat(subject.discoveryTopics.get("topic"), hasItem(listenerMock));
138
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"));
143     }
144 }