]> git.basschouten.com Git - openhab-addons.git/blob
4ef12460acd7ec9c11f90363523c4a3db965ff28
[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.binding.mqtt.generic.internal.handler;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.junit.Assert.assertThat;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
19 import static org.openhab.binding.mqtt.generic.internal.handler.ThingChannelConstants.*;
20
21 import java.util.concurrent.CompletableFuture;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.openhab.binding.mqtt.generic.ChannelConfig;
28 import org.openhab.binding.mqtt.generic.ChannelConfigBuilder;
29 import org.openhab.binding.mqtt.generic.ChannelState;
30 import org.openhab.binding.mqtt.generic.MqttChannelStateDescriptionProvider;
31 import org.openhab.binding.mqtt.generic.ThingHandlerHelper;
32 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
33 import org.openhab.binding.mqtt.generic.values.OnOffValue;
34 import org.openhab.binding.mqtt.generic.values.TextValue;
35 import org.openhab.binding.mqtt.generic.values.ValueFactory;
36 import org.openhab.binding.mqtt.handler.AbstractBrokerHandler;
37 import org.openhab.core.config.core.Configuration;
38 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
39 import org.openhab.core.library.types.OnOffType;
40 import org.openhab.core.library.types.StringType;
41 import org.openhab.core.thing.ChannelUID;
42 import org.openhab.core.thing.Thing;
43 import org.openhab.core.thing.ThingStatus;
44 import org.openhab.core.thing.ThingStatusDetail;
45 import org.openhab.core.thing.ThingStatusInfo;
46 import org.openhab.core.thing.binding.ThingHandlerCallback;
47 import org.openhab.core.types.RefreshType;
48
49 /**
50  * Tests cases for {@link GenericMQTTThingHandler}.
51  *
52  * @author David Graeff - Initial contribution
53  */
54 public class GenericThingHandlerTests {
55     @Mock
56     private ThingHandlerCallback callback;
57
58     @Mock
59     private Thing thing;
60
61     @Mock
62     private AbstractBrokerHandler bridgeHandler;
63
64     @Mock
65     private MqttBrokerConnection connection;
66
67     private GenericMQTTThingHandler thingHandler;
68
69     @Before
70     public void setUp() {
71         ThingStatusInfo thingStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
72
73         MockitoAnnotations.initMocks(this);
74         // Mock the thing: We need the thingUID and the bridgeUID
75         when(thing.getUID()).thenReturn(testGenericThing);
76         when(thing.getChannels()).thenReturn(thingChannelList);
77         when(thing.getStatusInfo()).thenReturn(thingStatus);
78         when(thing.getConfiguration()).thenReturn(new Configuration());
79
80         // Return the mocked connection object if the bridge handler is asked for it
81         when(bridgeHandler.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connection));
82
83         CompletableFuture<Void> voidFutureComplete = new CompletableFuture<>();
84         voidFutureComplete.complete(null);
85         doReturn(voidFutureComplete).when(connection).unsubscribeAll();
86         doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
87         doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
88         doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any());
89         doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any(), anyInt(),
90                 anyBoolean());
91
92         thingHandler = spy(new GenericMQTTThingHandler(thing, mock(MqttChannelStateDescriptionProvider.class),
93                 mock(TransformationServiceProvider.class), 1500));
94         thingHandler.setCallback(callback);
95
96         // Return the bridge handler if the thing handler asks for it
97         doReturn(bridgeHandler).when(thingHandler).getBridgeHandler();
98
99         // The broker connection bridge is by default online
100         doReturn(thingStatus).when(thingHandler).getBridgeStatus();
101     }
102
103     @Test(expected = IllegalArgumentException.class)
104     public void initializeWithUnknownThingUID() {
105         ChannelConfig config = textConfiguration().as(ChannelConfig.class);
106         thingHandler.createChannelState(config, new ChannelUID(testGenericThing, "test"),
107                 ValueFactory.createValueState(config, unknownChannel.getId()));
108     }
109
110     @Test
111     public void initialize() {
112         thingHandler.initialize();
113         verify(thingHandler).bridgeStatusChanged(any());
114         verify(thingHandler).start(any());
115         assertThat(thingHandler.getConnection(), is(connection));
116
117         ChannelState channelConfig = thingHandler.channelStateByChannelUID.get(textChannelUID);
118         assertThat(channelConfig.getStateTopic(), is("test/state"));
119         assertThat(channelConfig.getCommandTopic(), is("test/command"));
120
121         verify(connection).subscribe(eq(channelConfig.getStateTopic()), eq(channelConfig));
122
123         verify(callback).statusUpdated(eq(thing), argThat((arg) -> arg.getStatus().equals(ThingStatus.ONLINE)
124                 && arg.getStatusDetail().equals(ThingStatusDetail.NONE)));
125     }
126
127     @Test
128     public void handleCommandRefresh() {
129         TextValue value = spy(new TextValue());
130         value.update(new StringType("DEMOVALUE"));
131
132         ChannelState channelConfig = mock(ChannelState.class);
133         doReturn(CompletableFuture.completedFuture(true)).when(channelConfig).start(any(), any(), anyInt());
134         doReturn(CompletableFuture.completedFuture(true)).when(channelConfig).stop();
135         doReturn(value).when(channelConfig).getCache();
136         doReturn(channelConfig).when(thingHandler).createChannelState(any(), any(), any());
137         thingHandler.initialize();
138
139         ThingHandlerHelper.setConnection(thingHandler, connection);
140
141         thingHandler.handleCommand(textChannelUID, RefreshType.REFRESH);
142         verify(callback).stateUpdated(eq(textChannelUID), argThat(arg -> "DEMOVALUE".equals(arg.toString())));
143     }
144
145     @Test
146     public void handleCommandUpdateString() {
147         TextValue value = spy(new TextValue());
148         ChannelState channelConfig = spy(
149                 new ChannelState(ChannelConfigBuilder.create("stateTopic", "commandTopic").build(), textChannelUID,
150                         value, thingHandler));
151         doReturn(channelConfig).when(thingHandler).createChannelState(any(), any(), any());
152         thingHandler.initialize();
153         ThingHandlerHelper.setConnection(thingHandler, connection);
154
155         StringType updateValue = new StringType("UPDATE");
156         thingHandler.handleCommand(textChannelUID, updateValue);
157         verify(value).update(eq(updateValue));
158         assertThat(channelConfig.getCache().getChannelState().toString(), is("UPDATE"));
159     }
160
161     @Test
162     public void handleCommandUpdateBoolean() {
163         OnOffValue value = spy(new OnOffValue("ON", "OFF"));
164         ChannelState channelConfig = spy(
165                 new ChannelState(ChannelConfigBuilder.create("stateTopic", "commandTopic").build(), textChannelUID,
166                         value, thingHandler));
167         doReturn(channelConfig).when(thingHandler).createChannelState(any(), any(), any());
168         thingHandler.initialize();
169         ThingHandlerHelper.setConnection(thingHandler, connection);
170
171         StringType updateValue = new StringType("ON");
172         thingHandler.handleCommand(textChannelUID, updateValue);
173
174         verify(value).update(eq(updateValue));
175         assertThat(channelConfig.getCache().getChannelState(), is(OnOffType.ON));
176     }
177
178     @Test
179     public void processMessage() {
180         TextValue textValue = new TextValue();
181         ChannelState channelConfig = spy(
182                 new ChannelState(ChannelConfigBuilder.create("test/state", "test/state/set").build(), textChannelUID,
183                         textValue, thingHandler));
184         doReturn(channelConfig).when(thingHandler).createChannelState(any(), any(), any());
185         thingHandler.initialize();
186         byte payload[] = "UPDATE".getBytes();
187         // Test process message
188         channelConfig.processMessage("test/state", payload);
189
190         verify(callback, atLeastOnce()).statusUpdated(eq(thing),
191                 argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
192
193         verify(callback).stateUpdated(eq(textChannelUID), argThat(arg -> "UPDATE".equals(arg.toString())));
194         assertThat(textValue.getChannelState().toString(), is("UPDATE"));
195     }
196 }