2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.generic;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.junit.Assert.*;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.*;
20 import static org.mockito.MockitoAnnotations.initMocks;
22 import java.math.BigDecimal;
23 import java.time.ZonedDateTime;
24 import java.time.format.DateTimeFormatter;
25 import java.util.Arrays;
26 import java.util.concurrent.CompletableFuture;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.ScheduledExecutorService;
29 import java.util.concurrent.ScheduledThreadPoolExecutor;
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.TimeoutException;
33 import org.eclipse.jdt.annotation.Nullable;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.Spy;
39 import org.openhab.binding.mqtt.generic.mapping.ColorMode;
40 import org.openhab.binding.mqtt.generic.values.ColorValue;
41 import org.openhab.binding.mqtt.generic.values.DateTimeValue;
42 import org.openhab.binding.mqtt.generic.values.ImageValue;
43 import org.openhab.binding.mqtt.generic.values.LocationValue;
44 import org.openhab.binding.mqtt.generic.values.NumberValue;
45 import org.openhab.binding.mqtt.generic.values.PercentageValue;
46 import org.openhab.binding.mqtt.generic.values.TextValue;
47 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
48 import org.openhab.core.library.types.HSBType;
49 import org.openhab.core.library.types.RawType;
50 import org.openhab.core.library.types.StringType;
51 import org.openhab.core.thing.ChannelUID;
54 * Tests the {@link ChannelState} class.
56 * @author David Graeff - Initial contribution
58 public class ChannelStateTests {
60 private MqttBrokerConnection connection;
63 private ChannelStateUpdateListener channelStateUpdateListener;
66 private ChannelUID channelUID;
69 private TextValue textValue;
71 private ScheduledExecutorService scheduler;
73 private ChannelConfig config = ChannelConfigBuilder.create("state", "command").build();
78 CompletableFuture<Void> voidFutureComplete = new CompletableFuture<>();
79 voidFutureComplete.complete(null);
80 doReturn(voidFutureComplete).when(connection).unsubscribeAll();
81 doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
82 doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
83 doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any());
84 doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any(), anyInt(),
87 scheduler = new ScheduledThreadPoolExecutor(1);
91 public void tearDown() {
92 scheduler.shutdownNow();
96 public void noInteractionTimeoutTest() throws InterruptedException, ExecutionException, TimeoutException {
97 ChannelState c = spy(new ChannelState(config, channelUID, textValue, channelStateUpdateListener));
98 c.start(connection, scheduler, 50).get(100, TimeUnit.MILLISECONDS);
99 verify(connection).subscribe(eq("state"), eq(c));
101 verify(connection).unsubscribe(eq("state"), eq(c));
105 public void publishFormatTest() throws InterruptedException, ExecutionException, TimeoutException {
106 ChannelState c = spy(new ChannelState(config, channelUID, textValue, channelStateUpdateListener));
108 c.start(connection, scheduler, 0).get(50, TimeUnit.MILLISECONDS);
109 verify(connection).subscribe(eq("state"), eq(c));
111 c.publishValue(new StringType("UPDATE")).get();
112 verify(connection).publish(eq("command"), argThat(p -> Arrays.equals(p, "UPDATE".getBytes())), anyInt(),
115 c.config.formatBeforePublish = "prefix%s";
116 c.publishValue(new StringType("UPDATE")).get();
117 verify(connection).publish(eq("command"), argThat(p -> Arrays.equals(p, "prefixUPDATE".getBytes())), anyInt(),
120 c.config.formatBeforePublish = "%1$s-%1$s";
121 c.publishValue(new StringType("UPDATE")).get();
122 verify(connection).publish(eq("command"), argThat(p -> Arrays.equals(p, "UPDATE-UPDATE".getBytes())), anyInt(),
125 c.config.formatBeforePublish = "%s";
126 c.config.retained = true;
127 c.publishValue(new StringType("UPDATE")).get();
128 verify(connection).publish(eq("command"), any(), anyInt(), eq(true));
131 verify(connection).unsubscribe(eq("state"), eq(c));
135 public void receiveWildcardTest() throws InterruptedException, ExecutionException, TimeoutException {
136 ChannelState c = spy(new ChannelState(ChannelConfigBuilder.create("state/+/topic", "command").build(),
137 channelUID, textValue, channelStateUpdateListener));
139 CompletableFuture<@Nullable Void> future = c.start(connection, scheduler, 100);
140 c.processMessage("state/bla/topic", "A TEST".getBytes());
141 future.get(300, TimeUnit.MILLISECONDS);
143 assertThat(textValue.getChannelState().toString(), is("A TEST"));
144 verify(channelStateUpdateListener).updateChannelState(eq(channelUID), any());
148 public void receiveStringTest() throws InterruptedException, ExecutionException, TimeoutException {
149 ChannelState c = spy(new ChannelState(config, channelUID, textValue, channelStateUpdateListener));
151 CompletableFuture<@Nullable Void> future = c.start(connection, scheduler, 100);
152 c.processMessage("state", "A TEST".getBytes());
153 future.get(300, TimeUnit.MILLISECONDS);
155 assertThat(textValue.getChannelState().toString(), is("A TEST"));
156 verify(channelStateUpdateListener).updateChannelState(eq(channelUID), any());
160 public void receiveDecimalTest() {
161 NumberValue value = new NumberValue(null, null, new BigDecimal(10), null);
162 ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
163 c.start(connection, mock(ScheduledExecutorService.class), 100);
165 c.processMessage("state", "15".getBytes());
166 assertThat(value.getChannelState().toString(), is("15"));
168 c.processMessage("state", "INCREASE".getBytes());
169 assertThat(value.getChannelState().toString(), is("25"));
171 c.processMessage("state", "DECREASE".getBytes());
172 assertThat(value.getChannelState().toString(), is("15"));
174 verify(channelStateUpdateListener, times(3)).updateChannelState(eq(channelUID), any());
178 public void receiveDecimalFractionalTest() {
179 NumberValue value = new NumberValue(null, null, new BigDecimal(10.5), null);
180 ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
181 c.start(connection, mock(ScheduledExecutorService.class), 100);
183 c.processMessage("state", "5.5".getBytes());
184 assertThat(value.getChannelState().toString(), is("5.5"));
186 c.processMessage("state", "INCREASE".getBytes());
187 assertThat(value.getChannelState().toString(), is("16.0"));
191 public void receivePercentageTest() {
192 PercentageValue value = new PercentageValue(new BigDecimal(-100), new BigDecimal(100), new BigDecimal(10), null,
194 ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
195 c.start(connection, mock(ScheduledExecutorService.class), 100);
197 c.processMessage("state", "-100".getBytes()); // 0%
198 assertThat(value.getChannelState().toString(), is("0"));
200 c.processMessage("state", "100".getBytes()); // 100%
201 assertThat(value.getChannelState().toString(), is("100"));
203 c.processMessage("state", "0".getBytes()); // 50%
204 assertThat(value.getChannelState().toString(), is("50"));
206 c.processMessage("state", "INCREASE".getBytes());
207 assertThat(value.getChannelState().toString(), is("55"));
208 assertThat(value.getMQTTpublishValue(null), is("10"));
209 assertThat(value.getMQTTpublishValue("%03.0f"), is("010"));
213 public void receiveRGBColorTest() {
214 ColorValue value = new ColorValue(ColorMode.RGB, "FON", "FOFF", 10);
215 ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
216 c.start(connection, mock(ScheduledExecutorService.class), 100);
218 c.processMessage("state", "ON".getBytes()); // Normal on state
219 assertThat(value.getChannelState().toString(), is("0,0,10"));
220 assertThat(value.getMQTTpublishValue(null), is("25,25,25"));
222 c.processMessage("state", "FOFF".getBytes()); // Custom off state
223 assertThat(value.getChannelState().toString(), is("0,0,0"));
224 assertThat(value.getMQTTpublishValue(null), is("0,0,0"));
226 c.processMessage("state", "10".getBytes()); // Brightness only
227 assertThat(value.getChannelState().toString(), is("0,0,10"));
228 assertThat(value.getMQTTpublishValue(null), is("25,25,25"));
230 HSBType t = HSBType.fromRGB(12, 18, 231);
232 c.processMessage("state", "12,18,231".getBytes());
233 assertThat(value.getChannelState(), is(t)); // HSB
234 // rgb -> hsv -> rgb is quite lossy
235 assertThat(value.getMQTTpublishValue(null), is("13,20,225"));
236 assertThat(value.getMQTTpublishValue("%3$d,%2$d,%1$d"), is("225,20,13"));
240 public void receiveHSBColorTest() {
241 ColorValue value = new ColorValue(ColorMode.HSB, "FON", "FOFF", 10);
242 ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
243 c.start(connection, mock(ScheduledExecutorService.class), 100);
245 c.processMessage("state", "ON".getBytes()); // Normal on state
246 assertThat(value.getChannelState().toString(), is("0,0,10"));
247 assertThat(value.getMQTTpublishValue(null), is("0,0,10"));
249 c.processMessage("state", "FOFF".getBytes()); // Custom off state
250 assertThat(value.getChannelState().toString(), is("0,0,0"));
251 assertThat(value.getMQTTpublishValue(null), is("0,0,0"));
253 c.processMessage("state", "10".getBytes()); // Brightness only
254 assertThat(value.getChannelState().toString(), is("0,0,10"));
255 assertThat(value.getMQTTpublishValue(null), is("0,0,10"));
257 c.processMessage("state", "12,18,100".getBytes());
258 assertThat(value.getChannelState().toString(), is("12,18,100"));
259 assertThat(value.getMQTTpublishValue(null), is("12,18,100"));
263 public void receiveXYYColorTest() {
264 ColorValue value = new ColorValue(ColorMode.XYY, "FON", "FOFF", 10);
265 ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
266 c.start(connection, mock(ScheduledExecutorService.class), 100);
268 c.processMessage("state", "ON".getBytes()); // Normal on state
269 assertThat(value.getChannelState().toString(), is("0,0,10"));
270 assertThat(value.getMQTTpublishValue(null), is("0.312716,0.329002,10.00"));
272 c.processMessage("state", "FOFF".getBytes()); // Custom off state
273 assertThat(value.getChannelState().toString(), is("0,0,0"));
274 assertThat(value.getMQTTpublishValue(null), is("0.312716,0.329002,0.00"));
276 c.processMessage("state", "10".getBytes()); // Brightness only
277 assertThat(value.getChannelState().toString(), is("0,0,10"));
278 assertThat(value.getMQTTpublishValue(null), is("0.312716,0.329002,10.00"));
280 HSBType t = HSBType.fromXY(0.3f, 0.6f);
282 c.processMessage("state", "0.3,0.6,100".getBytes());
283 assertThat(value.getChannelState(), is(t)); // HSB
284 assertThat(value.getMQTTpublishValue(null), is("0.300000,0.600000,100.00"));
285 assertThat(value.getMQTTpublishValue("%3$.1f,%2$.4f,%1$.4f"), is("100.0,0.6000,0.3000"));
289 public void receiveLocationTest() {
290 LocationValue value = new LocationValue();
291 ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
292 c.start(connection, mock(ScheduledExecutorService.class), 100);
294 c.processMessage("state", "46.833974, 7.108433".getBytes());
295 assertThat(value.getChannelState().toString(), is("46.833974,7.108433"));
296 assertThat(value.getMQTTpublishValue(null), is("46.833974,7.108433"));
300 public void receiveDateTimeTest() {
301 DateTimeValue value = new DateTimeValue();
302 ChannelState subject = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
303 subject.start(connection, mock(ScheduledExecutorService.class), 100);
305 ZonedDateTime zd = ZonedDateTime.now();
306 String datetime = zd.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
308 subject.processMessage("state", datetime.getBytes());
310 String channelState = value.getChannelState().toString();
311 assertTrue("Expected '" + channelState + "' to start with '" + datetime + "'",
312 channelState.startsWith(datetime));
313 assertThat(value.getMQTTpublishValue(null), is(datetime));
317 public void receiveImageTest() {
318 ImageValue value = new ImageValue();
319 ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
320 c.start(connection, mock(ScheduledExecutorService.class), 100);
322 byte[] payload = new byte[] { (byte) 0xFF, (byte) 0xD8, 0x01, 0x02, (byte) 0xFF, (byte) 0xD9 };
323 c.processMessage("state", payload);
324 assertThat(value.getChannelState(), is(instanceOf(RawType.class)));
325 assertThat(((RawType) value.getChannelState()).getMimeType(), is("image/jpeg"));