]> git.basschouten.com Git - openhab-addons.git/blob
7a25dafaf7017516a20032824e8cd26b1610bdb6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.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;
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.WARN)
48 public class MQTTTopicDiscoveryServiceTest {
49     private ScheduledExecutorService scheduler;
50
51     private MqttBrokerHandlerFactory subject;
52
53     @Mock
54     private MqttService mqttService;
55
56     @Mock
57     private Bridge thing;
58
59     @Mock
60     private ThingHandlerCallback callback;
61
62     @Mock
63     MQTTTopicDiscoveryParticipant listener;
64
65     private MqttBrokerConnectionEx connection;
66
67     private BrokerHandler handler;
68
69     @BeforeEach
70     public void setUp() {
71         scheduler = new ScheduledThreadPoolExecutor(1);
72
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);
77
78         Configuration config = new Configuration();
79         config.put("host", "10.10.0.10");
80         config.put("port", 80);
81         when(thing.getConfiguration()).thenReturn(config);
82
83         handler = spy(new BrokerHandlerEx(thing, connection));
84         handler.setCallback(callback);
85
86         subject = new MqttBrokerHandlerFactory(mqttService);
87     }
88
89     @AfterEach
90     public void tearDown() {
91         scheduler.shutdownNow();
92     }
93
94     @Test
95     public void firstSubscribeThenHandler() {
96         handler.initialize();
97         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
98
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));
106     }
107
108     @Test
109     public void firstHandlerThenSubscribe() {
110         handler.initialize();
111         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
112
113         subject.createdHandler(handler);
114         subject.subscribe(listener, "topic");
115         assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
116
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));
121     }
122
123     @Test
124     public void handlerInitializeAfterSubscribe() {
125         subject.createdHandler(handler);
126         subject.subscribe(listener, "topic");
127         assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
128
129         // Init handler -> create connection
130         handler.initialize();
131         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
132
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));
137     }
138
139     @Test
140     public void topicVanished() {
141         handler.initialize();
142         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
143
144         subject.createdHandler(handler);
145         subject.subscribe(listener, "topic");
146         assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
147
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"));
152     }
153 }