]> git.basschouten.com Git - openhab-addons.git/blob
66bb79f7b04dd4e48550126926a4333f59eb23e4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.*;
20 import static org.mockito.MockitoAnnotations.openMocks;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.concurrent.CompletableFuture;
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.ExecutionException;
30 import java.util.concurrent.ScheduledExecutorService;
31 import java.util.concurrent.ScheduledThreadPoolExecutor;
32 import java.util.concurrent.TimeUnit;
33 import java.util.concurrent.TimeoutException;
34
35 import org.eclipse.jdt.annotation.NonNullByDefault;
36 import org.eclipse.jdt.annotation.Nullable;
37 import org.junit.jupiter.api.AfterEach;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.mockito.Mock;
41 import org.openhab.binding.mqtt.generic.AvailabilityTracker;
42 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
43 import org.openhab.binding.mqtt.generic.MqttChannelTypeProvider;
44 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
45 import org.openhab.binding.mqtt.homeassistant.internal.AbstractComponent;
46 import org.openhab.binding.mqtt.homeassistant.internal.ChannelConfigurationTypeAdapterFactory;
47 import org.openhab.binding.mqtt.homeassistant.internal.ComponentSwitch;
48 import org.openhab.binding.mqtt.homeassistant.internal.DiscoverComponents;
49 import org.openhab.binding.mqtt.homeassistant.internal.DiscoverComponents.ComponentDiscovered;
50 import org.openhab.binding.mqtt.homeassistant.internal.HaID;
51 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
52 import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
53 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
54 import org.openhab.core.io.transport.mqtt.MqttService;
55 import org.openhab.core.library.types.OnOffType;
56 import org.openhab.core.test.java.JavaOSGiTest;
57 import org.openhab.core.types.State;
58 import org.openhab.core.types.UnDefType;
59 import org.openhab.core.util.UIDUtils;
60 import org.osgi.service.cm.ConfigurationAdmin;
61
62 import com.google.gson.Gson;
63 import com.google.gson.GsonBuilder;
64
65 /**
66  * A full implementation test, that starts the embedded MQTT broker and publishes a homeassistant MQTT discovery device
67  * tree.
68  *
69  * @author David Graeff - Initial contribution
70  */
71 @NonNullByDefault
72 public class HomeAssistantMQTTImplementationTest extends JavaOSGiTest {
73     private @NonNullByDefault({}) ConfigurationAdmin configurationAdmin;
74     private @NonNullByDefault({}) MqttService mqttService;
75     private @NonNullByDefault({}) MqttBrokerConnection embeddedConnection;
76     private @NonNullByDefault({}) MqttBrokerConnection connection;
77     private int registeredTopics = 100;
78     private @Nullable Throwable failure;
79
80     private @NonNullByDefault({}) AutoCloseable mocksCloseable;
81
82     private @Mock @NonNullByDefault({}) ChannelStateUpdateListener channelStateUpdateListener;
83     private @Mock @NonNullByDefault({}) AvailabilityTracker availabilityTracker;
84     private @Mock @NonNullByDefault({}) TransformationServiceProvider transformationServiceProvider;
85
86     /**
87      * Create an observer that fails the test as soon as the broker client connection changes its connection state
88      * to something else then CONNECTED.
89      */
90     private final MqttConnectionObserver failIfChange = (state, error) -> assertThat(state,
91             is(MqttConnectionState.CONNECTED));
92     private final String testObjectTopic = "homeassistant/switch/node/"
93             + ThingChannelConstants.testHomeAssistantThing.getId();
94
95     @BeforeEach
96     public void beforeEach() throws Exception {
97         registerVolatileStorageService();
98         mocksCloseable = openMocks(this);
99         configurationAdmin = getService(ConfigurationAdmin.class);
100         mqttService = getService(MqttService.class);
101
102         // Wait for the EmbeddedBrokerService internal connection to be connected
103         embeddedConnection = new EmbeddedBrokerTools(configurationAdmin, mqttService).waitForConnection();
104
105         connection = new MqttBrokerConnection(embeddedConnection.getHost(), embeddedConnection.getPort(),
106                 embeddedConnection.isSecure(), "ha_mqtt");
107         connection.start().get(2, TimeUnit.SECONDS);
108         assertThat(connection.connectionState(), is(MqttConnectionState.CONNECTED));
109
110         // If the connection state changes in between -> fail
111         connection.addConnectionObserver(failIfChange);
112
113         // Create topic string and config for one example HA component (a Switch)
114         final String config = "{'name':'testname','state_topic':'" + testObjectTopic + "/state','command_topic':'"
115                 + testObjectTopic + "/set'}";
116
117         // Publish component configurations and component states to MQTT
118         List<CompletableFuture<Boolean>> futures = new ArrayList<>();
119         futures.add(embeddedConnection.publish(testObjectTopic + "/config", config.getBytes(), 0, true));
120         futures.add(embeddedConnection.publish(testObjectTopic + "/state", "ON".getBytes(), 0, true));
121
122         registeredTopics = futures.size();
123         CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(2, TimeUnit.SECONDS);
124
125         failure = null;
126
127         doReturn(null).when(transformationServiceProvider).getTransformationService(any());
128     }
129
130     @AfterEach
131     public void afterEach() throws Exception {
132         if (connection != null) {
133             connection.removeConnectionObserver(failIfChange);
134             connection.stop().get(2, TimeUnit.SECONDS);
135         }
136
137         mocksCloseable.close();
138     }
139
140     @Test
141     public void reconnectTest() throws InterruptedException, ExecutionException, TimeoutException {
142         connection.removeConnectionObserver(failIfChange);
143         connection.stop().get(2, TimeUnit.SECONDS);
144         connection = new MqttBrokerConnection(embeddedConnection.getHost(), embeddedConnection.getPort(),
145                 embeddedConnection.isSecure(), "ha_mqtt");
146         connection.start().get(2, TimeUnit.SECONDS);
147     }
148
149     @Test
150     public void retrieveAllTopics() throws InterruptedException, ExecutionException, TimeoutException {
151         CountDownLatch c = new CountDownLatch(registeredTopics);
152         connection.subscribe("homeassistant/+/+/" + ThingChannelConstants.testHomeAssistantThing.getId() + "/#",
153                 (topic, payload) -> c.countDown()).get(2, TimeUnit.SECONDS);
154         assertTrue(c.await(2, TimeUnit.SECONDS),
155                 "Connection " + connection.getClientId() + " not retrieving all topics");
156     }
157
158     @Test
159     public void parseHATree() throws InterruptedException, ExecutionException, TimeoutException {
160         MqttChannelTypeProvider channelTypeProvider = mock(MqttChannelTypeProvider.class);
161
162         final Map<String, AbstractComponent<?>> haComponents = new HashMap<>();
163         Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory()).create();
164
165         ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(4);
166         DiscoverComponents discover = spy(new DiscoverComponents(ThingChannelConstants.testHomeAssistantThing,
167                 scheduler, channelStateUpdateListener, availabilityTracker, gson, transformationServiceProvider));
168
169         // The DiscoverComponents object calls ComponentDiscovered callbacks.
170         // In the following implementation we add the found component to the `haComponents` map
171         // and add the types to the channelTypeProvider, like in the real Thing handler.
172         final CountDownLatch latch = new CountDownLatch(1);
173         ComponentDiscovered cd = (haID, c) -> {
174             haComponents.put(c.uid().getId(), c);
175             c.addChannelTypes(channelTypeProvider);
176             channelTypeProvider.setChannelGroupType(c.groupTypeUID(), c.type());
177             latch.countDown();
178         };
179
180         // Start the discovery for 2000ms. Forced timeout after 4000ms.
181         HaID haID = new HaID(testObjectTopic + "/config");
182         CompletableFuture<Void> future = discover.startDiscovery(connection, 2000, Collections.singleton(haID), cd)
183                 .thenRun(() -> {
184                 }).exceptionally(e -> {
185                     failure = e;
186                     return null;
187                 });
188
189         assertTrue(latch.await(4, TimeUnit.SECONDS));
190         future.get(2, TimeUnit.SECONDS);
191
192         // No failure expected and one discovered result
193         assertNull(failure);
194         assertThat(haComponents.size(), is(1));
195
196         // For the switch component we should have one channel group type and one channel type
197         // setChannelGroupType is called once above
198         verify(channelTypeProvider, times(2)).setChannelGroupType(any(), any());
199         verify(channelTypeProvider, times(1)).setChannelType(any(), any());
200
201         String channelGroupId = UIDUtils
202                 .encode("node_" + ThingChannelConstants.testHomeAssistantThing.getId() + "_switch");
203
204         State value = haComponents.get(channelGroupId).channelTypes().get(ComponentSwitch.switchChannelID).getState()
205                 .getCache().getChannelState();
206         assertThat(value, is(UnDefType.UNDEF));
207
208         haComponents.values().stream().map(e -> e.start(connection, scheduler, 100))
209                 .reduce(CompletableFuture.completedFuture(null), (a, v) -> a.thenCompose(b -> v)).exceptionally(e -> {
210                     failure = e;
211                     return null;
212                 }).get();
213
214         // We should have received the retained value, while subscribing to the channels MQTT state topic.
215         verify(channelStateUpdateListener, timeout(4000).times(1)).updateChannelState(any(), any());
216
217         // Value should be ON now.
218         value = haComponents.get(channelGroupId).channelTypes().get(ComponentSwitch.switchChannelID).getState()
219                 .getCache().getChannelState();
220         assertThat(value, is(OnOffType.ON));
221     }
222 }