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.ruuvigateway;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertNotNull;
19 import java.nio.charset.StandardCharsets;
20 import java.util.Properties;
21 import java.util.concurrent.CompletableFuture;
22 import java.util.concurrent.TimeUnit;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.openhab.core.config.discovery.inbox.Inbox;
28 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
29 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
30 import org.openhab.core.items.ItemProvider;
31 import org.openhab.core.items.ItemRegistry;
32 import org.openhab.core.items.ManagedItemProvider;
33 import org.openhab.core.test.java.JavaOSGiTest;
34 import org.openhab.core.thing.ManagedThingProvider;
35 import org.openhab.core.thing.ThingProvider;
36 import org.openhab.core.thing.link.ItemChannelLinkProvider;
37 import org.openhab.core.thing.link.ManagedItemChannelLinkProvider;
39 import io.moquette.BrokerConstants;
40 import io.moquette.broker.Server;
43 * Creates a Moquette MQTT broker instance and a {@link MqttBrokerConnection} for testing MQTT bindings.
45 * @author Wouter Born - Initial contribution
46 * @author Sami Salonen - Copied to MQTT Ruuvi Gateway addon
49 public class MqttOSGiTest extends JavaOSGiTest {
51 private static final String BROKER_ID = "test-broker";
52 @SuppressWarnings("null")
53 private static final int BROKER_PORT = Integer.getInteger("mqttbroker.port", 1883);
55 protected @NonNullByDefault({}) MqttBrokerConnection brokerConnection;
57 private Server moquetteServer = new Server();
58 protected @NonNullByDefault({}) ManagedThingProvider thingProvider;
59 protected @NonNullByDefault({}) ManagedItemProvider itemProvider;
60 protected @NonNullByDefault({}) ItemRegistry itemRegistry;
61 protected @NonNullByDefault({}) ManagedItemChannelLinkProvider itemChannelLinkProvider;
62 protected @NonNullByDefault({}) Inbox inbox;
65 public void beforeEach() throws Exception {
66 registerVolatileStorageService();
68 thingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
69 assertNotNull(thingProvider, "Could not get ManagedThingProvider");
71 itemProvider = getService(ItemProvider.class, ManagedItemProvider.class);
72 assertNotNull(itemProvider, "Could not get ManagedItemProvider");
73 itemRegistry = getService(ItemRegistry.class);
74 assertNotNull(itemProvider, "Could not get ItemRegistry");
76 itemChannelLinkProvider = getService(ItemChannelLinkProvider.class, ManagedItemChannelLinkProvider.class);
77 assertNotNull(itemChannelLinkProvider, "Could not get ManagedItemChannelLinkProvider");
79 inbox = getService(Inbox.class);
80 assertNotNull(inbox, "Could not get Inbox");
82 moquetteServer = new Server();
83 moquetteServer.startServer(brokerProperties());
85 brokerConnection = createBrokerConnection(BROKER_ID);
89 public void afterEach() throws Exception {
90 brokerConnection.stop().get(5, TimeUnit.SECONDS);
91 moquetteServer.stopServer();
94 private Properties brokerProperties() {
95 Properties properties = new Properties();
96 properties.put(BrokerConstants.HOST_PROPERTY_NAME, BrokerConstants.HOST);
97 properties.put(BrokerConstants.PORT_PROPERTY_NAME, String.valueOf(BROKER_PORT));
98 properties.put(BrokerConstants.SSL_PORT_PROPERTY_NAME, BrokerConstants.DISABLED_PORT_BIND);
99 properties.put(BrokerConstants.WEB_SOCKET_PORT_PROPERTY_NAME, BrokerConstants.DISABLED_PORT_BIND);
100 properties.put(BrokerConstants.WSS_PORT_PROPERTY_NAME, BrokerConstants.DISABLED_PORT_BIND);
104 protected MqttBrokerConnection createBrokerConnection(String clientId) throws Exception {
105 MqttBrokerConnection connection = new MqttBrokerConnection(BrokerConstants.HOST, BROKER_PORT, false, clientId);
106 connection.setQos(1);
107 connection.start().get(5, TimeUnit.SECONDS);
109 waitForAssert(() -> assertThat(connection.connectionState(), is(MqttConnectionState.CONNECTED)));
114 protected CompletableFuture<Boolean> publish(String topic, String message) {
115 return brokerConnection.publish(topic, message.getBytes(StandardCharsets.UTF_8), 1, true);
119 * Whether tests are run in Continuous Integration environment, i.e. Jenkins or Travis CI
121 * Travis CI is detected using CI environment variable, see https://docs.travis-ci.com/us>
122 * Jenkins CI is detected using JENKINS_HOME environment variable
126 protected boolean isRunningInCI() {
127 String jenkinsHome = System.getenv("JENKINS_HOME");
128 return "true".equals(System.getenv("CI")) || (jenkinsHome != null && !jenkinsHome.isBlank());