]> git.basschouten.com Git - openhab-addons.git/blob
e41f4dffb06a909b33d50a350a872c90619ada80
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.thing.Bridge;
38 import org.openhab.core.thing.binding.ThingHandlerCallback;
39
40 /**
41  * Test cases for the {@link MQTTTopicDiscoveryService} service.
42  *
43  * @author David Graeff - Initial contribution
44  */
45 @ExtendWith(MockitoExtension.class)
46 @MockitoSettings(strictness = Strictness.WARN)
47 public class MQTTTopicDiscoveryServiceTest {
48     private ScheduledExecutorService scheduler;
49
50     private MqttBrokerHandlerFactory subject;
51
52     @Mock
53     private Bridge thing;
54
55     @Mock
56     private ThingHandlerCallback callback;
57
58     @Mock
59     MQTTTopicDiscoveryParticipant listener;
60
61     private MqttBrokerConnectionEx connection;
62
63     private BrokerHandler handler;
64
65     @BeforeEach
66     public void setUp() {
67         scheduler = new ScheduledThreadPoolExecutor(1);
68
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);
73
74         Configuration config = new Configuration();
75         config.put("host", "10.10.0.10");
76         config.put("port", 80);
77         when(thing.getConfiguration()).thenReturn(config);
78
79         handler = spy(new BrokerHandlerEx(thing, connection));
80         handler.setCallback(callback);
81
82         subject = new MqttBrokerHandlerFactory();
83     }
84
85     @AfterEach
86     public void tearDown() {
87         scheduler.shutdownNow();
88     }
89
90     @Test
91     public void firstSubscribeThenHandler() {
92         handler.initialize();
93         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
94
95         subject.subscribe(listener, "topic");
96         subject.createdHandler(handler);
97         assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
98         // Simulate receiving
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));
102     }
103
104     @Test
105     public void firstHandlerThenSubscribe() {
106         handler.initialize();
107         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
108
109         subject.createdHandler(handler);
110         subject.subscribe(listener, "topic");
111         assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
112
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));
117     }
118
119     @Test
120     public void handlerInitializeAfterSubscribe() {
121         subject.createdHandler(handler);
122         subject.subscribe(listener, "topic");
123         assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
124
125         // Init handler -> create connection
126         handler.initialize();
127         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
128
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));
133     }
134
135     @Test
136     public void topicVanished() {
137         handler.initialize();
138         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
139
140         subject.createdHandler(handler);
141         subject.subscribe(listener, "topic");
142         assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
143
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"));
148     }
149 }