]> git.basschouten.com Git - openhab-addons.git/blob
6d9d02f170edf25d7d320715fe728c14eeebe9ef
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.values;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
18
19 import java.math.BigDecimal;
20
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;
33
34 /**
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.
37  *
38  * The on/off value class should accept a multitude of values including the custom defined ones.
39  *
40  * The string value class states are tested.
41  *
42  * @author David Graeff - Initial contribution
43  */
44 public class ValueTests {
45     Command p(Value v, String str) {
46         return TypeParser.parseCommand(v.getSupportedCommandTypes(), str);
47     }
48
49     @Test
50     public void illegalTextStateUpdate() {
51         TextValue v = new TextValue("one,two".split(","));
52         assertThrows(IllegalArgumentException.class, () -> v.update(p(v, "three")));
53     }
54
55     public void textStateUpdate() {
56         TextValue v = new TextValue("one,two".split(","));
57         v.update(p(v, "one"));
58     }
59
60     public void colorUpdate() {
61         ColorValue v = new ColorValue(ColorMode.RGB, "fancyON", "fancyOFF", 77);
62         v.update(p(v, "255, 255, 255"));
63
64         v.update(p(v, "OFF"));
65         assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(0));
66         v.update(p(v, "ON"));
67         assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(77));
68
69         v.update(p(v, "0"));
70         assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(0));
71         v.update(p(v, "1"));
72         assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(1));
73     }
74
75     @Test
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")));
79     }
80
81     @Test
82     public void illegalNumberCommand() {
83         NumberValue v = new NumberValue(null, null, null, null);
84         assertThrows(IllegalArgumentException.class, () -> v.update(OnOffType.OFF));
85     }
86
87     @Test
88     public void illegalPercentCommand() {
89         PercentageValue v = new PercentageValue(null, null, null, null, null);
90         assertThrows(IllegalStateException.class, () -> v.update(new StringType("demo")));
91     }
92
93     @Test
94     public void illegalOnOffCommand() {
95         OnOffValue v = new OnOffValue(null, null);
96         assertThrows(IllegalArgumentException.class, () -> v.update(new DecimalType(101.0)));
97     }
98
99     @Test
100     public void illegalPercentUpdate() {
101         PercentageValue v = new PercentageValue(null, null, null, null, null);
102         assertThrows(IllegalArgumentException.class, () -> v.update(new DecimalType(101.0)));
103     }
104
105     @Test
106     public void onoffUpdate() {
107         OnOffValue v = new OnOffValue("fancyON", "fancyOff");
108         // Test with command
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));
115
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));
123
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));
133     }
134
135     @Test
136     public void openCloseUpdate() {
137         OpenCloseValue v = new OpenCloseValue("fancyON", "fancyOff");
138         // Test with command
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));
145
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));
153
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));
161     }
162
163     @Test
164     public void rollershutterUpdateWithStrings() {
165         RollershutterValue v = new RollershutterValue("fancyON", "fancyOff", "fancyStop");
166         // Test with command
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));
173
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)));
184     }
185
186     @Test
187     public void rollershutterUpdateWithOutStrings() {
188         RollershutterValue v = new RollershutterValue(null, null, "fancyStop");
189         // Test with command
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));
196
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)));
207     }
208
209     @Test
210     public void percentCalc() {
211         PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0), null,
212                 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"));
219
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)));
224     }
225
226     @Test
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));
234         }
235     }
236
237     @Test
238     public void percentCustomOnOff() {
239         PercentageValue v = new PercentageValue(new BigDecimal("0.0"), new BigDecimal("100.0"), new BigDecimal("1.0"),
240                 "on", "off");
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)));
245     }
246
247     @Test
248     public void decimalCalc() {
249         PercentageValue v = new PercentageValue(new BigDecimal("0.1"), new BigDecimal("1.0"), new BigDecimal("0.1"),
250                 null, null);
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);
257     }
258
259     @Test
260     public void increaseDecreaseCalc() {
261         PercentageValue v = new PercentageValue(new BigDecimal("1.0"), new BigDecimal("11.0"), new BigDecimal("0.5"),
262                 null, null);
263
264         // Normal operation.
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);
272
273         // Lower limit.
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);
278
279         // Upper limit.
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);
284     }
285
286     @Test
287     public void upDownCalc() {
288         PercentageValue v = new PercentageValue(new BigDecimal("1.0"), new BigDecimal("11.0"), new BigDecimal("0.5"),
289                 null, null);
290
291         // Normal operation.
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);
299
300         // Lower limit.
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);
305
306         // Upper limit.
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);
311     }
312
313     @Test
314     public void percentCalcInvalid() {
315         PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0), null,
316                 null);
317         assertThrows(IllegalArgumentException.class, () -> v.update(new DecimalType(9.0)));
318     }
319 }