]> git.basschouten.com Git - openhab-addons.git/blob
6e7addfc215f6dd3a296bc66ac9e69a933bd7d84
[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.ruuvigateway;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertNotNull;
18
19 import java.nio.charset.StandardCharsets;
20 import java.util.Properties;
21 import java.util.concurrent.CompletableFuture;
22 import java.util.concurrent.TimeUnit;
23
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;
38
39 import io.moquette.BrokerConstants;
40 import io.moquette.broker.Server;
41
42 /**
43  * Creates a Moquette MQTT broker instance and a {@link MqttBrokerConnection} for testing MQTT bindings.
44  *
45  * @author Wouter Born - Initial contribution
46  * @author Sami Salonen - Copied to MQTT Ruuvi Gateway addon
47  */
48 @NonNullByDefault
49 public class MqttOSGiTest extends JavaOSGiTest {
50
51     private static final String BROKER_ID = "test-broker";
52     @SuppressWarnings("null")
53     private static final int BROKER_PORT = Integer.getInteger("mqttbroker.port", 1883);
54
55     protected @NonNullByDefault({}) MqttBrokerConnection brokerConnection;
56
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;
63
64     @BeforeEach
65     public void beforeEach() throws Exception {
66         registerVolatileStorageService();
67
68         thingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
69         assertNotNull(thingProvider, "Could not get ManagedThingProvider");
70
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");
75
76         itemChannelLinkProvider = getService(ItemChannelLinkProvider.class, ManagedItemChannelLinkProvider.class);
77         assertNotNull(itemChannelLinkProvider, "Could not get ManagedItemChannelLinkProvider");
78
79         inbox = getService(Inbox.class);
80         assertNotNull(inbox, "Could not get Inbox");
81
82         moquetteServer = new Server();
83         moquetteServer.startServer(brokerProperties());
84
85         brokerConnection = createBrokerConnection(BROKER_ID);
86     }
87
88     @AfterEach
89     public void afterEach() throws Exception {
90         brokerConnection.stop().get(5, TimeUnit.SECONDS);
91         moquetteServer.stopServer();
92     }
93
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);
101         return properties;
102     }
103
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);
108
109         waitForAssert(() -> assertThat(connection.connectionState(), is(MqttConnectionState.CONNECTED)));
110
111         return connection;
112     }
113
114     protected CompletableFuture<Boolean> publish(String topic, String message) {
115         return brokerConnection.publish(topic, message.getBytes(StandardCharsets.UTF_8), 1, true);
116     }
117
118     /**
119      * Whether tests are run in Continuous Integration environment, i.e. Jenkins or Travis CI
120      *
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
123      *
124      * @return
125      */
126     protected boolean isRunningInCI() {
127         String jenkinsHome = System.getenv("JENKINS_HOME");
128         return "true".equals(System.getenv("CI")) || (jenkinsHome != null && !jenkinsHome.isBlank());
129     }
130 }