]> git.basschouten.com Git - openhab-addons.git/blob
897833e3f7713a8818ecfd099153bde2908e5a8f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.deconz.internal.handler;
14
15 import static org.openhab.binding.deconz.internal.BindingConstants.*;
16
17 import java.io.IOException;
18 import java.util.List;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.api.extension.ExtendWith;
24 import org.mockito.junit.jupiter.MockitoExtension;
25 import org.mockito.junit.jupiter.MockitoSettings;
26 import org.mockito.quality.Strictness;
27 import org.openhab.binding.deconz.internal.Util;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.types.StringType;
32 import org.openhab.core.types.UnDefType;
33
34 /**
35  * The {@link SensorThingHandlerTest} contains test classes for the {@link SensorThingHandler}
36  *
37  * @author Jan N. Klug - Initial contribution
38  */
39 @ExtendWith(MockitoExtension.class)
40 @MockitoSettings(strictness = Strictness.LENIENT)
41 @NonNullByDefault
42 public class SensorThingHandlerTest extends BaseDeconzThingHandlerTest {
43
44     @Test
45     public void testAirQuality() throws IOException {
46         createThing(THING_TYPE_AIRQUALITY_SENSOR,
47                 List.of(CHANNEL_AIRQUALITY, CHANNEL_AIRQUALITYPPB, CHANNEL_LAST_UPDATED), SensorThingHandler::new);
48
49         Set<TestParam> expected = Set.of(
50                 // standard channels
51                 new TestParam(CHANNEL_AIRQUALITY, new StringType("good")),
52                 new TestParam(CHANNEL_AIRQUALITYPPB, new QuantityType<>("129 ppb")),
53                 new TestParam(CHANNEL_LAST_UPDATED, Util.convertTimestampToDateTime("2021-12-29T01:18:41.184")),
54                 // battery
55                 new TestParam(CHANNEL_BATTERY_LEVEL, new DecimalType(100)),
56                 new TestParam(CHANNEL_BATTERY_LOW, OnOffType.OFF),
57                 // last seen
58                 new TestParam(CHANNEL_LAST_SEEN, Util.convertTimestampToDateTime("2021-12-29T01:18Z")));
59
60         assertThing("json/sensors/airquality.json", expected);
61     }
62
63     @Test
64     public void testCarbonMonoxide() throws IOException {
65         createThing(THING_TYPE_CARBONMONOXIDE_SENSOR, List.of(CHANNEL_CARBONMONOXIDE, CHANNEL_LAST_UPDATED),
66                 SensorThingHandler::new);
67
68         Set<TestParam> expected = Set.of(
69                 // standard channels
70                 new TestParam(CHANNEL_CARBONMONOXIDE, OnOffType.ON),
71                 new TestParam(CHANNEL_LAST_UPDATED, UnDefType.UNDEF),
72                 // battery
73                 new TestParam(CHANNEL_BATTERY_LEVEL, new DecimalType(100)),
74                 new TestParam(CHANNEL_BATTERY_LOW, OnOffType.OFF));
75
76         assertThing("json/sensors/carbonmonoxide.json", expected);
77     }
78
79     @Test
80     public void testFire() throws IOException {
81         createThing(THING_TYPE_FIRE_SENSOR, List.of(CHANNEL_FIRE, CHANNEL_LAST_UPDATED), SensorThingHandler::new);
82
83         Set<TestParam> expected = Set.of(
84                 // standard channels
85                 new TestParam(CHANNEL_FIRE, OnOffType.OFF), new TestParam(CHANNEL_LAST_UPDATED, UnDefType.UNDEF),
86                 // battery
87                 new TestParam(CHANNEL_BATTERY_LEVEL, new DecimalType(98)),
88                 new TestParam(CHANNEL_BATTERY_LOW, OnOffType.OFF));
89
90         assertThing("json/sensors/fire.json", expected);
91     }
92
93     @Test
94     public void testSwitch() throws IOException {
95         createThing(THING_TYPE_SWITCH, List.of(CHANNEL_BUTTON, CHANNEL_BUTTONEVENT, CHANNEL_LAST_UPDATED),
96                 SensorThingHandler::new);
97
98         Set<TestParam> expected = Set.of(
99                 // standard channels
100                 new TestParam(CHANNEL_BUTTON, new DecimalType(1002)), new TestParam(CHANNEL_BUTTONEVENT, null),
101                 new TestParam(CHANNEL_LAST_UPDATED, Util.convertTimestampToDateTime("2022-02-15T13:36:16.271")),
102                 // battery
103                 new TestParam(CHANNEL_BATTERY_LEVEL, new DecimalType(7)),
104                 new TestParam(CHANNEL_BATTERY_LOW, OnOffType.ON));
105
106         assertThing("json/sensors/switch.json", expected);
107     }
108
109     @Test
110     public void testVibration() throws IOException {
111         createThing(THING_TYPE_VIBRATION_SENSOR, List.of(CHANNEL_LAST_UPDATED, CHANNEL_VIBRATION),
112                 SensorThingHandler::new);
113
114         Set<TestParam> expected = Set.of(
115                 // standard channels
116                 new TestParam(CHANNEL_LAST_UPDATED, Util.convertTimestampToDateTime("2022-09-09T18:13:44.653")),
117                 new TestParam(CHANNEL_VIBRATION, OnOffType.ON),
118                 // battery
119                 new TestParam(CHANNEL_BATTERY_LEVEL, new DecimalType(100)),
120                 new TestParam(CHANNEL_BATTERY_LOW, OnOffType.OFF),
121                 // last seen
122                 new TestParam(CHANNEL_LAST_SEEN, Util.convertTimestampToDateTime("2022-09-09T18:13Z")),
123                 // dynamic channels
124                 new TestParam(CHANNEL_ORIENTATION_X, new DecimalType(3)),
125                 new TestParam(CHANNEL_ORIENTATION_Y, new DecimalType(-1)),
126                 new TestParam(CHANNEL_ORIENTATION_Z, new DecimalType(-87)),
127                 new TestParam(CHANNEL_TEMPERATURE, new QuantityType<>("26.00 °C")),
128                 new TestParam(CHANNEL_TILTANGLE, new QuantityType<>("176 °")),
129                 new TestParam(CHANNEL_VIBRATION_STRENGTH, new DecimalType(110)));
130
131         assertThing("json/sensors/vibration.json", expected);
132     }
133 }