]> git.basschouten.com Git - openhab-addons.git/blob
a41a740a8f2273681d544360ccc4290e6243bc9d
[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.homeassistant;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.nio.charset.StandardCharsets;
19 import java.util.Properties;
20 import java.util.concurrent.CompletableFuture;
21 import java.util.concurrent.TimeUnit;
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.openhab.core.io.transport.mqtt.MqttBrokerConnection;
27 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
28 import org.openhab.core.test.java.JavaOSGiTest;
29
30 import io.moquette.BrokerConstants;
31 import io.moquette.broker.Server;
32
33 /**
34  * Creates a Moquette MQTT broker instance and a {@link MqttBrokerConnection} for testing MQTT bindings.
35  *
36  * @author Wouter Born - Initial contribution
37  */
38 @NonNullByDefault
39 public class MqttOSGiTest extends JavaOSGiTest {
40
41     private static final String BROKER_ID = "test-broker";
42     private static final int BROKER_PORT = Integer.getInteger("mqttbroker.port", 1883);
43
44     protected @NonNullByDefault({}) MqttBrokerConnection brokerConnection;
45
46     private Server moquetteServer = new Server();
47
48     @BeforeEach
49     public void beforeEach() throws Exception {
50         registerVolatileStorageService();
51
52         moquetteServer = new Server();
53         moquetteServer.startServer(brokerProperties());
54
55         brokerConnection = createBrokerConnection(BROKER_ID);
56     }
57
58     @AfterEach
59     public void afterEach() throws Exception {
60         brokerConnection.stop().get(5, TimeUnit.SECONDS);
61         moquetteServer.stopServer();
62     }
63
64     private Properties brokerProperties() {
65         Properties properties = new Properties();
66         properties.put(BrokerConstants.HOST_PROPERTY_NAME, BrokerConstants.HOST);
67         properties.put(BrokerConstants.PORT_PROPERTY_NAME, String.valueOf(BROKER_PORT));
68         properties.put(BrokerConstants.SSL_PORT_PROPERTY_NAME, BrokerConstants.DISABLED_PORT_BIND);
69         properties.put(BrokerConstants.WEB_SOCKET_PORT_PROPERTY_NAME, BrokerConstants.DISABLED_PORT_BIND);
70         properties.put(BrokerConstants.WSS_PORT_PROPERTY_NAME, BrokerConstants.DISABLED_PORT_BIND);
71         return properties;
72     }
73
74     protected MqttBrokerConnection createBrokerConnection(String clientId) throws Exception {
75         MqttBrokerConnection connection = new MqttBrokerConnection(BrokerConstants.HOST, BROKER_PORT, false, clientId);
76         connection.setQos(1);
77         connection.start().get(5, TimeUnit.SECONDS);
78
79         waitForAssert(() -> assertThat(connection.connectionState(), is(MqttConnectionState.CONNECTED)));
80
81         return connection;
82     }
83
84     protected CompletableFuture<Boolean> publish(String topic, String message) {
85         return brokerConnection.publish(topic, message.getBytes(StandardCharsets.UTF_8), 1, true);
86     }
87 }