]> git.basschouten.com Git - openhab-addons.git/blob
0cf38fb2f55b239e3ffb3811f719c61c55027358
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.concurrent.CompletableFuture;
27 import java.util.concurrent.CountDownLatch;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.ScheduledExecutorService;
30 import java.util.concurrent.ScheduledThreadPoolExecutor;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.TimeoutException;
33
34 import org.eclipse.jdt.annotation.NonNullByDefault;
35 import org.eclipse.jdt.annotation.Nullable;
36 import org.junit.jupiter.api.AfterEach;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.junit.jupiter.api.extension.ExtendWith;
40 import org.mockito.Mock;
41 import org.mockito.junit.jupiter.MockitoExtension;
42 import org.mockito.junit.jupiter.MockitoSettings;
43 import org.mockito.quality.Strictness;
44 import org.openhab.binding.mqtt.generic.AvailabilityTracker;
45 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
46 import org.openhab.binding.mqtt.generic.MqttChannelTypeProvider;
47 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
48 import org.openhab.binding.mqtt.homeassistant.internal.AbstractComponent;
49 import org.openhab.binding.mqtt.homeassistant.internal.ChannelConfigurationTypeAdapterFactory;
50 import org.openhab.binding.mqtt.homeassistant.internal.ComponentSwitch;
51 import org.openhab.binding.mqtt.homeassistant.internal.DiscoverComponents;
52 import org.openhab.binding.mqtt.homeassistant.internal.DiscoverComponents.ComponentDiscovered;
53 import org.openhab.binding.mqtt.homeassistant.internal.HaID;
54 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
55 import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
56 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
57 import org.openhab.core.io.transport.mqtt.MqttService;
58 import org.openhab.core.library.types.OnOffType;
59 import org.openhab.core.test.java.JavaOSGiTest;
60 import org.openhab.core.types.State;
61 import org.openhab.core.types.UnDefType;
62 import org.openhab.core.util.UIDUtils;
63 import org.osgi.service.cm.ConfigurationAdmin;
64
65 import com.google.gson.Gson;
66 import com.google.gson.GsonBuilder;
67
68 /**
69  * A full implementation test, that starts the embedded MQTT broker and publishes a homeassistant MQTT discovery device
70  * tree.
71  *
72  * @author David Graeff - Initial contribution
73  */
74 @ExtendWith(MockitoExtension.class)
75 @MockitoSettings(strictness = Strictness.LENIENT)
76 @NonNullByDefault
77 public class HomeAssistantMQTTImplementationTest extends JavaOSGiTest {
78     private @NonNullByDefault({}) ConfigurationAdmin configurationAdmin;
79     private @NonNullByDefault({}) MqttService mqttService;
80     private @NonNullByDefault({}) MqttBrokerConnection embeddedConnection;
81     private @NonNullByDefault({}) MqttBrokerConnection connection;
82     private int registeredTopics = 100;
83     private @Nullable Throwable failure;
84
85     private @Mock @NonNullByDefault({}) ChannelStateUpdateListener channelStateUpdateListener;
86     private @Mock @NonNullByDefault({}) AvailabilityTracker availabilityTracker;
87     private @Mock @NonNullByDefault({}) TransformationServiceProvider transformationServiceProvider;
88
89     /**
90      * Create an observer that fails the test as soon as the broker client connection changes its connection state
91      * to something else then CONNECTED.
92      */
93     private final MqttConnectionObserver failIfChange = (state, error) -> assertThat(state,
94             is(MqttConnectionState.CONNECTED));
95     private final String testObjectTopic = "homeassistant/switch/node/"
96             + ThingChannelConstants.testHomeAssistantThing.getId();
97
98     @BeforeEach
99     public void beforeEach() throws Exception {
100         registerVolatileStorageService();
101         configurationAdmin = getService(ConfigurationAdmin.class);
102         mqttService = getService(MqttService.class);
103
104         // Wait for the EmbeddedBrokerService internal connection to be connected
105         embeddedConnection = new EmbeddedBrokerTools(configurationAdmin, mqttService).waitForConnection();
106
107         connection = new MqttBrokerConnection(embeddedConnection.getHost(), embeddedConnection.getPort(),
108                 embeddedConnection.isSecure(), "ha_mqtt");
109         connection.start().get(2, TimeUnit.SECONDS);
110         assertThat(connection.connectionState(), is(MqttConnectionState.CONNECTED));
111
112         // If the connection state changes in between -> fail
113         connection.addConnectionObserver(failIfChange);
114
115         // Create topic string and config for one example HA component (a Switch)
116         final String config = "{'name':'testname','state_topic':'" + testObjectTopic + "/state','command_topic':'"
117                 + testObjectTopic + "/set'}";
118
119         // Publish component configurations and component states to MQTT
120         List<CompletableFuture<Boolean>> futures = new ArrayList<>();
121         futures.add(embeddedConnection.publish(testObjectTopic + "/config", config.getBytes(), 0, true));
122         futures.add(embeddedConnection.publish(testObjectTopic + "/state", "ON".getBytes(), 0, true));
123
124         registeredTopics = futures.size();
125         CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(2, TimeUnit.SECONDS);
126
127         failure = null;
128
129         doReturn(null).when(transformationServiceProvider).getTransformationService(any());
130     }
131
132     @AfterEach
133     public void afterEach() throws Exception {
134         if (connection != null) {
135             connection.removeConnectionObserver(failIfChange);
136             connection.stop().get(2, TimeUnit.SECONDS);
137         }
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 }