2 * Copyright (c) 2010-2023 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.deconz.internal.handler;
15 import static org.openhab.binding.deconz.internal.BindingConstants.*;
17 import java.io.IOException;
18 import java.util.List;
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;
35 * The {@link SensorThingHandlerTest} contains test classes for the {@link SensorThingHandler}
37 * @author Jan N. Klug - Initial contribution
39 @ExtendWith(MockitoExtension.class)
40 @MockitoSettings(strictness = Strictness.LENIENT)
42 public class SensorThingHandlerTest extends BaseDeconzThingHandlerTest {
45 public void testAirQuality() throws IOException {
46 createThing(THING_TYPE_AIRQUALITY_SENSOR,
47 List.of(CHANNEL_AIRQUALITY, CHANNEL_AIRQUALITYPPB, CHANNEL_LAST_UPDATED), SensorThingHandler::new);
49 Set<TestParam> expected = Set.of(
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")),
55 new TestParam(CHANNEL_BATTERY_LEVEL, new DecimalType(100)),
56 new TestParam(CHANNEL_BATTERY_LOW, OnOffType.OFF),
58 new TestParam(CHANNEL_LAST_SEEN, Util.convertTimestampToDateTime("2021-12-29T01:18Z")));
60 assertThing("json/sensors/airquality.json", expected);
64 public void testCarbonMonoxide() throws IOException {
65 createThing(THING_TYPE_CARBONMONOXIDE_SENSOR, List.of(CHANNEL_CARBONMONOXIDE, CHANNEL_LAST_UPDATED),
66 SensorThingHandler::new);
68 Set<TestParam> expected = Set.of(
70 new TestParam(CHANNEL_CARBONMONOXIDE, OnOffType.ON),
71 new TestParam(CHANNEL_LAST_UPDATED, UnDefType.UNDEF),
73 new TestParam(CHANNEL_BATTERY_LEVEL, new DecimalType(100)),
74 new TestParam(CHANNEL_BATTERY_LOW, OnOffType.OFF));
76 assertThing("json/sensors/carbonmonoxide.json", expected);
80 public void testFire() throws IOException {
81 createThing(THING_TYPE_FIRE_SENSOR, List.of(CHANNEL_FIRE, CHANNEL_LAST_UPDATED), SensorThingHandler::new);
83 Set<TestParam> expected = Set.of(
85 new TestParam(CHANNEL_FIRE, OnOffType.OFF), new TestParam(CHANNEL_LAST_UPDATED, UnDefType.UNDEF),
87 new TestParam(CHANNEL_BATTERY_LEVEL, new DecimalType(98)),
88 new TestParam(CHANNEL_BATTERY_LOW, OnOffType.OFF));
90 assertThing("json/sensors/fire.json", expected);
94 public void testSwitch() throws IOException {
95 createThing(THING_TYPE_SWITCH, List.of(CHANNEL_BUTTON, CHANNEL_BUTTONEVENT, CHANNEL_LAST_UPDATED),
96 SensorThingHandler::new);
98 Set<TestParam> expected = Set.of(
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")),
103 new TestParam(CHANNEL_BATTERY_LEVEL, new DecimalType(7)),
104 new TestParam(CHANNEL_BATTERY_LOW, OnOffType.ON));
106 assertThing("json/sensors/switch.json", expected);
110 public void testVibration() throws IOException {
111 createThing(THING_TYPE_VIBRATION_SENSOR, List.of(CHANNEL_LAST_UPDATED, CHANNEL_VIBRATION),
112 SensorThingHandler::new);
114 Set<TestParam> expected = Set.of(
116 new TestParam(CHANNEL_LAST_UPDATED, Util.convertTimestampToDateTime("2022-09-09T18:13:44.653")),
117 new TestParam(CHANNEL_VIBRATION, OnOffType.ON),
119 new TestParam(CHANNEL_BATTERY_LEVEL, new DecimalType(100)),
120 new TestParam(CHANNEL_BATTERY_LOW, OnOffType.OFF),
122 new TestParam(CHANNEL_LAST_SEEN, Util.convertTimestampToDateTime("2022-09-09T18:13Z")),
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)));
131 assertThing("json/sensors/vibration.json", expected);