]> git.basschouten.com Git - openhab-addons.git/blob
6d35ee9360089feeccdd90f4ec397d941925780b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.junit.Assert.*;
17
18 import java.math.BigDecimal;
19
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;
32
33 /**
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.
36  *
37  * The on/off value class should accept a multitude of values including the custom defined ones.
38  *
39  * The string value class states are tested.
40  *
41  * @author David Graeff - Initial contribution
42  */
43 public class ValueTests {
44     Command p(Value v, String str) {
45         return TypeParser.parseCommand(v.getSupportedCommandTypes(), str);
46     }
47
48     @Test(expected = IllegalArgumentException.class)
49     public void illegalTextStateUpdate() {
50         TextValue v = new TextValue("one,two".split(","));
51         v.update(p(v, "three"));
52     }
53
54     public void textStateUpdate() {
55         TextValue v = new TextValue("one,two".split(","));
56         v.update(p(v, "one"));
57     }
58
59     public void colorUpdate() {
60         ColorValue v = new ColorValue(ColorMode.RGB, "fancyON", "fancyOFF", 77);
61         v.update(p(v, "255, 255, 255"));
62
63         v.update(p(v, "OFF"));
64         assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(0));
65         v.update(p(v, "ON"));
66         assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(77));
67
68         v.update(p(v, "0"));
69         assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(0));
70         v.update(p(v, "1"));
71         assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(1));
72     }
73
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"));
78     }
79
80     @Test(expected = IllegalArgumentException.class)
81     public void illegalNumberCommand() {
82         NumberValue v = new NumberValue(null, null, null, null);
83         v.update(OnOffType.OFF);
84     }
85
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"));
90     }
91
92     @Test(expected = IllegalArgumentException.class)
93     public void illegalOnOffCommand() {
94         OnOffValue v = new OnOffValue(null, null);
95         v.update(new DecimalType(101.0));
96     }
97
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));
102     }
103
104     @Test
105     public void onoffUpdate() {
106         OnOffValue v = new OnOffValue("fancyON", "fancyOff");
107         // Test with command
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));
114
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));
122
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));
132     }
133
134     @Test
135     public void openCloseUpdate() {
136         OpenCloseValue v = new OpenCloseValue("fancyON", "fancyOff");
137         // Test with command
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));
144
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));
152
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));
160     }
161
162     @Test
163     public void rollershutterUpdateWithStrings() {
164         RollershutterValue v = new RollershutterValue("fancyON", "fancyOff", "fancyStop");
165         // Test with command
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));
172
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)));
183     }
184
185     @Test
186     public void rollershutterUpdateWithOutStrings() {
187         RollershutterValue v = new RollershutterValue(null, null, "fancyStop");
188         // Test with command
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));
195
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)));
206     }
207
208     @Test
209     public void percentCalc() {
210         PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0), null,
211                 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"));
218
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)));
223     }
224
225     @Test
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));
233         }
234     }
235
236     @Test
237     public void percentCustomOnOff() {
238         PercentageValue v = new PercentageValue(new BigDecimal("0.0"), new BigDecimal("100.0"), new BigDecimal("1.0"),
239                 "on", "off");
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)));
244     }
245
246     @Test
247     public void decimalCalc() {
248         PercentageValue v = new PercentageValue(new BigDecimal("0.1"), new BigDecimal("1.0"), new BigDecimal("0.1"),
249                 null, null);
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);
256     }
257
258     @Test
259     public void increaseDecreaseCalc() {
260         PercentageValue v = new PercentageValue(new BigDecimal("1.0"), new BigDecimal("11.0"), new BigDecimal("0.5"),
261                 null, null);
262
263         // Normal operation.
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);
271
272         // Lower limit.
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);
277
278         // Upper limit.
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);
283     }
284
285     @Test
286     public void upDownCalc() {
287         PercentageValue v = new PercentageValue(new BigDecimal("1.0"), new BigDecimal("11.0"), new BigDecimal("0.5"),
288                 null, null);
289
290         // Normal operation.
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);
298
299         // Lower limit.
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);
304
305         // Upper limit.
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);
310     }
311
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,
315                 null);
316         v.update(new DecimalType(9.0));
317     }
318 }