]> git.basschouten.com Git - openhab-addons.git/blob
9a459ad7c498604f49aa173fbe4f1eadd91e7dc8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.io.mqttembeddedbroker;
14
15 import static org.junit.Assert.assertTrue;
16
17 import java.util.concurrent.Semaphore;
18 import java.util.concurrent.TimeUnit;
19
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
24 import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
25 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
26 import org.openhab.core.io.transport.mqtt.MqttService;
27 import org.openhab.core.io.transport.mqtt.MqttServiceObserver;
28 import org.openhab.io.mqttembeddedbroker.Constants;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * A full implementation test, that starts the embedded MQTT broker and publishes a homeassistant MQTT discovery device
34  * tree.
35  *
36  * @author David Graeff - Initial contribution
37  */
38 @NonNullByDefault
39 public class EmbeddedBrokerTools {
40     private final Logger logger = LoggerFactory.getLogger(EmbeddedBrokerTools.class);
41     public @Nullable MqttBrokerConnection embeddedConnection = null;
42
43     /**
44      * Request the embedded broker connection from the {@link MqttService} and wait for a connection to be established.
45      *
46      * @throws InterruptedException
47      */
48     public MqttBrokerConnection waitForConnection(MqttService mqttService) throws InterruptedException {
49         embeddedConnection = mqttService.getBrokerConnection(Constants.CLIENTID);
50         if (embeddedConnection == null) {
51             Semaphore semaphore = new Semaphore(1);
52             semaphore.acquire();
53             MqttServiceObserver observer = new MqttServiceObserver() {
54
55                 @Override
56                 public void brokerAdded(@NonNull String brokerID, @NonNull MqttBrokerConnection broker) {
57                     if (brokerID.equals(Constants.CLIENTID)) {
58                         embeddedConnection = broker;
59                         semaphore.release();
60                     }
61                 }
62
63                 @Override
64                 public void brokerRemoved(@NonNull String brokerID, @NonNull MqttBrokerConnection broker) {
65                 }
66
67             };
68             mqttService.addBrokersListener(observer);
69             assertTrue("Wait for embedded connection client failed", semaphore.tryAcquire(700, TimeUnit.MILLISECONDS));
70         }
71         MqttBrokerConnection embeddedConnection = this.embeddedConnection;
72         if (embeddedConnection == null) {
73             throw new IllegalStateException();
74         }
75
76         logger.warn("waitForConnection {}", embeddedConnection.connectionState());
77         Semaphore semaphore = new Semaphore(1);
78         semaphore.acquire();
79         MqttConnectionObserver mqttConnectionObserver = (state, error) -> {
80             if (state == MqttConnectionState.CONNECTED) {
81                 semaphore.release();
82             }
83         };
84         embeddedConnection.addConnectionObserver(mqttConnectionObserver);
85         if (embeddedConnection.connectionState() == MqttConnectionState.CONNECTED) {
86             semaphore.release();
87         }
88         assertTrue("Connection " + embeddedConnection.getClientId() + " failed. State: "
89                 + embeddedConnection.connectionState(), semaphore.tryAcquire(500, TimeUnit.MILLISECONDS));
90         return embeddedConnection;
91     }
92
93 }