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.values;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.junit.Assert.*;
18 import java.math.BigDecimal;
20 import org.junit.Test;
21 import org.openhab.binding.mqtt.generic.mapping.ColorMode;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.HSBType;
24 import org.openhab.core.library.types.IncreaseDecreaseType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.OpenClosedType;
27 import org.openhab.core.library.types.PercentType;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.library.types.UpDownType;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.TypeParser;
34 * Test cases for the value classes. They should throw exceptions if the wrong command type is used
35 * for an update. The percent value class should raise an exception if the value is out of range.
37 * The on/off value class should accept a multitude of values including the custom defined ones.
39 * The string value class states are tested.
41 * @author David Graeff - Initial contribution
43 public class ValueTests {
44 Command p(Value v, String str) {
45 return TypeParser.parseCommand(v.getSupportedCommandTypes(), str);
48 @Test(expected = IllegalArgumentException.class)
49 public void illegalTextStateUpdate() {
50 TextValue v = new TextValue("one,two".split(","));
51 v.update(p(v, "three"));
54 public void textStateUpdate() {
55 TextValue v = new TextValue("one,two".split(","));
56 v.update(p(v, "one"));
59 public void colorUpdate() {
60 ColorValue v = new ColorValue(ColorMode.RGB, "fancyON", "fancyOFF", 77);
61 v.update(p(v, "255, 255, 255"));
63 v.update(p(v, "OFF"));
64 assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(0));
66 assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(77));
69 assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(0));
71 assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(1));
74 @Test(expected = IllegalArgumentException.class)
75 public void illegalColorUpdate() {
76 ColorValue v = new ColorValue(ColorMode.RGB, null, null, 10);
77 v.update(p(v, "255,255,abc"));
80 @Test(expected = IllegalArgumentException.class)
81 public void illegalNumberCommand() {
82 NumberValue v = new NumberValue(null, null, null, null);
83 v.update(OnOffType.OFF);
86 @Test(expected = IllegalStateException.class)
87 public void illegalPercentCommand() {
88 PercentageValue v = new PercentageValue(null, null, null, null, null);
89 v.update(new StringType("demo"));
92 @Test(expected = IllegalArgumentException.class)
93 public void illegalOnOffCommand() {
94 OnOffValue v = new OnOffValue(null, null);
95 v.update(new DecimalType(101.0));
98 @Test(expected = IllegalArgumentException.class)
99 public void illegalPercentUpdate() {
100 PercentageValue v = new PercentageValue(null, null, null, null, null);
101 v.update(new DecimalType(101.0));
105 public void onoffUpdate() {
106 OnOffValue v = new OnOffValue("fancyON", "fancyOff");
108 v.update(OnOffType.OFF);
109 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
110 assertThat(v.getChannelState(), is(OnOffType.OFF));
111 v.update(OnOffType.ON);
112 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
113 assertThat(v.getChannelState(), is(OnOffType.ON));
115 // Test with string, representing the command
116 v.update(new StringType("OFF"));
117 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
118 assertThat(v.getChannelState(), is(OnOffType.OFF));
119 v.update(new StringType("ON"));
120 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
121 assertThat(v.getChannelState(), is(OnOffType.ON));
123 // Test with custom string, setup in the constructor
124 v.update(new StringType("fancyOff"));
125 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
126 assertThat(v.getMQTTpublishValue("=%s"), is("=fancyOff"));
127 assertThat(v.getChannelState(), is(OnOffType.OFF));
128 v.update(new StringType("fancyON"));
129 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
130 assertThat(v.getMQTTpublishValue("=%s"), is("=fancyON"));
131 assertThat(v.getChannelState(), is(OnOffType.ON));
135 public void openCloseUpdate() {
136 OpenCloseValue v = new OpenCloseValue("fancyON", "fancyOff");
138 v.update(OpenClosedType.CLOSED);
139 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
140 assertThat(v.getChannelState(), is(OpenClosedType.CLOSED));
141 v.update(OpenClosedType.OPEN);
142 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
143 assertThat(v.getChannelState(), is(OpenClosedType.OPEN));
145 // Test with string, representing the command
146 v.update(new StringType("CLOSED"));
147 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
148 assertThat(v.getChannelState(), is(OpenClosedType.CLOSED));
149 v.update(new StringType("OPEN"));
150 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
151 assertThat(v.getChannelState(), is(OpenClosedType.OPEN));
153 // Test with custom string, setup in the constructor
154 v.update(new StringType("fancyOff"));
155 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
156 assertThat(v.getChannelState(), is(OpenClosedType.CLOSED));
157 v.update(new StringType("fancyON"));
158 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
159 assertThat(v.getChannelState(), is(OpenClosedType.OPEN));
163 public void rollershutterUpdateWithStrings() {
164 RollershutterValue v = new RollershutterValue("fancyON", "fancyOff", "fancyStop");
166 v.update(UpDownType.UP);
167 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
168 assertThat(v.getChannelState(), is(PercentType.ZERO));
169 v.update(UpDownType.DOWN);
170 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
171 assertThat(v.getChannelState(), is(PercentType.HUNDRED));
173 // Test with custom string
174 v.update(new StringType("fancyON"));
175 assertThat(v.getMQTTpublishValue(null), is("fancyON"));
176 assertThat(v.getChannelState(), is(PercentType.ZERO));
177 v.update(new StringType("fancyOff"));
178 assertThat(v.getMQTTpublishValue(null), is("fancyOff"));
179 assertThat(v.getChannelState(), is(PercentType.HUNDRED));
180 v.update(new PercentType(27));
181 assertThat(v.getMQTTpublishValue(null), is("27"));
182 assertThat(v.getChannelState(), is(new PercentType(27)));
186 public void rollershutterUpdateWithOutStrings() {
187 RollershutterValue v = new RollershutterValue(null, null, "fancyStop");
189 v.update(UpDownType.UP);
190 assertThat(v.getMQTTpublishValue(null), is("0"));
191 assertThat(v.getChannelState(), is(PercentType.ZERO));
192 v.update(UpDownType.DOWN);
193 assertThat(v.getMQTTpublishValue(null), is("100"));
194 assertThat(v.getChannelState(), is(PercentType.HUNDRED));
196 // Test with custom string
197 v.update(PercentType.ZERO);
198 assertThat(v.getMQTTpublishValue(null), is("0"));
199 assertThat(v.getChannelState(), is(PercentType.ZERO));
200 v.update(PercentType.HUNDRED);
201 assertThat(v.getMQTTpublishValue(null), is("100"));
202 assertThat(v.getChannelState(), is(PercentType.HUNDRED));
203 v.update(new PercentType(27));
204 assertThat(v.getMQTTpublishValue(null), is("27"));
205 assertThat(v.getChannelState(), is(new PercentType(27)));
209 public void percentCalc() {
210 PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0), null,
212 v.update(new DecimalType("110.0"));
213 assertThat((PercentType) v.getChannelState(), is(new PercentType(100)));
214 assertThat(v.getMQTTpublishValue(null), is("110"));
215 v.update(new DecimalType(10.0));
216 assertThat((PercentType) v.getChannelState(), is(new PercentType(0)));
217 assertThat(v.getMQTTpublishValue(null), is("10"));
219 v.update(OnOffType.ON);
220 assertThat((PercentType) v.getChannelState(), is(new PercentType(100)));
221 v.update(OnOffType.OFF);
222 assertThat((PercentType) v.getChannelState(), is(new PercentType(0)));
226 public void percentMQTTValue() {
227 PercentageValue v = new PercentageValue(null, null, null, null, null);
228 v.update(new DecimalType("10.10000"));
229 assertThat(v.getMQTTpublishValue(null), is("10.1"));
230 for (int i = 0; i <= 100; i++) {
231 v.update(new DecimalType(i));
232 assertThat(v.getMQTTpublishValue(null), is("" + i));
237 public void percentCustomOnOff() {
238 PercentageValue v = new PercentageValue(new BigDecimal("0.0"), new BigDecimal("100.0"), new BigDecimal("1.0"),
240 v.update(new StringType("on"));
241 assertThat((PercentType) v.getChannelState(), is(new PercentType(100)));
242 v.update(new StringType("off"));
243 assertThat((PercentType) v.getChannelState(), is(new PercentType(0)));
247 public void decimalCalc() {
248 PercentageValue v = new PercentageValue(new BigDecimal("0.1"), new BigDecimal("1.0"), new BigDecimal("0.1"),
250 v.update(new DecimalType(1.0));
251 assertThat((PercentType) v.getChannelState(), is(new PercentType(100)));
252 v.update(new DecimalType(0.1));
253 assertThat((PercentType) v.getChannelState(), is(new PercentType(0)));
254 v.update(new DecimalType(0.2));
255 assertEquals(((PercentType) v.getChannelState()).floatValue(), 11.11f, 0.01f);
259 public void increaseDecreaseCalc() {
260 PercentageValue v = new PercentageValue(new BigDecimal("1.0"), new BigDecimal("11.0"), new BigDecimal("0.5"),
264 v.update(new DecimalType("6.0"));
265 assertEquals(((PercentType) v.getChannelState()).floatValue(), 50.0f, 0.01f);
266 v.update(IncreaseDecreaseType.INCREASE);
267 assertEquals(((PercentType) v.getChannelState()).floatValue(), 55.0f, 0.01f);
268 v.update(IncreaseDecreaseType.DECREASE);
269 v.update(IncreaseDecreaseType.DECREASE);
270 assertEquals(((PercentType) v.getChannelState()).floatValue(), 45.0f, 0.01f);
273 v.update(new DecimalType("1.1"));
274 assertEquals(((PercentType) v.getChannelState()).floatValue(), 1.0f, 0.01f);
275 v.update(IncreaseDecreaseType.DECREASE);
276 assertEquals(((PercentType) v.getChannelState()).floatValue(), 0.0f, 0.01f);
279 v.update(new DecimalType("10.8"));
280 assertEquals(((PercentType) v.getChannelState()).floatValue(), 98.0f, 0.01f);
281 v.update(IncreaseDecreaseType.INCREASE);
282 assertEquals(((PercentType) v.getChannelState()).floatValue(), 100.0f, 0.01f);
286 public void upDownCalc() {
287 PercentageValue v = new PercentageValue(new BigDecimal("1.0"), new BigDecimal("11.0"), new BigDecimal("0.5"),
291 v.update(new DecimalType("6.0"));
292 assertEquals(((PercentType) v.getChannelState()).floatValue(), 50.0f, 0.01f);
293 v.update(UpDownType.UP);
294 assertEquals(((PercentType) v.getChannelState()).floatValue(), 55.0f, 0.01f);
295 v.update(UpDownType.DOWN);
296 v.update(UpDownType.DOWN);
297 assertEquals(((PercentType) v.getChannelState()).floatValue(), 45.0f, 0.01f);
300 v.update(new DecimalType("1.1"));
301 assertEquals(((PercentType) v.getChannelState()).floatValue(), 1.0f, 0.01f);
302 v.update(UpDownType.DOWN);
303 assertEquals(((PercentType) v.getChannelState()).floatValue(), 0.0f, 0.01f);
306 v.update(new DecimalType("10.8"));
307 assertEquals(((PercentType) v.getChannelState()).floatValue(), 98.0f, 0.01f);
308 v.update(UpDownType.UP);
309 assertEquals(((PercentType) v.getChannelState()).floatValue(), 100.0f, 0.01f);
312 @Test(expected = IllegalArgumentException.class)
313 public void percentCalcInvalid() {
314 PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0), null,
316 v.update(new DecimalType(9.0));