2 * Copyright (c) 2010-2022 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;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.mqtt.generic.mapping.ColorMode;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.HSBType;
25 import org.openhab.core.library.types.IncreaseDecreaseType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.OpenClosedType;
28 import org.openhab.core.library.types.PercentType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.library.types.UpDownType;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.TypeParser;
35 * Test cases for the value classes. They should throw exceptions if the wrong command type is used
36 * for an update. The percent value class should raise an exception if the value is out of range.
38 * The on/off value class should accept a multitude of values including the custom defined ones.
40 * The string value class states are tested.
42 * @author David Graeff - Initial contribution
44 public class ValueTests {
45 Command p(Value v, String str) {
46 return TypeParser.parseCommand(v.getSupportedCommandTypes(), str);
50 public void illegalTextStateUpdate() {
51 TextValue v = new TextValue("one,two".split(","));
52 assertThrows(IllegalArgumentException.class, () -> v.update(p(v, "three")));
55 public void textStateUpdate() {
56 TextValue v = new TextValue("one,two".split(","));
57 v.update(p(v, "one"));
60 public void colorUpdate() {
61 ColorValue v = new ColorValue(ColorMode.RGB, "fancyON", "fancyOFF", 77);
62 v.update(p(v, "255, 255, 255"));
64 v.update(p(v, "OFF"));
65 assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(0));
67 assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(77));
70 assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(0));
72 assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(1));
76 public void illegalColorUpdate() {
77 ColorValue v = new ColorValue(ColorMode.RGB, null, null, 10);
78 assertThrows(IllegalArgumentException.class, () -> v.update(p(v, "255,255,abc")));
82 public void illegalNumberCommand() {
83 NumberValue v = new NumberValue(null, null, null, null);
84 assertThrows(IllegalArgumentException.class, () -> v.update(OnOffType.OFF));
88 public void illegalPercentCommand() {
89 PercentageValue v = new PercentageValue(null, null, null, null, null);
90 assertThrows(IllegalStateException.class, () -> v.update(new StringType("demo")));
94 public void illegalOnOffCommand() {
95 OnOffValue v = new OnOffValue(null, null);
96 assertThrows(IllegalArgumentException.class, () -> v.update(new DecimalType(101.0)));
100 public void illegalPercentUpdate() {
101 PercentageValue v = new PercentageValue(null, null, null, null, null);
102 assertThrows(IllegalArgumentException.class, () -> v.update(new DecimalType(101.0)));
106 public void onoffUpdate() {
107 OnOffValue v = new OnOffValue("fancyON", "fancyOff");
109 v.update(OnOffType.OFF);
110 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
111 assertThat(v.getChannelState(), is(OnOffType.OFF));
112 v.update(OnOffType.ON);
113 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
114 assertThat(v.getChannelState(), is(OnOffType.ON));
116 // Test with string, representing the command
117 v.update(new StringType("OFF"));
118 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
119 assertThat(v.getChannelState(), is(OnOffType.OFF));
120 v.update(new StringType("ON"));
121 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
122 assertThat(v.getChannelState(), is(OnOffType.ON));
124 // Test with custom string, setup in the constructor
125 v.update(new StringType("fancyOff"));
126 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
127 assertThat(v.getMQTTpublishValue("=%s"), is("=fancyOff"));
128 assertThat(v.getChannelState(), is(OnOffType.OFF));
129 v.update(new StringType("fancyON"));
130 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
131 assertThat(v.getMQTTpublishValue("=%s"), is("=fancyON"));
132 assertThat(v.getChannelState(), is(OnOffType.ON));
136 public void openCloseUpdate() {
137 OpenCloseValue v = new OpenCloseValue("fancyON", "fancyOff");
139 v.update(OpenClosedType.CLOSED);
140 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
141 assertThat(v.getChannelState(), is(OpenClosedType.CLOSED));
142 v.update(OpenClosedType.OPEN);
143 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
144 assertThat(v.getChannelState(), is(OpenClosedType.OPEN));
146 // Test with string, representing the command
147 v.update(new StringType("CLOSED"));
148 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
149 assertThat(v.getChannelState(), is(OpenClosedType.CLOSED));
150 v.update(new StringType("OPEN"));
151 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
152 assertThat(v.getChannelState(), is(OpenClosedType.OPEN));
154 // Test with custom string, setup in the constructor
155 v.update(new StringType("fancyOff"));
156 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
157 assertThat(v.getChannelState(), is(OpenClosedType.CLOSED));
158 v.update(new StringType("fancyON"));
159 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
160 assertThat(v.getChannelState(), is(OpenClosedType.OPEN));
164 public void rollershutterUpdateWithStrings() {
165 RollershutterValue v = new RollershutterValue("fancyON", "fancyOff", "fancyStop");
167 v.update(UpDownType.UP);
168 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
169 assertThat(v.getChannelState(), is(PercentType.ZERO));
170 v.update(UpDownType.DOWN);
171 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
172 assertThat(v.getChannelState(), is(PercentType.HUNDRED));
174 // Test with custom string
175 v.update(new StringType("fancyON"));
176 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
177 assertThat(v.getChannelState(), is(PercentType.ZERO));
178 v.update(new StringType("fancyOff"));
179 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
180 assertThat(v.getChannelState(), is(PercentType.HUNDRED));
181 v.update(new PercentType(27));
182 assertThat(v.getMQTTpublishValue(null), is("27"));
183 assertThat(v.getChannelState(), is(new PercentType(27)));
187 public void rollershutterUpdateWithOutStrings() {
188 RollershutterValue v = new RollershutterValue(null, null, "fancyStop");
190 v.update(UpDownType.UP);
191 assertThat(v.getMQTTpublishValue(null), is("0"));
192 assertThat(v.getChannelState(), is(PercentType.ZERO));
193 v.update(UpDownType.DOWN);
194 assertThat(v.getMQTTpublishValue(null), is("100"));
195 assertThat(v.getChannelState(), is(PercentType.HUNDRED));
197 // Test with custom string
198 v.update(PercentType.ZERO);
199 assertThat(v.getMQTTpublishValue(null), is("0"));
200 assertThat(v.getChannelState(), is(PercentType.ZERO));
201 v.update(PercentType.HUNDRED);
202 assertThat(v.getMQTTpublishValue(null), is("100"));
203 assertThat(v.getChannelState(), is(PercentType.HUNDRED));
204 v.update(new PercentType(27));
205 assertThat(v.getMQTTpublishValue(null), is("27"));
206 assertThat(v.getChannelState(), is(new PercentType(27)));
210 public void percentCalc() {
211 PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0), null,
213 v.update(new DecimalType("110.0"));
214 assertThat((PercentType) v.getChannelState(), is(new PercentType(100)));
215 assertThat(v.getMQTTpublishValue(null), is("110"));
216 v.update(new DecimalType(10.0));
217 assertThat((PercentType) v.getChannelState(), is(new PercentType(0)));
218 assertThat(v.getMQTTpublishValue(null), is("10"));
220 v.update(OnOffType.ON);
221 assertThat((PercentType) v.getChannelState(), is(new PercentType(100)));
222 v.update(OnOffType.OFF);
223 assertThat((PercentType) v.getChannelState(), is(new PercentType(0)));
227 public void percentMQTTValue() {
228 PercentageValue v = new PercentageValue(null, null, null, null, null);
229 v.update(new DecimalType("10.10000"));
230 assertThat(v.getMQTTpublishValue(null), is("10.1"));
231 for (int i = 0; i <= 100; i++) {
232 v.update(new DecimalType(i));
233 assertThat(v.getMQTTpublishValue(null), is("" + i));
238 public void percentCustomOnOff() {
239 PercentageValue v = new PercentageValue(new BigDecimal("0.0"), new BigDecimal("100.0"), new BigDecimal("1.0"),
241 v.update(new StringType("on"));
242 assertThat((PercentType) v.getChannelState(), is(new PercentType(100)));
243 v.update(new StringType("off"));
244 assertThat((PercentType) v.getChannelState(), is(new PercentType(0)));
248 public void decimalCalc() {
249 PercentageValue v = new PercentageValue(new BigDecimal("0.1"), new BigDecimal("1.0"), new BigDecimal("0.1"),
251 v.update(new DecimalType(1.0));
252 assertThat((PercentType) v.getChannelState(), is(new PercentType(100)));
253 v.update(new DecimalType(0.1));
254 assertThat((PercentType) v.getChannelState(), is(new PercentType(0)));
255 v.update(new DecimalType(0.2));
256 assertEquals(((PercentType) v.getChannelState()).floatValue(), 11.11f, 0.01f);
260 public void increaseDecreaseCalc() {
261 PercentageValue v = new PercentageValue(new BigDecimal("1.0"), new BigDecimal("11.0"), new BigDecimal("0.5"),
265 v.update(new DecimalType("6.0"));
266 assertEquals(((PercentType) v.getChannelState()).floatValue(), 50.0f, 0.01f);
267 v.update(IncreaseDecreaseType.INCREASE);
268 assertEquals(((PercentType) v.getChannelState()).floatValue(), 55.0f, 0.01f);
269 v.update(IncreaseDecreaseType.DECREASE);
270 v.update(IncreaseDecreaseType.DECREASE);
271 assertEquals(((PercentType) v.getChannelState()).floatValue(), 45.0f, 0.01f);
274 v.update(new DecimalType("1.1"));
275 assertEquals(((PercentType) v.getChannelState()).floatValue(), 1.0f, 0.01f);
276 v.update(IncreaseDecreaseType.DECREASE);
277 assertEquals(((PercentType) v.getChannelState()).floatValue(), 0.0f, 0.01f);
280 v.update(new DecimalType("10.8"));
281 assertEquals(((PercentType) v.getChannelState()).floatValue(), 98.0f, 0.01f);
282 v.update(IncreaseDecreaseType.INCREASE);
283 assertEquals(((PercentType) v.getChannelState()).floatValue(), 100.0f, 0.01f);
287 public void upDownCalc() {
288 PercentageValue v = new PercentageValue(new BigDecimal("1.0"), new BigDecimal("11.0"), new BigDecimal("0.5"),
292 v.update(new DecimalType("6.0"));
293 assertEquals(((PercentType) v.getChannelState()).floatValue(), 50.0f, 0.01f);
294 v.update(UpDownType.UP);
295 assertEquals(((PercentType) v.getChannelState()).floatValue(), 55.0f, 0.01f);
296 v.update(UpDownType.DOWN);
297 v.update(UpDownType.DOWN);
298 assertEquals(((PercentType) v.getChannelState()).floatValue(), 45.0f, 0.01f);
301 v.update(new DecimalType("1.1"));
302 assertEquals(((PercentType) v.getChannelState()).floatValue(), 1.0f, 0.01f);
303 v.update(UpDownType.DOWN);
304 assertEquals(((PercentType) v.getChannelState()).floatValue(), 0.0f, 0.01f);
307 v.update(new DecimalType("10.8"));
308 assertEquals(((PercentType) v.getChannelState()).floatValue(), 98.0f, 0.01f);
309 v.update(UpDownType.UP);
310 assertEquals(((PercentType) v.getChannelState()).floatValue(), 100.0f, 0.01f);
314 public void percentCalcInvalid() {
315 PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0), null,
317 assertThrows(IllegalArgumentException.class, () -> v.update(new DecimalType(9.0)));