2 * Copyright (c) 2010-2024 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.values;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
19 import java.math.BigDecimal;
20 import java.util.Objects;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.mqtt.generic.mapping.ColorMode;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.HSBType;
27 import org.openhab.core.library.types.IncreaseDecreaseType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.OpenClosedType;
30 import org.openhab.core.library.types.PercentType;
31 import org.openhab.core.library.types.QuantityType;
32 import org.openhab.core.library.types.StopMoveType;
33 import org.openhab.core.library.types.StringType;
34 import org.openhab.core.library.types.UpDownType;
35 import org.openhab.core.library.unit.MetricPrefix;
36 import org.openhab.core.library.unit.Units;
37 import org.openhab.core.types.Command;
38 import org.openhab.core.types.State;
39 import org.openhab.core.types.TypeParser;
40 import org.openhab.core.types.UnDefType;
43 * Test cases for the value classes. They should throw exceptions if the wrong command type is used
44 * for an update. The percent value class should raise an exception if the value is out of range.
46 * The on/off value class should accept a multitude of values including the custom defined ones.
48 * The string value class states are tested.
50 * @author David Graeff - Initial contribution
53 public class ValueTests {
54 private Command p(Value v, String str) {
55 return Objects.requireNonNull(TypeParser.parseCommand(v.getSupportedCommandTypes(), str));
59 public void illegalTextStateUpdate() {
60 TextValue v = new TextValue("one,two".split(","));
61 assertThrows(IllegalArgumentException.class, () -> v.parseCommand(p(v, "three")));
65 public void textStateUpdate() {
66 TextValue v = new TextValue("one,two".split(","));
67 v.parseCommand(p(v, "one"));
71 public void colorUpdate() {
72 ColorValue v = new ColorValue(ColorMode.RGB, "fancyON", "fancyOFF", 77);
73 v.update((State) v.parseCommand(p(v, "255,255,255")));
75 HSBType hsb = (HSBType) v.parseCommand(p(v, "OFF"));
76 assertThat(hsb.getBrightness().intValue(), is(0));
78 hsb = (HSBType) v.parseCommand(p(v, "ON"));
79 assertThat(hsb.getBrightness().intValue(), is(77));
81 hsb = (HSBType) v.parseCommand(p(v, "0"));
82 assertThat(hsb.getBrightness().intValue(), is(0));
83 hsb = (HSBType) v.parseCommand(p(v, "1"));
84 assertThat(hsb.getBrightness().intValue(), is(1));
88 public void illegalColorUpdate() {
89 ColorValue v = new ColorValue(ColorMode.RGB, null, null, 10);
90 assertThrows(IllegalArgumentException.class, () -> v.parseCommand(p(v, "255,255,abc")));
94 public void illegalNumberCommand() {
95 NumberValue v = new NumberValue(null, null, null, null);
96 assertThrows(IllegalArgumentException.class, () -> v.parseCommand(OnOffType.OFF));
100 public void illegalPercentCommand() {
101 PercentageValue v = new PercentageValue(null, null, null, null, null);
102 assertThrows(IllegalStateException.class, () -> v.parseCommand(new StringType("demo")));
106 public void illegalOnOffCommand() {
107 OnOffValue v = new OnOffValue(null, null);
108 assertThrows(IllegalArgumentException.class, () -> v.parseCommand(new DecimalType(101.0)));
112 public void illegalPercentUpdate() {
113 PercentageValue v = new PercentageValue(null, null, null, null, null);
114 assertThrows(IllegalArgumentException.class, () -> v.parseCommand(new DecimalType(101.0)));
118 public void onoffUpdate() {
119 OnOffValue v = new OnOffValue("fancyON", "fancyOff");
122 assertThat(v.parseCommand(OnOffType.OFF), is(OnOffType.OFF));
123 assertThat(v.parseCommand(OnOffType.ON), is(OnOffType.ON));
125 // Test with string, representing the command
126 assertThat(v.parseCommand(new StringType("OFF")), is(OnOffType.OFF));
127 assertThat(v.parseCommand(new StringType("ON")), is(OnOffType.ON));
129 // Test with custom string, setup in the constructor
130 assertThat(v.parseCommand(new StringType("fancyOff")), is(OnOffType.OFF));
131 assertThat(v.parseCommand(new StringType("fancyON")), is(OnOffType.ON));
133 // Test basic formatting
134 assertThat(v.getMQTTpublishValue(OnOffType.ON, null), is("fancyON"));
135 assertThat(v.getMQTTpublishValue(OnOffType.OFF, null), is("fancyOff"));
137 // Test custom formatting
138 assertThat(v.getMQTTpublishValue(OnOffType.OFF, "=%s"), is("=fancyOff"));
139 assertThat(v.getMQTTpublishValue(OnOffType.ON, "=%s"), is("=fancyON"));
143 public void openCloseUpdate() {
144 OpenCloseValue v = new OpenCloseValue("fancyON", "fancyOff");
147 assertThat(v.parseCommand(OpenClosedType.CLOSED), is(OpenClosedType.CLOSED));
148 assertThat(v.parseCommand(OpenClosedType.OPEN), is(OpenClosedType.OPEN));
150 // Test with string, representing the command
151 assertThat(v.parseCommand(new StringType("CLOSED")), is(OpenClosedType.CLOSED));
152 assertThat(v.parseCommand(new StringType("OPEN")), is(OpenClosedType.OPEN));
154 // Test with custom string, setup in the constructor
155 assertThat(v.parseCommand(new StringType("fancyOff")), is(OpenClosedType.CLOSED));
156 assertThat(v.parseCommand(new StringType("fancyON")), is(OpenClosedType.OPEN));
158 // Test basic formatting
159 assertThat(v.getMQTTpublishValue(OpenClosedType.CLOSED, null), is("fancyOff"));
160 assertThat(v.getMQTTpublishValue(OpenClosedType.OPEN, null), is("fancyON"));
164 public void numberUpdate() {
165 NumberValue v = new NumberValue(null, null, new BigDecimal(10), Units.WATT);
167 // Test with command with units
168 Command command = v.parseCommand(new QuantityType<>(20, Units.WATT));
169 assertThat(command, is(new QuantityType<>(20, Units.WATT)));
170 assertThat(v.getMQTTpublishValue(command, null), is("20"));
171 command = v.parseCommand(new QuantityType<>(20, MetricPrefix.KILO(Units.WATT)));
172 assertThat(command, is(new QuantityType<>(20, MetricPrefix.KILO(Units.WATT))));
173 assertThat(v.getMQTTpublishValue(command, null), is("20000"));
175 // Test with command without units
176 command = v.parseCommand(new QuantityType<>("20"));
177 assertThat(command, is(new QuantityType<>(20, Units.WATT)));
178 assertThat(v.getMQTTpublishValue(command, null), is("20"));
180 assertThat(v.parseMessage(new StringType("NaN")), is(UnDefType.UNDEF));
181 assertThat(v.parseMessage(new StringType("nan")), is(UnDefType.UNDEF));
185 public void numberUpdateMireds() {
186 NumberValue v = new NumberValue(null, null, new BigDecimal(10), Units.MIRED);
188 Command command = v.parseCommand(new QuantityType<>(2700, Units.KELVIN));
189 assertThat(v.getMQTTpublishValue(command, "%.0f"), is("370"));
193 public void numberPercentageUpdate() {
194 NumberValue v = new NumberValue(null, null, new BigDecimal(10), Units.PERCENT);
196 // Test with command with units
197 Command command = v.parseCommand(new QuantityType<>(20, Units.PERCENT));
198 assertThat(command, is(new QuantityType<>(20, Units.PERCENT)));
199 assertThat(v.getMQTTpublishValue(command, null), is("20"));
201 // Test with command without units
202 command = v.parseCommand(new QuantityType<>("20"));
203 assertThat(command, is(new QuantityType<>(20, Units.PERCENT)));
204 assertThat(v.getMQTTpublishValue(command, null), is("20"));
208 public void rollershutterUpdateWithStrings() {
209 RollershutterValue v = new RollershutterValue("fancyON", "fancyOff", "fancyStop");
210 // Test with UP/DOWN/STOP command
211 assertThat(v.parseCommand(UpDownType.UP), is(UpDownType.UP));
212 assertThat(v.getMQTTpublishValue(UpDownType.UP, null), is("fancyON"));
213 assertThat(v.parseCommand(UpDownType.DOWN), is(UpDownType.DOWN));
214 assertThat(v.getMQTTpublishValue(UpDownType.DOWN, null), is("fancyOff"));
215 assertThat(v.parseCommand(StopMoveType.STOP), is(StopMoveType.STOP));
216 assertThat(v.getMQTTpublishValue(StopMoveType.STOP, null), is("fancyStop"));
218 // Test with custom string
219 assertThat(v.parseCommand(new StringType("fancyON")), is(UpDownType.UP));
220 assertThat(v.parseCommand(new StringType("fancyOff")), is(UpDownType.DOWN));
222 // Test with exact percent
223 Command command = new PercentType(27);
224 assertThat(v.parseCommand((Command) command), is(command));
225 assertThat(v.getMQTTpublishValue(command, null), is("27"));
227 // Test formatting 0/100
228 assertThat(v.getMQTTpublishValue(PercentType.ZERO, null), is("fancyON"));
229 assertThat(v.getMQTTpublishValue(PercentType.HUNDRED, null), is("fancyOff"));
231 // Test parsing from MQTT
232 assertThat(v.parseMessage(new StringType("fancyON")), is(UpDownType.UP));
233 assertThat(v.parseMessage(new StringType("fancyOff")), is(UpDownType.DOWN));
237 public void rollershutterUpdateWithDiscreteCommandAndStateStrings() {
238 RollershutterValue v = new RollershutterValue("OPEN", "CLOSE", "STOP", "open", "closed", false, true);
239 // Test with UP/DOWN/STOP command
240 assertThat(v.parseCommand(UpDownType.UP), is(UpDownType.UP));
241 assertThat(v.getMQTTpublishValue(UpDownType.UP, null), is("OPEN"));
242 assertThat(v.parseCommand(UpDownType.DOWN), is(UpDownType.DOWN));
243 assertThat(v.getMQTTpublishValue(UpDownType.DOWN, null), is("CLOSE"));
244 assertThat(v.parseCommand(StopMoveType.STOP), is(StopMoveType.STOP));
245 assertThat(v.getMQTTpublishValue(StopMoveType.STOP, null), is("STOP"));
247 // Test with custom string
248 assertThat(v.parseCommand(new StringType("OPEN")), is(UpDownType.UP));
249 assertThat(v.parseCommand(new StringType("CLOSE")), is(UpDownType.DOWN));
251 // Test with exact percent
252 Command command = new PercentType(27);
253 assertThat(v.parseCommand((Command) command), is(command));
254 assertThat(v.getMQTTpublishValue(command, null), is("27"));
256 // Test formatting 0/100
257 assertThat(v.getMQTTpublishValue(PercentType.ZERO, null), is("OPEN"));
258 assertThat(v.getMQTTpublishValue(PercentType.HUNDRED, null), is("CLOSE"));
260 // Test parsing from MQTT
261 assertThat(v.parseMessage(new StringType("open")), is(UpDownType.UP));
262 assertThat(v.parseMessage(new StringType("closed")), is(UpDownType.DOWN));
266 public void rollershutterUpdateWithOutStrings() {
267 RollershutterValue v = new RollershutterValue(null, null, "fancyStop");
269 assertThat(v.parseCommand(UpDownType.UP), is(PercentType.ZERO));
270 assertThat(v.parseCommand(UpDownType.DOWN), is(PercentType.HUNDRED));
272 // Test with custom string
273 // Test formatting 0/100
274 assertThat(v.getMQTTpublishValue(PercentType.ZERO, null), is("0"));
275 assertThat(v.getMQTTpublishValue(PercentType.HUNDRED, null), is("100"));
279 public void percentCalc() {
280 PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0), null,
282 assertThat(v.parseCommand(new DecimalType("110.0")), is(PercentType.HUNDRED));
283 assertThat(v.getMQTTpublishValue(PercentType.HUNDRED, null), is("110"));
284 assertThat(v.parseCommand(new DecimalType(10.0)), is(PercentType.ZERO));
285 assertThat(v.getMQTTpublishValue(PercentType.ZERO, null), is("10"));
287 assertThat(v.parseCommand(OnOffType.ON), is(PercentType.HUNDRED));
288 assertThat(v.parseCommand(OnOffType.OFF), is(PercentType.ZERO));
292 public void percentMQTTValue() {
293 PercentageValue v = new PercentageValue(null, null, null, null, null);
294 assertThat(v.parseCommand(new DecimalType("10.10000")), is(new PercentType("10.1")));
295 assertThat(v.getMQTTpublishValue(new PercentType("10.1"), null), is("10.1"));
297 for (int i = 0; i <= 100; i++) {
298 command = v.parseCommand(new DecimalType(i));
299 assertThat(v.getMQTTpublishValue(command, null), is("" + i));
304 public void percentCustomOnOff() {
305 PercentageValue v = new PercentageValue(new BigDecimal("0.0"), new BigDecimal("100.0"), new BigDecimal("1.0"),
307 assertThat(v.parseCommand(new StringType("on")), is(PercentType.HUNDRED));
308 assertThat(v.parseCommand(new StringType("off")), is(PercentType.ZERO));
312 public void decimalCalc() {
313 PercentageValue v = new PercentageValue(new BigDecimal("0.1"), new BigDecimal("1.0"), new BigDecimal("0.1"),
315 assertThat(v.parseCommand(new DecimalType(1.0)), is(PercentType.HUNDRED));
316 assertThat(v.parseCommand(new DecimalType(0.1)), is(PercentType.ZERO));
317 PercentType command = (PercentType) v.parseCommand(new DecimalType(0.2));
318 assertEquals(command.floatValue(), 11.11f, 0.01f);
322 public void increaseDecreaseCalc() {
323 PercentageValue v = new PercentageValue(new BigDecimal("1.0"), new BigDecimal("11.0"), new BigDecimal("0.5"),
327 PercentType command = (PercentType) v.parseCommand(new DecimalType("6.0"));
328 assertEquals(command.floatValue(), 50.0f, 0.01f);
330 command = (PercentType) v.parseCommand(IncreaseDecreaseType.INCREASE);
331 assertEquals(command.floatValue(), 55.0f, 0.01f);
332 command = (PercentType) v.parseCommand(IncreaseDecreaseType.DECREASE);
333 assertEquals(command.floatValue(), 45.0f, 0.01f);
336 command = (PercentType) v.parseCommand(new DecimalType("1.1"));
337 assertEquals(command.floatValue(), 1.0f, 0.01f);
339 command = (PercentType) v.parseCommand(IncreaseDecreaseType.DECREASE);
340 assertEquals(command.floatValue(), 0.0f, 0.01f);
343 command = (PercentType) v.parseCommand(new DecimalType("10.8"));
344 assertEquals(command.floatValue(), 98.0f, 0.01f);
346 command = (PercentType) v.parseCommand(IncreaseDecreaseType.INCREASE);
347 assertEquals(command.floatValue(), 100.0f, 0.01f);
351 public void upDownCalc() {
352 PercentageValue v = new PercentageValue(new BigDecimal("1.0"), new BigDecimal("11.0"), new BigDecimal("0.5"),
356 PercentType command = (PercentType) v.parseCommand(new DecimalType("6.0"));
357 assertEquals(command.floatValue(), 50.0f, 0.01f);
359 command = (PercentType) v.parseCommand(UpDownType.UP);
360 assertEquals(command.floatValue(), 55.0f, 0.01f);
361 command = (PercentType) v.parseCommand(UpDownType.DOWN);
362 assertEquals(command.floatValue(), 45.0f, 0.01f);
365 command = (PercentType) v.parseCommand(new DecimalType("1.1"));
366 assertEquals(command.floatValue(), 1.0f, 0.01f);
368 command = (PercentType) v.parseCommand(UpDownType.DOWN);
369 assertEquals(command.floatValue(), 0.0f, 0.01f);
372 command = (PercentType) v.parseCommand(new DecimalType("10.8"));
373 assertEquals(command.floatValue(), 98.0f, 0.01f);
375 command = (PercentType) v.parseCommand(UpDownType.UP);
376 assertEquals(command.floatValue(), 100.0f, 0.01f);
380 public void percentCalcInvalid() {
381 PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0), null,
383 assertThrows(IllegalArgumentException.class, () -> v.parseCommand(new DecimalType(9.0)));